简体   繁体   中英

Flutter ListView not scrolling to the last added item but the one before the last

I use a ListView.builder in my class to add dynamically contents to the list. I use a controller to keep the bottom focus with controller.jumpTo(controller.position.maxScrollExtent); But I dont know why, it doesn't focus the last item but the one before the last...

here is my fonction that add an item to the list

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

and here is my 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),);
  },
),

do you have any idea why?

You need to do the jump in a post-frame callback to make sure the item will be included in the list:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM