【问题标题】:Flutter - can't scroll to the bottom of listViewFlutter - 无法滚动到listView的底部
【发布时间】:2023-03-26 05:15:01
【问题描述】:

我有这样的代码:

    Column(children: [
          Container(
            alignment: Alignment.centerLeft,
            child: IconButton(
              color: theme.iconTheme.color,
              icon: const Icon(Icons.arrow_back),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
          ),
          Expanded(
            child: ListView(
              children: allOperations,
              controller: listViewController,
            ),
          ),
    ]);

我想在构建小部件后滚动到底部。

    WidgetsBinding.instance?.addPostFrameCallback((_) {
          listViewController.jumpTo(listViewController.position.maxScrollExtent);
    });

但它不起作用。始终保持大约 150 像素向下滚动。

有人遇到过这个问题吗?

更新 根据ListView 中的元素数量,它保持向下滚动的像素数。例如,如果我有 20 个元素,则底部将保持 50px 左右,如果 30 则大约 80px。

问题示例(假设小部件占据屏幕的所有高度,如果高于屏幕则可以滚动):
ListView 的全高:2000px
屏幕高度:600px
maxScrollExtent 应该是 1400 像素,但是是 1300 像素。

【问题讨论】:

    标签: flutter dart flutter-layout


    【解决方案1】:

    您是否尝试在列表下方留下一些空白?这通常是一个 SizedBox,它可以让您查看列表的其余部分,然后根据您选择的大小留下更多。

    【讨论】:

    • 我不希望列表下方有空白。我可以看到其余元素,但 maxScrollExtent 的值错误。检查我的更新。
    【解决方案2】:

    在滚动之前添加 Timer 可能会解决您的问题,我猜 listview 需要一些时间来添加新项目。

    Timer(Duration(milliseconds: 100), () {
        WidgetsBinding.instance?.addPostFrameCallback((_) {
      listViewController.jumpTo(listViewController.position.maxScrollExtent);
        });
    });
    

    您也可以尝试“animateTo”:

    Timer(Duration(milliseconds: 100), () {
        listViewController.animateTo(
          listViewController.position.maxScrollExtent,
          curve: Curves.easeOut,
          duration: const Duration(milliseconds: 750),
        );
    });
    

    【讨论】:

    • 它不起作用。
    • 如下添加 Padding 可能会有所帮助:Padding( padding: E​​dgeInsets.only( bottom: MediaQuery.of(context).viewInsets.bottom, ), child: ....),
    • 很遗憾没有帮助
    • 所以,尝试根据列表中的项目数添加一个动态高度的 SizedBox
    猜你喜欢
    • 2018-06-03
    • 1970-01-01
    • 2018-07-21
    • 2021-07-25
    • 2020-07-16
    • 2016-11-22
    • 2018-02-27
    • 2021-07-31
    • 1970-01-01
    相关资源
    最近更新 更多