Home > Blockchain >  Flutter ScrollController in ListView
Flutter ScrollController in ListView

Time:01-11

I made a chat app in flutter . i want when i open the chat page automatic scroll to the last message like real whats app or messenger . i want when i open the page i be in the last message . i dont want to use reverse in list View

i tried to do this in innit state but it didn't work when i open the chat page for first time but after that it work fines

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

CodePudding user response:

Using jumpTo will instancly move to bottom after 1st frame is build. You can include a FutureDelay(..) but .animateTo looks better.

  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      await scrollController.animateTo(
          scrollController.position.maxScrollExtent,
          duration: Duration(seconds: 1),
          curve: Curves.bounceIn);
    });
  }

Find more about /ScrollController/animateTo

  • Related