如何使用Shimmer在Flutter App中增加X持续时间的延迟?

问题描述

我正在Flutter应用程序上工作,我想在其中显示加载选项5秒钟,并在5秒钟后显示“未找到结果”。

CODE

Widget Loading(){
 return Center(
  child: Shimmer.fromColors(
  baseColor: Colors.blueAccent,highlightColor: Colors.red,child:Text("Loading...",style: TextStyle(
      fontWeight: FontWeight.w900,fontSize: 50),textAlign: TextAlign.center,),period: Duration(seconds: 2),);
}

/// Use of Loading() in Flutter pages

 body: (Details == null||Details.isEmpty)?
      Loading()
      : new SizedBox(.............)

// In this case,if "Details" has some data it automatically displays on-screen else,continuously loading(),I want after 5 seconds it displays "No Data Found"

有人知道如何在上面的代码添加计时器/持续时间/延迟吗,所以5秒钟后消息将显示在“找不到数据”屏幕上。

解决方法

您可以为此使用FutureBuilder。这是一个示例:

这是FutureBuilder的未来功能。在此功能中,您可以构建自己的逻辑。

Future<bool> _future = Future<bool>.delayed(
    Duration(seconds: 5),() {
      //do something here
      return true;
    },);

这FutureBuilder应该看起来像

FutureBuilder<bool>(
      future: _future,builder: (BuildContext context,AsyncSnapshot<bool> snapshot) {
        if (snapshot.hasData) {
          return snapshot.data ? SizedBox(.............) : Text("No data found");
        } else {
          return Loading();
        }
      },)