dart - 如何在旋转木马 View 中添加 fadeinImage

标签 dart flutter carousel flutter-layout

我有以下代码在轮播中显示网络图像。当我启动我的应用程序时,我在轮播中看到一个空白的白色屏幕。我想改为显示占位符图像。我试图将占位符图像放在 if 子句下,但 if 子句仅在我导航到另一个屏幕并返回轮播屏幕时才显示占位符。我希望在启动我的应用程序时显示占位符。我怎样才能做到这一点?

return Container(
  child: FutureBuilder(
      future: getCarouselWidget(),
      builder: (context, AsyncSnapshot snapshot) {
        List<NetworkImage> list = new List<NetworkImage>();
        if (snapshot.connectionState == ConnectionState.waiting || snapshot.connectionState == ConnectionState.active
        ) {
          debugPrint("connection state is " + snapshot.connectionState.toString() );
          return new FadeInImage(
            height: 200.0, // Change it to your need
            width: 300.0, // Change it to your need
            fit: BoxFit.cover,
            placeholder: new AssetImage("assets/placeholder.jpg"),
            image: new AssetImage("assets/placeholder.jpg"),
          );
        } else if(snapshot.connectionState == ConnectionState.done) {
          debugPrint("connection state is inside else" + snapshot.connectionState.toString() );
          if (snapshot.hasError) {
            return new Text("fetch error");
          } else {
            for (int i = 0; i < snapshot.data.length; i++) {
              //debugPrint("Index is " + idx.toString());
              list.add(NetworkImage(snapshot.data[i].data["image"]));
              //idx++;
            }
            return new Container(
                height: 250.0,
                child: InkWell(
                    child: new Carousel(
                      boxFit: BoxFit.cover,
                      images: list,
                      onImageTap: (imageIndex) {
                        Navigator.of(context).push(
                          new MaterialPageRoute(
                            builder: (context) => new CustomClass(
                                name:
                                    snapshot.data[imageIndex].data["title"],
                                pic: snapshot
                                    .data[imageIndex].data["image"]),
                          ),
                        );
                      },
                      autoplay: false,
                      dotSize: 4.0,
                      indicatorBgPadding: 4.0,
                      animationCurve: Curves.fastOutSlowIn,
                      animationDuration: Duration(milliseconds: 1000),
                    )));
          }
        }
      }),
);

最佳答案

你可以使用cached network image库,它提供了delayplaceholder和fade动画,一个例子:

  child: new CachedNetworkImage(
             imageUrl: "http://via.placeholder.com/350x150",
             placeholder: new Image.asset('assets/placeholder.jpg'),
             errorWidget: new Icon(Icons.error),
             fit: BoxFit.fill,
             fadeInCurve: Curves.easeIn ,
             fadeInDuration: Duration(seconds: 2),
             fadeOutCurve: Curves.easeOut,
             fadeOutDuration: Duration(seconds: 2),
  )

关于dart - 如何在旋转木马 View 中添加 fadeinImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54720757/

相关文章:

dart - 包含多个 Dart 项目的 WebStorm 解决方案

flutter - 无效的参数:URL文件:///Trousers中未指定主机

datetime - 在Flutter获得凌晨0点的简单方法是什么?

css - 在轮播图片下方显示轮播标题

javascript - 实现 twitter bootstrap carousel v2.3.2

flutter - Navigator 没有要替换的事件路线

flutter - 在 flutter block 中处理

flutter - 如何在 Dart 中为条件导入设置自定义属性?

user-interface - 如何在Flutter中制作一个适合整个屏幕的列

javascript - 如何重新定位光滑的点?