Flutter 如何在Flatter中的NetworkImage()中添加网络图像列表

Flutter 如何在Flatter中的NetworkImage()中添加网络图像列表,flutter,dart,Flutter,Dart,我想用多个图像进行幻灯片放映,但我的图像是以json作为列表视图的,我不知道如何才能只用列表进行幻灯片放映 那么,如何在NetworkImage()中通过一个列表而不仅仅是一个图像来实现呢 class SlideShow extends StatefulWidget { _SlideShowState createState() => new _SlideShowState(); } class _SlideShowState extends State<SlideShow&g

我想用多个图像进行幻灯片放映,但我的图像是以json作为列表视图的,我不知道如何才能只用列表进行幻灯片放映

那么,如何在NetworkImage()中通过一个列表而不仅仅是一个图像来实现呢

class SlideShow extends StatefulWidget {
  _SlideShowState createState() => new _SlideShowState();
}

class _SlideShowState extends State<SlideShow>
    with SingleTickerProviderStateMixin {
  Animation<double> animation;
  AnimationController controller;
  List<GallariesModel> _images = <GallariesModel>[];

  initState() {
    super.initState();
    controller = new AnimationController(
        duration: const Duration(milliseconds: 2000), vsync: this);
    animation = new Tween(begin: 0.0, end: 18.0).animate(controller)
      ..addListener(() {
        setState(() {
          // the state that has changed here is the animation object’s value
          listenForGallaries();
        });
      });
    controller.forward();
  }

  void listenForGallaries() async {
    final Stream<GallariesModel> stream = await getGallaries();
    stream.listen(
        (GallariesModel galaries) => setState(() => _images.add(galaries)));
  }

  @override
  Widget build(BuildContext context) {
    double screenHeight = MediaQuery.of(context).size.height;

    Widget carousel = new Carousel(
      boxFit: BoxFit.cover,
      images: [
        NetworkImage("www.imagelink.com/"+
            _images.first.file),
             NetworkImage("www.imagelink.com/"+
            _images.last.file),
      ],
      animationCurve: Curves.fastOutSlowIn,
      animationDuration: Duration(seconds: 1),
    );

    return new Container(
      margin: EdgeInsets.all(13.0),
      height: screenHeight / 3.5,
      child: new ClipRRect(
        borderRadius: BorderRadius.circular(5.0),
        child: new Stack(
          children: [
            carousel,
          ],
        ),
      ),
    );
  }

  dispose() {
    controller.dispose();
    super.dispose();
  }
}
类幻灯片扩展StatefulWidget{
_SlideShowState createState()=>new_SlideShowState();
}
类_SlideShowState扩展状态
使用SingleTickerProviderStateMixin{
动画;
动画控制器;
列表_图像=[];
initState(){
super.initState();
控制器=新的AnimationController(
持续时间:常量持续时间(毫秒:2000),vsync:this;
动画=新吐温(开始:0.0,结束:18.0)。动画(控制器)
…addListener(){
设置状态(){
//此处更改的状态是动画对象的值
listenForGallaries();
});
});
controller.forward();
}
void listenformallaries()异步{
最终流=等待getGallaries();
听我说(
(GallariesModelGalaries)=>setState(()=>\u images.add(galaries));
}
@凌驾
小部件构建(构建上下文){
double screenHeight=MediaQuery.of(context).size.height;
小部件旋转木马=新旋转木马(
boxFit:boxFit.cover,
图像:[
NetworkImage(“www.imagelink.com/”+
_images.first.file),
NetworkImage(“www.imagelink.com/”+
_images.last.file),
],
动画曲线:Curves.FastOutSwowin,
动画持续时间:持续时间(秒数:1),
);
退回新货柜(
边距:所有边缘集(13.0),
高度:屏幕高度/3.5,
子项:新ClipRRect(
边界半径:边界半径。圆形(5.0),
子:新堆栈(
儿童:[
旋转木马
],
),
),
);
}
处置{
controller.dispose();
super.dispose();
}
}

现在,我使用某种方式查看列表中的第一张图像和最后一张图像。是否有任何方法可以帮助我执行此操作

您可以使用映射功能将所有图像更改为列表网络图像:

images: _images.map((GallariesModel image) {
      return new NetworkImage(
       "www.imagelink.com/"+image.file));
      }).toList(),

@hoangquyy提议的一种更简单的方法是:

_images.map((image) => NetworkImage('www.imagelink.com/${image.file}')).toList()

没错,但它不应该在括号[]内,因为您已经返回了一个列表。toList()我得到了此错误类型“list”不是“ImageProvider”类型的子类型我得到了此错误类型“list”不是“ImageProvider”类型的子类型