如何构建列表视图,其中包含根据需要重建的列表和常量部分,以利用列表视图中的索引
我正在评论页面上工作,我需要构建下面显示的屏幕,第一部分是列表视图分隔,第二部分是文本字段,但问题是我不能使用列表视图中的 index 因为textfiled 位于 listview.separated 树之外。
所以我想如果我能够以某种方式让 textfiled 进入 listview.separated 我将能够使用 index,但是 textfiled 不断重建,这不是我想要的,无论如何都可以阻止它在 listview 内重建。分开了。
postId 表示我将在哪个帖子上发表评论评论
body: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Expanded(
child: ListView.separated(
itemBuilder: (context, index) =>
buildCommentSection(context, index),
separatorBuilder: (context, index) => const Padding(
padding: EdgeInsets.only(
top: 10, bottom: 10, right: 20, left: 20),
child: Divider(
color: Colors.grey,
thickness: 1,
height: 1,
),
),
itemCount: 1,
),
),
const Divider(
color: Colors.grey,
thickness: 1,
height: 1,
),
Padding(
padding: const EdgeInsets.all(10.0),
child: TextFormField(
minLines: 1,
maxLines: 4,
controller: commentController,
keyboardType: TextInputType.text,
decoration: InputDecoration(
hintText: 'Write a comment... ',
contentPadding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 5, bottom: 5),
hintStyle: TextStyle(color: Colors.grey[400]),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
),
Padding(
padding: const EdgeInsets.only(right: 10, bottom: 10),
child: InkWell(
onTap: () {
//here to use the postComment function and I need the index here
SocialCubit.get(context).postComment(
SocialCubit.get(context).postsId[index],
comment: commentController.text,
);
},
child: const Icon(
Icons.send,
),
),
),
Container buildCommentSection(context, index) {
return Container(
// decoration: const BoxDecoration(
// image: DecorationImage(
// image: NetworkImage(
// 'https://i.ibb.co/zGZyH0w/asd.jpg',
// ),
// ),
// ),
child: Align(
alignment: Alignment.topLeft,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(
left: 65.0,
),
child: Text(
'Ahmad M. Hassanien',
style: Theme.of(context).textTheme.bodyText1?.copyWith(
fontSize: 15, color: Colors.black, height: 1.3),
),
),
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Padding(
padding:
EdgeInsets.only(left: 10.0, top: 0, bottom: 10.0),
child: CircleAvatar(
radius: 20,
backgroundImage: NetworkImage(
'https://scontent.fcai21-2.fna.fbcdn.net/v/t1.6435-9/36137473_1672721836179430_3456862169725927424_n.jpg?_nc_cat=104&ccb=1-5&_nc_sid=09cbfe&_nc_ohc=9tYeesdPuIQAX_nct-b&tn=vOLAikUZ_sGc5L8h&_nc_ht=scontent.fcai21-2.fna&oh=00_AT8Oo-xDechHRInJ_IBubDRLxcFftLHFysjrw5LF-iGpKg&oe=625A1564'),
)),
Expanded(
child: Padding(
padding: const EdgeInsets.only(
left: 10, right: 15, top: 0, bottom: 10),
child: TextFormField(
readOnly: true,
minLines: 1,
maxLines: 50,
controller: commentController,
keyboardType: TextInputType.text,
decoration: InputDecoration(
hintText: commentController.text,
contentPadding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 5, bottom: 5),
hintStyle: const TextStyle(color: Colors.black),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
),
),
],
),
],
),
),
);
}
Padding(
padding: const EdgeInsets.only(right: 10, bottom: 10),
child: InkWell(
onTap: () {
//here to use the postComment function and I need the index here
SocialCubit.get(context).postComment(
SocialCubit.get(context).postsId[index],
comment: commentController.text,
);
},
child: const Icon(
Icons.send,
),
),
),
I'm working on comment page which I need to build the screen shown blow the first part is list veiw separated and second part is text field, but the problem is I can not use the index inside listview because the textfiled is outside the listview.separated tree.
So I figured if I was able to somehow get textfiled to be inside the listview.separated
I will be able to use the index, but the textfiled keeps rebuilding and that's not what I want, is there anyway to stop it from rebuilding inside the listview.separated .
postId indicates which post I will write the comment on
body: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Expanded(
child: ListView.separated(
itemBuilder: (context, index) =>
buildCommentSection(context, index),
separatorBuilder: (context, index) => const Padding(
padding: EdgeInsets.only(
top: 10, bottom: 10, right: 20, left: 20),
child: Divider(
color: Colors.grey,
thickness: 1,
height: 1,
),
),
itemCount: 1,
),
),
const Divider(
color: Colors.grey,
thickness: 1,
height: 1,
),
Padding(
padding: const EdgeInsets.all(10.0),
child: TextFormField(
minLines: 1,
maxLines: 4,
controller: commentController,
keyboardType: TextInputType.text,
decoration: InputDecoration(
hintText: 'Write a comment... ',
contentPadding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 5, bottom: 5),
hintStyle: TextStyle(color: Colors.grey[400]),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
),
Padding(
padding: const EdgeInsets.only(right: 10, bottom: 10),
child: InkWell(
onTap: () {
//here to use the postComment function and I need the index here
SocialCubit.get(context).postComment(
SocialCubit.get(context).postsId[index],
comment: commentController.text,
);
},
child: const Icon(
Icons.send,
),
),
),
Container buildCommentSection(context, index) {
return Container(
// decoration: const BoxDecoration(
// image: DecorationImage(
// image: NetworkImage(
// 'https://i.ibb.co/zGZyH0w/asd.jpg',
// ),
// ),
// ),
child: Align(
alignment: Alignment.topLeft,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(
left: 65.0,
),
child: Text(
'Ahmad M. Hassanien',
style: Theme.of(context).textTheme.bodyText1?.copyWith(
fontSize: 15, color: Colors.black, height: 1.3),
),
),
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Padding(
padding:
EdgeInsets.only(left: 10.0, top: 0, bottom: 10.0),
child: CircleAvatar(
radius: 20,
backgroundImage: NetworkImage(
'https://scontent.fcai21-2.fna.fbcdn.net/v/t1.6435-9/36137473_1672721836179430_3456862169725927424_n.jpg?_nc_cat=104&ccb=1-5&_nc_sid=09cbfe&_nc_ohc=9tYeesdPuIQAX_nct-b&tn=vOLAikUZ_sGc5L8h&_nc_ht=scontent.fcai21-2.fna&oh=00_AT8Oo-xDechHRInJ_IBubDRLxcFftLHFysjrw5LF-iGpKg&oe=625A1564'),
)),
Expanded(
child: Padding(
padding: const EdgeInsets.only(
left: 10, right: 15, top: 0, bottom: 10),
child: TextFormField(
readOnly: true,
minLines: 1,
maxLines: 50,
controller: commentController,
keyboardType: TextInputType.text,
decoration: InputDecoration(
hintText: commentController.text,
contentPadding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 5, bottom: 5),
hintStyle: const TextStyle(color: Colors.black),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
),
),
],
),
],
),
),
);
}
Padding(
padding: const EdgeInsets.only(right: 10, bottom: 10),
child: InkWell(
onTap: () {
//here to use the postComment function and I need the index here
SocialCubit.get(context).postComment(
SocialCubit.get(context).postsId[index],
comment: commentController.text,
);
},
child: const Icon(
Icons.send,
),
),
),
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论