Home > Software engineering >  Flutter - can't scroll to the bottom of listView
Flutter - can't scroll to the bottom of listView

Time:11-18

I have code like this:

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,
        ),
      ),
]);

and I want to scroll to the bottom after widget is built.

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

but it doesn't work. Always remain about 150px to scroll down.

Anyone had this problem?

CodePudding user response:

Have you tried to leave some white space below your list? This is typically a SizedBox and it would allow you to see the rest of your list and then leave some more depending on the size you choose.

CodePudding user response:

adding Timer before scroll may solve your problem, I guess listview needs some time to add a new item.

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

you can also try "animateTo" instead:

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