Reputation: 99
I have a SliverAppBar, which scrolls up to shrink and scrolls down to expand.
I want to expand SliverAppBar when I switch BottomNavigationBar.
Under the current situation, the state of SliverAppBar before switching is kept.
This is my code. https://gist.github.com/ysknsn/d90a84a180e32de5b0691de874c65d55
Any advices are helpful. Thanks.
Upvotes: 2
Views: 3170
Reputation: 138
Add a scroll controller to your 'Custom Scroll View'
ScrollController _scrollController;
Widget build(BuildContext context) {
_scrollController = ScrollController();
return Scaffold(
body: CustomScrollView( shrinkWrap: true, controller: _scrollController,
physics: BouncingScrollPhysics(),
slivers: <Widget>[
SliverAppBar(
pinned: false,
:
:
:
:
:
:
:
:
:
onPressed: () {// Scroll to top when on click => Expand
_scrollController.animateTo(
_scrollController.position.minScrollExtent,
duration:Duration(milliseconds:1300),
curve: Curves.decelerate,);
},
onPressed: () {// Scroll to bottom when on click => Collapse
scrollController.animateTo(
scrollController.position.maxScrollExtent,
duration: Duration(milliseconds: 1300),
curve: Curves.decelerate,
);
},
Upvotes: 2
Reputation: 1044
Just do the same you did to the TabController
...
Put it in a local variable:
TabController _tabController;
ScrollController _scrollController;
int _selectedIndex = 0;
@override
void initState() {
_tabController = TabController(vsync: this, length: 2);
_scrollController = ScrollController(keepScrollOffset: true);
super.initState();
}
Then use it in your NestedScrollView`
@override
Widget build(BuildContext context) {
return Scaffold(
body: DefaultTabController(
length: _tabController.length,
child: NestedScrollView(
controller: _scrollController,
key: PageStorageKey(widget.title),
...
Then update the position when you click:
void _onItemTapped(int index) {
_scrollController.jumpTo(0);
setState(() {
_selectedIndex = index;
_tabController.index = index;
});
}
Upvotes: 2