Search code examples
flutterdartcarousel

What does "items: child" in carousel_slider (flutter package) mean?


In carousel_slider "items" is defined as "The widgets to be shown in the carousel of default constructor" and "items" has a type of "{List items}". What does the "items: child" mean in the code below? To be specific which child widget does it refer to?

class CarouselDemo extends StatelessWidget {
  CarouselController buttonCarouselController = CarouselController();

 @override
  Widget build(BuildContext context) => Column(
    children: <Widget>[
      CarouselSlider(
        **items: child,**
        carouselController: buttonCarouselController,
        options: CarouselOptions(
          autoPlay: false,
          enlargeCenterPage: true,
          viewportFraction: 0.9,
          aspectRatio: 2.0,
          initialPage: 2,
        ),
      ),
      RaisedButton(
        onPressed: () => buttonCarouselController.nextPage(
            duration: Duration(milliseconds: 300), curve: Curves.linear),
        child: Text('→'),
      )
    ]
  );
}

Solution

  • I think you can ignore it (maybe a typo) and use it as the other examples:

    CarouselSlider(
      options: CarouselOptions(height: 400.0),
      items: [1,2,3,4,5].map((i) {
        return Builder(
          builder: (BuildContext context) {
            return Container(
              width: MediaQuery.of(context).size.width,
              margin: EdgeInsets.symmetric(horizontal: 5.0),
              decoration: BoxDecoration(
                color: Colors.amber
              ),
              child: Text('text $i', style: TextStyle(fontSize: 16.0),)
            );
          },
        );
      }).toList(),
    )
    

    It is just the items that you want to show on the carousel.

    As the docs (https://pub.dev/documentation/carousel_slider/latest/carousel_slider/CarouselSlider-class.html) says:

    items → List<Widget> The widgets to be shown in the carousel of default constructor.

    So you just need to provide a List of the type Widget and it can be anything: an image, container, you name it.