0

我有一个 streambuidler 小部件,其中包含一个列表视图,用于显示当前用户最近与谁聊天。当当前用户收到一条消息,该消息应该被推送到列表视图的顶部,但是该消息总是显示在列表视图的底部,如果我刷新我的屏幕,它只会显示在顶部。

NoSearchResultScreen() {
final Orientation orientation = MediaQuery.of(context).orientation;
print("hasAlreadyChatWithSomeone: $hasAlreadyChatWithSomeone");
return hasAlreadyChatWithSomeone
    ? StreamBuilder<QuerySnapshot>(
        stream: (FirebaseFirestore.instance
            .collection("user")
            .where("id", isEqualTo: currentUserId)
            .snapshots()),
        builder: (context, snapshot) {

          List<ProfileChatWith> chatWithList = [];
          if (snapshot.connectionState == ConnectionState.waiting) {
            return Center(
              child: circularProgress(),
            );
          }
          if (snapshot.hasData) {
            final chatWithSnapshot = snapshot.data?.docs.first['chatWith'];
            //print("chatWithSnapshot: $chatWithSnapshot");
            for (var userChatWith in chatWithSnapshot) {
              final user = ProfileChatWith(
                userId: userChatWith,
                currentUserId: currentUserId,
              );
              chatWithList.add(user);
              //print("I have chatted with: $userChatWith");
            }
            return Container(
              width: MediaQuery.of(context).size.width,
              child: ListView(
                //shrinkWrap: true,
                children: chatWithList,
              ),
            );
          } else {
            return Center(
              child: circularProgress(),
            );
          }
        },
      )
    : Container(
        child: Center(
          child: ListView(
            shrinkWrap: true,
            children: <Widget>[
              Icon(
                Icons.group,
                color: Colors.greenAccent,
                size: 200.0,
              ),
              Text(
                "Search Users",
                textAlign: TextAlign.center,
                style: TextStyle(
                    color: Colors.greenAccent,
                    fontSize: 50.0,
                    fontWeight: FontWeight.bold),
              )
            ],
          ),
        ),
      );

}

4

2 回答 2

0

尝试反向:真,

return SizedBox(
            width: MediaQuery.of(context).size.width,
  child: ListView(
    reverse: true,
    children: chatWithList,
  ),
);

使用 Listview.builder 进行性能优化

return SizedBox(
      width: MediaQuery.of(context).size.width,
      child: ListView.builder(
        reverse: true, 
        itemBuilder: (BuildContext context, int index) => chatWithList[index],
      ),
    );
于 2021-12-27T09:10:29.553 回答
0

有效的解决方案如下,@Leo Tran 自己的话

我找到了解决我的问题的方法是,只要数据更新,我就会重建我的小部件。

于 2022-01-14T12:05:55.070 回答