如何在SilverList为空时显示消息?我正在尝试使用SliverList和SliverChildBuilderDelegate过滤用户列表。当我键入一些过滤器时,它返回空屏幕,我想显示一些消息。以下是我的代码
Widget build(BuildContext context) {
return Scaffold(
body: BlocBuilder<UsersCubit, UsersState>(builder: (context, userState) {
if (userState is UsersLoaded) {
List<UserEntity> allUsers = userState.users
.where((user) =>
user.name!.contains(_searchController.text.toLowerCase()))
.toList();
return CustomScrollView(
slivers: <Widget>[
SliverAppBar(
floating: true,
snap: true,
automaticallyImplyLeading: false,
elevation: 0,
title: TextField(
controller: _searchController,
style: Theme.of(context).textTheme.bodyText1,
decoration: InputDecoration(
hintText: 'Search ...',
),
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return User(
otherUser: allUsers[index],
);
},
childCount: allUsers.length,
),
),
],
);
} else {
return const Scaffold(
body: Center(child: CircularProgressIndicator()));
}
}),
);
}
1条答案
按热度按时间0dxa2lsx1#
检查
allUsers
列表的长度,如果为空,则返回显示消息的小部件,否则返回SliverList
。