我有一个RefreshIndicator,当功能完成时加载ListView,但是,我想这样做,如果该功能返回一个空的数据集 - 显示另一个小部件 - '无数据'而不是ListView。
我能够使用三元运算符完成该操作,但是,在刷新期间会显示“无数据”小部件,直到被ListView替换。
如何在刷新完成后显示“无数据”小部件?
这是我的代码:
return RefreshIndicator(
key: _refreshIndicatorKey,
onRefresh: AppConfig.of(context).flavorName != FLAVOR_NAME.DEV
? () {
bloc.page = 1;
return bloc.getTasks(status, context).then((list) {
setState(() {
tasksList = list;
});
});
}
: () => Future.delayed(Duration(seconds: 2)),
child: tasksList.length > 0
? ListView.builder(
physics: const AlwaysScrollableScrollPhysics(),
controller: _scrollController,
itemCount: tasksList.length + 1,
itemBuilder: (context, index) {
if (index == tasksList.length) {
return _buildProgressIndicator();
} else {
Task item = tasksList != null ? tasksList[index] : null;
return GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return TaskDetailsScreen(index, item);
},
settings: RouteSettings(
name: ScreenName.taskDetails)));
},
child: MyTasksListItem(taskData: item));
}
},
)
: Stack(children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Image.asset(
'assets/tumbleweed.gif',
),
]),
Positioned(
bottom: 50,
left: 0.0,
right: 0.0,
child: Align(child: Text(allTranslations.text('no_tasks'))))
]));
在我的情况下,'无数据'小部件是Stack。
也许像这样的解决方案可行:
onRefresh: AppConfig.of(context).flavorName != FLAVOR_NAME.DEV
? () {
setState(() {
loading = true;
});
bloc.page = 1;
return bloc.getTasks(status, context).then((list) {
setState(() {
loading = false;
tasksList = list;
});
});
}
并在你的指标孩子:
child: Container(
child: loading ?
null :
tasksList.length > 0
? ListView.builder(..........