首页 > 解决方案 > Flutter ListView没有滚动到最后一个添加的项目,而是最后一个之前的项目

问题描述

我在我的类中使用 aListView.builder将动态内容添加到列表中。我使用控制器来保持底部焦点controller.jumpTo(controller.position.maxScrollExtent);但我不知道为什么,它不关注最后一项,而是最后一项之前的一项......

这是我将项目添加到列表中的功能

if(numTeam == 1){
        _listAnnonceTeam1.add(nbrPoints);
        _controllerAnnonceT1.jumpTo(_controllerAnnonceT1.position.maxScrollExtent);
      }

这是我的 ListView.builder :

child: ListView.builder(
  controller: _controllerAnnonceT2,
  itemCount: _listAnnonceTeam2.length,
  itemBuilder: (BuildContext context, int index){
    return new Text(_listAnnonceTeam2[index].toString(), textAlign: TextAlign.center, style: 
    TextStyle(color: textColor),);
  },
),

你知道为什么吗?

标签: androidiosflutterdart

解决方案


您需要在后帧回调中进行跳转,以确保该项目将包含在列表中:

SchedulerBinding.instance.addPostFrameCallback((_) {
  _controllerAnnonceT1.jumpTo(_controllerAnnonceT1.position.maxScrollExtent);
});

推荐阅读