flutter - Flutter FutureBuilder/Stream how to show 2 different widgets in ConnectionState.waiting depending on given time
问题描述
I want to show a Text()
widget in the first second of ConnectionState.waiting
, but if it takes longer than a second, I want to show a different Widget with a custom Animation.
Simplified code (StatelessWidget):
...
Stream
...
if (snapshot.connectionState == ConnectionState.waiting) {
return Text('Loading data');
return CustomWidget(); <- Show this widget instead of Text if more than 1 sec
}
解决方案
I do not know if this is what you need.
First define a DateTime variable:
DateTime time = DateTime.now();
Then in your build method set with the current DateTime:
@override
Widget build(BuildContext context) {
time = DateTime.now();
Then in your result you can check the "time" variable with the current DateTime like this:
if (snapshot.connectionState == ConnectionState.waiting) {
if (DateTime.now().difference(time).inSeconds>10){
return Text("More than 10 seconds");
}
else {
return Text('Less than 10 seconds');
}
}
推荐阅读
- dns - 为什么我的 dns 更改需要这么长时间才能更新?
- javascript - HTML/JS:将范围控件转换为数字输入并返回
- linux - 未知错误:找不到 Chrome 二进制文件,但它已经在 Linux 服务器的 Jenkins 文件夹中
- html - 如何在 css 中垂直居中这些表格行的内容?
- python - Django:dbbackup 显示 pg_dump:错误:命令行参数过多
- wpf - 在 Shell 中加载所有区域时打开一个对话框
- html - line-height: 1.4 是否与 line-height: 140% 相同?
- r - 使用 Dplyr - 股票数据计算组之间的百分比变化
- laravel - Laravel - softDelete设置空关系
- django - Django REST Framework - 在 ApiClient 的序列化程序测试中设置请求