我有一个评论和回复系统。我想让嵌套的列表视图在添加回复时一直滚动到底部,现在,它会将我带到倒数第二个回复的底部,但不会带到我添加的最后一个回复What I want
What I have, it scrolls to second last reply, not to last reply
void main() => runApp(MaterialApp(
home: CommentScreen(),
));
class Comment {
int id;
String text;
List<String> replies;
Comment(this.id,this.text, {this.replies = const []});
}
class CommentScreen extends StatefulWidget {
@override
_CommentScreenState createState() => _CommentScreenState();
}
class _CommentScreenState extends State<CommentScreen> {
List<Comment> comments = [
Comment(1,"First comment", replies: ["Reply 1", "Reply 2"]),
Comment(2,"Second comment",replies: ["Reply 1", "Reply 2"]),
Comment(3,"Second comment",replies: ["Reply 1", "Reply 2"]),
Comment(4,"Second comment",replies: ["Reply 1", "Reply 2"]),
Comment(5,"Second comment",replies: ["Reply 1", "Reply 2"]),
Comment(6,"Second comment",replies: ["Reply 1", "Reply 2"]),
Comment(7,"Second comment",replies: ["Reply 1", "Reply 2"]),
Comment(8,"Second comment",replies: ["Reply 1", "Reply 2"]),
Comment(9,"Second comment",replies: ["Reply 1", "Reply 2"]),
Comment(10,"Second comment", replies: ["Reply 1", "Reply 2"]),
];
ScrollController _scrollController = ScrollController();
void addReply(int commentIndex, String reply,int id) {
setState(() {
comments[commentIndex].replies.add(reply);
});
Scrollable.ensureVisible(GlobalObjectKey(id).currentContext!,
duration: Duration(milliseconds: 1), // duration for scrolling time
alignment: 1,
curve: Curves.easeInOutCubic,);
// Scroll to the last reply
/* _scrollController.animateTo(
_scrollController.position.maxScrollExtent,
duration: const Duration(milliseconds: 300),
curve: Curves.easeInOut,
);*/
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Comments"),
),
body: ListView.builder(
itemCount: comments.length,
physics: AlwaysScrollableScrollPhysics(),
itemBuilder: (BuildContext context, int index) {
Comment comment = comments[index];
return Column(
children: [
ListTile(
title: Text(comment.text),
trailing: ElevatedButton(
onPressed: () {
addReply(index, """Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.""",comment.id);
},
child: Text("Reply"),
),
),
// Display replies inside ListView
ListView.builder(
key: GlobalObjectKey(comment.id),
shrinkWrap: true,
physics: ClampingScrollPhysics(),
itemCount: comment.replies.length,
itemBuilder: (BuildContext context, int replyIndex) {
String reply = comment.replies[replyIndex];
return ListTile(
title: Text(reply),
);
},
),
],
);
},
),
);
}
}
1条答案
按热度按时间06odsfpq1#
举个例子。但是,有这么多的方法来使用切片。我认为阅读官方文件是了解切片的最好方法。