我的聊天工具和WhatsApp一样.我正在try 做这个功能,当用户想要回复特定的消息时,当点击回复的消息时,它应该跳到该特定的消息.要做到这一点,我使用的是indexed_list_view个套餐.

The problem is that it shows this error

RangeError (index): Invalid value: Not in inclusive range 0..3: 4 

但是当我使用ListView builder时,不会出现这个错误.

here is my code:

class ChatRoom extends StatefulWidget {
  const ChatRoom({Key? key}) : super(key: key);

  @override
  State<ChatRoom> createState() => _ChatRoomState();
}

class _ChatRoomState extends State<ChatRoom> {
  List<ChatRoomModel> chatRoom = [
    ChatRoomModel(
        replyMsgIndex: 0,
        msg: "Hello",
        time: "09:00 PM",
        replyCount: 0,
        replyMsg: "",
        userId: 1),
    ChatRoomModel(
        replyMsgIndex: 0,
        msg: "Helloo",
        time: "12:00 PM",
        replyCount: 0,
        replyMsg: "",
        userId: 1),
    ChatRoomModel(
        replyMsgIndex: 0,
        msg: "Hellooo",
        time: "09:05 PM",
        replyCount: 0,
        replyMsg: "",
        userId: 1),
    ChatRoomModel(
        replyMsgIndex: 0,
        msg: "Helloooo",
        time: "10:00 PM",
        replyCount: 0,
        replyMsg: "",
        userId: 1),
  ];

 bool leftSelected = false;
  bool rightSelected = false;
  String replymsg = "";
  int? getMsgIndex;
  int? jumpIndex;
  int? changeColorIndex;
  TextEditingController newReply = TextEditingController();
IndexedScrollController controller = IndexedScrollController(initialIndex: 0);

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
      appBar: PreferredSize(
        preferredSize: const Size.fromHeight(60.0),
        child: CustomApp(
          name: name,
          imagePath: imagePath,
        ),
      ),
      body: Column(children: [
//here i am using the package
        Expanded(
          child: IndexedListView.builder(
              maxItemCount: chatRoom.length,  //i passed the lenght of list too
              controller: controller,
              itemBuilder: ((context, index) {
               

                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Container(
                    decoration: BoxDecoration(
                        color: jumpIndex == index
                            ? Colors.blue
                            : Colors.transparent,
                        borderRadius: BorderRadius.circular(15.0)),
                    child: SwipeTo(
                      onRightSwipe: (() {
                        setState(() {
                          rightSelected = false;
                          leftSelected = true;
                          replymsg = chatRoom[index].msg;
                          getMsgIndex = index;
                          jumpIndex = index;
                          print("getMsgIndex  $getMsgIndex");
                        });
                      }),
                      child: Column(
//error points here                       
 crossAxisAlignment: chatRoom[index].userId == 1
                            ? CrossAxisAlignment.start
                            : CrossAxisAlignment.end,
                        children: [
                         
                          chatRoom[index].replyCount == 1
                              ? InkWell(
                                  onTap: () {
                                    controller.jumpToIndex(jumpIndex!);
                                  },
                                  child: Container(
                                    padding: const EdgeInsets.all(5),
                                    decoration: BoxDecoration(
                                      color: Colors.grey.shade100,
                                      border: const Border(
                                        left: BorderSide(
                                            color: Colors.purple, width: 3),
                                      ),
                                    ),
                                    child: Column(
                                      crossAxisAlignment:CrossAxisAlignment.end,
                                      children: [
                                        Text(name,style: const TextStyle(
                                                color: Colors.purple,
                                                fontSize: 15,
                                                fontWeight: FontWeight.w500)),
                                        const SizedBox(height: 3),
                                        Text(chatRoom[index].replyMsg,
                                            style: const TextStyle(
                                              color: Colors.black54,
                                              fontSize: 12,
                                            )),
                                      ],
                                    ),
                                  ),
                                )
                              : Text(
                                  chatRoom[index].replyCount > 0 &&
                                          chatRoom[index].replyMsg != ""
                                      ? chatRoom[index].replyMsg
                                      : "",
                                  style: const TextStyle(
                                      fontSize: 10.0,
                                      fontWeight: FontWeight.bold)),
                          Text(chatRoom[index].msg,
                              style: const TextStyle(fontSize: 15.0)),
                          Text(
                            "       " + chatRoom[index].time,
                            style: const TextStyle(fontSize: 10.0),
                          ),
                        ],
                      ),
                    ),
                  ),
                );
              })),
        ),
        Column(
          children: [
            leftSelected
                ? _replyWidget(replymsg)
                : AnimatedContainer(
                    duration: const Duration(seconds: 1),
                  ),
            BottomTextField(
                newReply: newReply,
                sendMessageFunc: () {
//I'm adding new messages to list
                  setState(() {
                    leftSelected == true
                        ? chatRoom.add(
                            ChatRoomModel(
                                replyMsgIndex: jumpIndex,
                                userId: 2,
                                msg: newReply.text,
                                time: "09:00 PM",
                                replyCount: 1,
                                replyMsg: replymsg),
                          )
                        : chatRoom.add(ChatRoomModel(
                            replyMsgIndex: jumpIndex,
                            userId: 2,
                            time: "09:00 PM",
                            msg: newReply.text,
                            replyCount: 0,
                            replyMsg: ""));
                    leftSelected = false;
                    newReply.clear();
                    replymsg = "";
                  });
                })
          ],
        )
      ]),
    ));
  }
}




