0

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?

Rahul Variya
  • 1,257
  • 1
  • 6
  • 15

2 Answers2

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

Luca Antonelli
  • 349
  • 1
  • 2
  • 21
1

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

Akash g krishnan
  • 469
  • 5
  • 16