carousel_slider(flutter package)中的"items: child"是什么意思?

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

在 carousel_slider 中,“items”被定义为“要在默认构造函数的轮播中显示的小部件”,并且“items”的类型为“{List items}”。下面代码中的“items: child”是什么意思?具体指的是哪个子widget?

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('→'),
      )
    ]
  );
}

它像默认构造函数一样工作 ListView(children: [widgets here]) -

所以只需将小部件放在那里 CarouselSlider(items: [ Text('foo'),Text('bar'),])

关于您的信息 - CarouselSlider 已将构造函数命名为 builder() 以按需构建小部件,类似于 ListView.builder()

我认为你可以忽略它(可能是打字错误)并将其用作其他示例:

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(),
)

这就是您要在轮播中显示的项目。

正如文档 (https://pub.dev/documentation/carousel_slider/latest/carousel_slider/CarouselSlider-class.html) 所说:

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

因此您只需要提供一个 Widget 类型的列表,它可以是任何东西:图像、容器,您可以命名。