flutter - 如何使用 Shimmer 在 Flutter App 中添加 X 持续时间的延迟?
问题描述
我正在开发 Flutter 应用程序,我想在其中显示加载选项 5 秒,5 秒后它将显示“未找到结果”。
代码
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();
}
},
)
推荐阅读
- html - Highchart 树节点文本溢出
- swift - 当默认模式兼容所有类别时,AVAudioSession 中的其他模式有什么用?
- c++ - 将结构内的数组从 Cpp 写入 QML
- angular - 是否可以在 Angular 中覆盖 MatIconRegistry 中的 HttpClient?
- linux - NGINX - 忽略 js 和 css 文件上的获取参数
- javascript - Redux dispatch 没有正确更新状态
- php - 用 HTML 将所有表情符号包装在一个字符串中?
- python-3.x - 在 wxpython 中的某个点一个接一个地显示位图与它们重叠并且不显示其余部分
- python - 使用 pandas 删除值中不包含字符串的行
- java - 拦截 monitorEnter - 生成的字节码无法 JIT 编译