A RenderViewport expected a child of type RenderSliver but received a child of type RenderConstraine

使用以下代码报错A RenderViewport expected a child of type RenderSliver but received a child of type RenderConstraine

// import 'dart:html';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
// import 'package:flutter_swiper/flutter_swiper.dart';

// import 'package:flutter_swiper_null_safety_flutter3/flutter_swiper_null_safety_flutter3.dart';


class AutoPlayPic extends StatefulWidget{
  const AutoPlayPic({Key? key}):super(key: key);

  
  State<StatefulWidget> createState() =>_AutoPlayPic();
  
  
}

class _AutoPlayPic extends State<AutoPlayPic>{
  
  Widget build(BuildContext context) {

    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: const Text(
          "TEST",
          style: TextStyle(
            fontWeight: FontWeight.bold,
            color: Colors.grey,
            fontSize: 18
          ),
        ),
        elevation: 0,
        backgroundColor: const Color.fromRGBO(239, 233, 255, 1),
      ),
      body:buildBanner();
      }, body: SizedBox(),
    )
      ,
    );
  }

  Widget buildBanner() {

    List imgList = [
      "images/ic_oryj.png",
      "images/ic_jzpp.png",
    ];
    return SliverToBoxAdapter(

      child:
      AspectRatio(aspectRatio: 350 / 150,
        child: Swiper(
          itemCount: imgList.length, itemBuilder: (BuildContext context, int index) {
          return Container(

            margin: const EdgeInsets.symmetric(
                vertical: 5,horizontal: 12
            ),
            child: Image.asset(
              imgList[index],
              fit: BoxFit.fill,
            ),
          );
        },
          pagination: const SwiperPagination(
              builder: SwiperPagination.rect),
          control: null,
          autoplay: true,
          loop: true,


        ),

      ),



    );

  }

}

后将buildBanner()使用NestedScrollView包裹,成功运行

body: NestedScrollView(headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
        return [buildBanner()];
      }, body: SizedBox(),

你可能感兴趣的:(flutter)