首页 > 解决方案 > Flutter 聊天回复消息

问题描述

我正在尝试使用 ListView 构建聊天 UI。我的第一个版本ListView.build很好用。

我想为“回复消息”选项升级我的聊天视图(类似于whatsapp)。这要求用户能够单击“replyTo”消息并向后滚动到原始消息。ListView.build不允许向上滚动到屏幕视图之外的消息(因为它甚至没有构建)。

body: Column(
    children: [
    Expanded(
        child: EasyRefresh.custom(
        ...
        slivers: <Widget>[
            SliverList(
            delegate: SliverChildBuilderDelegate(
                (context, index) {
                ChatMessage _currMsg = vm.chat[index]!;
                ...
                return Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                    ...
                    SwipeTo(
                        onRightSwipe: () {
                            setState(() {
                                isReplying = true;
                                replyMessage = _currMsg;
                            });
                        },
                        child: ShowMessage(
                            key: Key(_currMsg.id!),
                            msg: _currMsg,
                            myMessage: _currMsg.postById == vm.user!.uid,
                            onTapReply: (id) async {
                                final ctx = _itemKey.currentContext;
                                if (ctx != null)
                                    await Scrollable.ensureVisible(ctx);
                            },
                        ),
                    ),
                    ],
                );
                },
                childCount: vm.chat.length,
            ),
            ),
        ],
        ),
    ),

似乎唯一的方法是首先构建所有列表项。

我的问题是,有没有一种方法可以保留已经构建的项目,并且只添加新的传入项目,而不是每次都重建所有项目。

谢谢。

标签: flutterlistviewchat

解决方案


推荐阅读