当用户重定向到此屏幕时,我的屏幕看起来是这样的

enter image description here

请帮助解决此错误.

推荐答案

您使用的是indexed_list_view包中的IndexedListView.第一页上的文档如下所示:

限制:对于正索引和负索引,该列表始终是无限的.换句话说,它可以无限滚动到顶部和底部.

这意味着您不能指定项目的确切数量,因为IndexedListView适用于Infinity个列表

您必须手动选中index,如果它在您的集合范围内,则返回表示该项的小部件或null以确定您没有任何要返回的内容

简而言之,你的itemBuilder人应该看起来:

.. your code from above...
//error points here
child: index < 0 || index > chatRoom.length - 1 
    ? null
    : Column(crossAxisAlignment: chatRoom[index].userId == 1,
    ...

此外,不要忘记列表将以任何方式滚动,即使您没有要显示的项目,因为:

您可以通过为其提供minItemCount和MaxItemCount来定义索引界限,但这不会阻止列表无限滚动.当显示超出索引边界的项时,或者当您的itemBuilder返回空时,它将要求emptyItemBuilder创建一个要显示的"空"项.默认情况下,这将返回空容器.

Flutter相关问答推荐

ListTile未正确显示在flutter中

如何让一个名为ONTAP的回调函数、另一个名为onTapDown的回调函数和另一个名为onTapUp的回调函数不重叠?

blocTest错误:没有从`when()`中调用方法存根

来自FirebaseAuth的错误DisplayName和邮箱在Flutter应用程序中给出错误

有没有方法可以从当前时间轻松计算TimeStamp?

如何画三角形的圆角?

如何在flutter中使用youtube_explod_start加载下一页

如何使listview.builder在筛选列表后实时更新

按一下按钮即可更新 fl_chart

在 Flutter 中,SpinEdit 叫什么?

无法使用 bloc 更新 ListView.builder 的特定时间

这是使用导航功能处理从一页到另一页的移动的最佳方式

在 CircleAvatar 中放置芯片

Flutter:制作自定义小部件的最佳做法是什么?

如何让多个图标转到不同的网址

扫描flutter生成的二维码后如何弹出文本字段值

构建失败 - 无法解析 io.grpc:grpc-core:[1.28.0]. (是的,我已经升级到 mavenCentral())

阴影效果只出现在一张卡片的左边 在Flutter 中?

在 Flutter 应用程序中全局使用 assets 文件夹

设计响应式卡片的最佳方法,以避免像素溢出错误或 _AssertionError