【问题标题】:Flutter listview always scrolling to bottom when reverse is true当反向为真时,颤振列表视图总是滚动到底部
【发布时间】:2020-02-19 06:03:28
【问题描述】:

为什么我使用反向是正确的,它是为了实现从添加时间排序的实时数据库中获取消息。完成此操作后,顶部的新消息和旧消息位于列表视图中。一切正常,这是我想要的条件。但是列表视图总是滚动到底部而不是位于顶部。如果用户进入页面,他将首先看到旧帖子,而不是新帖子。这是我的ocde

return ListView.builder(
    controller: _scrollController,
    shrinkWrap: true,
    //physics: const AlwaysScrollableScrollPhysics(),
    itemCount: nots.length,
    reverse: true,
    itemBuilder: (context, index) {
      return Container(
        color: Colors.amber[50],
        child: Padding(
          padding: const EdgeInsets.all(0.0),
          child: Row(
            children: <Widget>[
              column(nots[index].title,nots[index].date,nots[index].body,nots[index].photourl),
              //column,
            ],
          ),
        ),
      );
    },
  );

如果我删除反向:真。然后旧帖子位于顶部,新消息位于底部。但在这种情况下,列表视图不会滚动到底部。简而言之,当我同时使用这两种情况时。用户只能先看到旧帖子。但我想先看看他们的新帖子。

【问题讨论】:

  • 为什么不以相反的顺序遍历消息,并保持列表的正常顺序?例如,在itemBuilder: (context, index) { 中以int reverseIndex = nots.length - 1 - index; 开头,然后使用reverseIndex 而不是index
  • 谢谢 问题已解决。这是公认的答案

标签: android flutter flutter-listview


【解决方案1】:

ListView reverse: true 显示从底部提供给它的列表。如果您希望 ListView 从顶部开始,则需要对 List 进行排序以显示在 ListView 上。

【讨论】:

    猜你喜欢
    • 2020-04-05
    • 2020-11-23
    • 2020-08-09
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 2020-01-18
    • 2020-11-09
    相关资源
    最近更新 更多