【问题标题】:Flutter Scrollcontroller maxScrollExtent doesn't goes till the end of listFlutter Scrollcontroller maxScrollExtent 直到列表末尾
【发布时间】:2019-12-12 16:04:40
【问题描述】:

它在列表中的最后一个元素之前停止。有没有办法走到最后一个元素的结束容器

        controller
          ..animateTo(
            controller.position.maxScrollExtent,
            curve: Curves.easeIn,
            duration: const Duration(milliseconds: 300),
          );
      });```




【问题讨论】:

  • 试过一个在 singlechildScrollView 上不起作用
  • 遇到同样的问题:如果从 addPostFrameCallback 执行滚动动画,有时 ListView 不会完全滚动到底部。以下内容有时不会将列表完全滚动到最后:SchedulerBinding.instance.addPostFrameCallback ((_) { _scrollController.animateTo (_scrollController.position.maxScrollExtent, duration: const Duration (milliseconds: chatTimeForScrollingAnimation), curve: Curves.easeOut); }); 但是如果我们在动画/跳转调用之前添加一些时间延迟(如@LIONELVSV 所建议的那样),问题就消失了。

标签: flutter flutter-layout


【解决方案1】:

不使用动画解决了问题

Timer(Duration(milliseconds: 500), () {
        controller.jumpTo(controller.position.maxScrollExtent);
      });

【讨论】:

  • 也适用于“animateTo”。时间延迟本身解决了这个问题,这似乎是 Flutter 中的一个错误。
【解决方案2】:

我认为这是滚动到底部的最佳功能

scrollToBottom() {
if (_chatController.hasClients) {
      Future.delayed(const Duration(milliseconds: 500)).then((value) {
        _chatController.animateTo(
          _chatController.position.maxScrollExtent,
          duration: const Duration(milliseconds: 200),
          curve: Curves.fastOutSlowIn,
        );
      });
    }
}

【讨论】:

    【解决方案3】:

    对我有用的是:

     void scrollAnimateToEnd(ScrollController controller) {
    Future.delayed(const Duration(milliseconds: 400)).then((_) {
      try {
        controller
            .animateTo(
          controller.position.maxScrollExtent,
          duration: const Duration(seconds: 1),
          curve: Curves.fastOutSlowIn,
        )
            .then((value) {
          controller.animateTo(
            controller.position.maxScrollExtent,
            duration: const Duration(milliseconds: 200),
            curve: Curves.easeInOut,
          );
        });
      } catch (e) {
        print('error on scroll $e');
      }
    });
    

    }

    【讨论】:

      【解决方案4】:

      我在使用 CustomeScrollView 小部件时遇到了同样的问题,我用这个解决了。

      CustomScrollView(
                        controller: Get.find<ChatController>(tag: 'chatDetail')
                            .scrollController
                            .value,
                        reverse: true,
                        keyboardDismissBehavior:
                            ScrollViewKeyboardDismissBehavior.onDrag,
                        physics: ClampingScrollPhysics(),
                        cacheExtent: 99999, // <-- here !!! ADD THIS CODE
                        slivers: [
                          ChatList(
                            isQuestion: isQuestion,
                            postCreatedAt: postCreatedAt,
                            title: title,
                          ),
                        ],
                      ),
      

      我的滚动功能和我们之前说的一样。

       void chatScrollUp() async {
      await scrollController.value.animateTo(
          scrollController.value.position.maxScrollExtent,
          duration: Duration(
              milliseconds:
                  (scrollController.value.position.maxScrollExtent / 2).round()),
          curve: Curves.easeOutCirc);}
      

      【讨论】:

        【解决方案5】:

        在我的情况下,问题是元素的大小不相等,并且在渲染之前大小不知道。因此,ListView 滚动到其最初估计的maxScrollExtent,但在滚动时元素被渲染并且新的maxScrollExtent 更大。我最初通过赋予它更大的价值来破解它:

        attachmentsScrollController.animateTo(
          attachmentsScrollController.position.maxScrollExtent * 2,
          duration: Duration(milliseconds: 300),
          curve: Curves.easeInOut);
        

        【讨论】:

          猜你喜欢
          • 2021-02-06
          • 2022-08-03
          • 1970-01-01
          • 1970-01-01
          • 2013-10-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多