如何动态地将照片添加到抖动轮播中?

时间:2020-09-18 01:39:34

标签: flutter carousel

我正在尝试将照片动态添加到carousel_slider插件,但是我不能简单地将项目添加到列表中。有人知道如何添加吗?

下面的示例显示我正在使用FloatActionButton将图像添加到imgList变量中。但是轮播无法获得这个新的附加值。

我已经尝试在setState()变量上调用imgList。但是没有成功。

Widget build(BuildContext context) {
    return Scaffold(
        floatingActionButton: Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children:
          [  FloatingActionButton(
              onPressed: () {
                print('save');
                imgList.add( 'https://images.unsplash.com/photo-1508704019882-f9cf40e475b4?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=8c6e5e3aba713b17aa1fe71ab4f0ae5b&auto=format&fit=crop&w=1352&q=80');
              },
              child: Icon(Icons.photo_camera),
              heroTag: null,
            ),
            SizedBox(
              height: 10,
            ),
            FloatingActionButton(
              onPressed: () => print('save'),
              child: Icon(Icons.check_circle),
              heroTag: null,
            ),
          ],
        ),
        appBar: AppBar(
          title: Text('Test carousel'),
          centerTitle: true,
          elevation: 0.0,
          toolbarOpacity: 0.5,
        ),
        body: Container(
        child: CarouselSlider(
          options: CarouselOptions(
            disableCenter: true,
          ),
          items: imgList.map((item) => Container(
            child: Image.network(item, fit: BoxFit.cover, width: 1000),
            color: Colors.green,
          )).toList(),
        ))     
    );

1 个答案:

答案 0 :(得分:0)

我已经尝试过setState添加图像。成功了。 您可以重试。

我的代码:

List<String> _image = [
"https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/1200px-Image_created_with_a_mobile_phone.png",
"https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRnltfxyRHuEEUE4gIZp9fr77Q8goigP7mQ6Q&usqp=CAU",
"https://imageproxy.themaven.net//https%3A%2F%2Fwww.history.com%2F.image%2FMTY1MTc3MjE0MzExMDgxNTQ1%2Ftopic-golden-gate-bridge-gettyimages-177770941.jpg",
"https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcQ-fff2lftqIE077pFAKU1Mhbcj8YFvBbMvpA&usqp=CAU",
"https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRU2U6QAHfoDaofFbNo4OLtaqYWzihF5d4fhw&usqp=CAU"];

@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text("Modular"),
  ),
  body: Center(
    child: Column(
      children: [
        CarouselSlider(
          options: CarouselOptions(
            disableCenter: true
          ),
          items: _image.map((e) => Container(
            child: Image.network(e),
          )).toList(),
        ),
        FlatButton(
          child: Text('abc'),
          onPressed: (){
            setState(() {
              _image.add("https://miro.medium.com/max/1200/1*mk1-6aYaf_Bes1E3Imhc0A.jpeg");
            });
          },
        )
      ],
    ),
  ),
);

}