flutter 银单为空时的消息

bvjxkvbb  于 2023-01-14  发布在  Flutter
关注(0)|答案(1)|浏览(62)

如何在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()));
        }
      }),
    );
  }
0dxa2lsx

0dxa2lsx1#

检查allUsers列表的长度,如果为空,则返回显示消息的小部件,否则返回SliverList

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 ...',
        ),
      ),
    ),
    if (allUsers.isEmpty) ...[
      SliverToBoxAdapter( 
          child: Container(
            // child widget to show when users is empty
          ),
      )
    ] else ...[
      SliverList(
        delegate: SliverChildBuilderDelegate(
          (BuildContext context, int index) {
            return User(
              otherUser: allUsers[index],
            );
          },
          childCount: allUsers.length,
        ),
      ),
    ],
  ],
);

相关问题