skip to Main Content

I have four tabs in bottomnavigation, i want to build functionally like Instagram when I tap on the home screen it brings the top of the home page For ex: I m on the home screen I scroll on the home screen then go to the second tab again come to the home screen by a tap on the bottom navigation screen is middle of the scroll but I tap again on home tap icon it will automatically scroll to top. I m using indexStack for bottomnavigation Any one has any idea about it?

2

Answers


  1. You can use a ScrollController.

    In the ListView or SingleChildScrollView (or whatever other scrollable widget) add controller.

    final ScrollController scrollController = ScrollController();
    
    ...
    
    SingleChildScrollView(
     controller: scrollController,
     child: ...
    )
    

    Then if you want to get back to the top of the scrollview you can call

    scrollController.animateTo(
     scrollController.position.minScrollExtent,
     duration: const Duration(milliseconds: 300),
     curve: Curves.easeOut,
    )
    

    To get this functionality only on the second tap of the bottomappbar icon you can check if the index is equal to the current page index.

    You can find a working example in the BottomNavigationBar api page: https://api.flutter.dev/flutter/material/BottomNavigationBar-class.html#material.BottomNavigationBar.3

    Login or Signup to reply.
  2. The best and most easiest way to do this without any scroll controller or anything would be to use the AutomaticKeepAliveClientMixin

    AutomaticKeepAliveClientMixin Docs

    Another question on stack overflow:
    SO link

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search