简体   繁体   中英

Flutter - How to make PageView & ListView?

I'm trying to make a Carousel using PageView, PageController and ListView from this Horizontally scrollable cards with Snap effect in flutter . But it throwed this exception...

══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════ I/flutter (17678): The following assertion was thrown during performResize(): I/flutter (17678): Horizontal viewport was given unbounded height. I/flutter (17678): Viewports expand in the cross axis to fill their container and constrain their children to match I/flutter (17678): their extent in the cross axis. In this case, a horizontal viewport was given an unlimited amount of I/flutter (17678): vertical space in which to expand.

Can someone help me to fix it?

I want to add this Carousel inside of Stack-filled with background image, transform class, and fade transition.

  @override
  void initState() {
   super.initState();
    controller = PageController(
     initialPage: 0,
     keepPage: true,
    );

 @override
  Widget build(BuildContext context) {

    return AnimatedBuilder(
        builder: (BuildContext context, Widget child) {
         return Scaffold(
           //BODY
          body: ListView(children: <Widget>[
            new Stack(
              children: <Widget>[
                new AspectRatio(...),
                new Transform(...),
                //THIS IS
                new ListView.builder(
                  itemCount: 3,
                  scrollDirection: Axis.horizontal,
                  padding: EdgeInsets.symmetric(vertical: 16.0),
                  itemBuilder: (BuildContext context, int index) {
                    if (index % 3 == 0) {
                      return _buildCarousel(context, index ~/ 3);
                    } else {
                      return Divider();
                    }
                  },
                ),
            }
         }
   }
   Widget _buildCarousel(BuildContext context, int carouselIndex) {
    return Column(
     mainAxisSize: MainAxisSize.min,
     children: <Widget>[
       Text('Carousel $carouselIndex'),
       SizedBox(
       // you may want to use an aspect ratio here for tablet support
         height: 200.0,
         child: PageView.builder(
        // store this controller in a State to save the carousel scroll position
         controller: PageController(viewportFraction: 0.8),
         itemBuilder: (BuildContext context, int itemIndex) {
           return _buildCarouselItem(context, carouselIndex, itemIndex);
         },
       ),
     )
   ],
 );
  Widget _buildCarouselItem(
    BuildContext context, int carouselIndex, int itemIndex) {
     return Padding(
     padding: EdgeInsets.symmetric(horizontal: 4.0),
       child: Container(
        decoration: BoxDecoration(
        color: Colors.grey,
        borderRadius: BorderRadius.all(Radius.circular(4.0)),
      ),
    ),
  );

This is the full code https://pastebin.com/xXRkaWuR

As you might have guessed from the error, basically it means since you haven't specified a finite height, ListView is getting infinite height. Try using shrinkWrap: true inside your ListView.builder and ListView .

Or alternatively you can also try wrapping your ListView s in a Container or SizedBox of finite height.

Example-

Container(
  height: 200.0,
  child: ListView(
   /*Remaining Code*/
  ),
)

You can try doing the same with ListView.builder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM