flutter 在fluttter中使用嵌套Litview进行注解和回复

rbl8hiat  于 2023-06-24  发布在  Flutter
关注(0)|答案(1)|浏览(149)

我有一个评论和回复系统。我想让嵌套的列表视图在添加回复时一直滚动到底部,现在,它会将我带到倒数第二个回复的底部,但不会带到我添加的最后一个回复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),
                  );
                },
              ),
            ],
          );
        },
      ),
    );
  }
}
06odsfpq

06odsfpq1#

import 'package:flutter/material.dart';

class TestScrollView extends StatefulWidget {
  const TestScrollView({super.key});

  @override
  State<TestScrollView> createState() => _TestScrollViewState();
}

class _TestScrollViewState extends State<TestScrollView> {
  late final ScrollController scrollController;

  @override
  void initState() {
    super.initState();
    scrollController = ScrollController();
  }

  @override
  void dispose() {
    scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('TEST')),
      body: CustomScrollView(
        controller: scrollController,
        slivers: [
          SliverAppBar(
            // See the official document for understanding paramters.
            pinned: true,
            floating: false,
            collapsedHeight: 300.0, // Min height
            expandedHeight: 500.0, // Max height
            flexibleSpace: ListView.builder(
              itemBuilder: (context, index) => Text('Upper Item $index'),
            ),
          ),
          SliverList(
            delegate: SliverChildBuilderDelegate(
              childCount: 1000,
              (context, index) => Text('item $index'),
            ),
          ),
        ],
      ),
    );
  }
}

举个例子。但是,有这么多的方法来使用切片。我认为阅读官方文件是了解切片的最好方法。

相关问题