我的聊天工具和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 = "";
});
})
],
)
]),
));
}
}
当用户重定向到此屏幕时,我的屏幕看起来是这样的
请帮助解决此错误.