Last active
February 5, 2024 07:16
-
-
Save demirdev/d2b17ba70aa2be26002352bc6601b6a6 to your computer and use it in GitHub Desktop.
nested_scroll_view_with_tabbarview_scroll_sync_issue.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'NestedScroll View with TabBarView', | |
home: PostsPage(), | |
debugShowCheckedModeBanner: false, | |
); | |
} | |
} | |
class PostsPage extends StatefulWidget { | |
@override | |
State<StatefulWidget> createState() => _PostsPageState(); | |
} | |
class _PostsPageState extends State<PostsPage> { | |
final List<String> _tabs = <String>[ | |
"Featured", | |
"Popular", | |
"Latest", | |
]; | |
@override | |
Widget build(BuildContext context) { | |
return SafeArea( | |
child: Scaffold( | |
body: DefaultTabController( | |
length: _tabs.length, | |
child: NestedScrollView( | |
body: TabBarView( | |
children: _tabs.map((String name) { | |
return TestTabItem(name); | |
}).toList(), | |
), | |
headerSliverBuilder: | |
(BuildContext context, bool innerBoxIsScrolled) { | |
return [ | |
SliverToBoxAdapter( | |
child: Container( | |
color: Colors.blue.withOpacity(0.2), | |
height: 100, | |
), | |
), | |
SliverAppBar( | |
pinned: true, | |
flexibleSpace: TabBar( | |
tabs: _tabs.map((String name) => Tab(text: name)).toList(), | |
), | |
), | |
]; | |
}, | |
), | |
), | |
), | |
); | |
} | |
} | |
class TestTabItem extends StatefulWidget { | |
const TestTabItem( | |
this.name, { | |
super.key, | |
}); | |
final String name; | |
@override | |
State<TestTabItem> createState() => _TestTabItemState(); | |
} | |
class _TestTabItemState extends State<TestTabItem> | |
with AutomaticKeepAliveClientMixin { | |
@override | |
Widget build(BuildContext context) { | |
return CustomScrollView( | |
slivers: <Widget>[ | |
SliverPadding( | |
padding: const EdgeInsets.all(8), | |
sliver: SliverList.list( | |
children: [ | |
for (int i = 0; i < 30; i++) | |
Container( | |
height: 100, | |
margin: const EdgeInsets.only(bottom: 8), | |
padding: const EdgeInsets.all(8), | |
color: Theme.of(context).secondaryHeaderColor, | |
child: Text( | |
'$i', | |
style: Theme.of(context).textTheme.headlineLarge, | |
), | |
) | |
], | |
), | |
), | |
], | |
); | |
} | |
@override | |
bool get wantKeepAlive => true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment