Last active
October 27, 2019 06:04
-
-
Save ysknsn/d90a84a180e32de5b0691de874c65d55 to your computer and use it in GitHub Desktop.
[Flutter]Listview in NestedScrollView
This file contains 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 { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.purple, | |
), | |
home: MyHomePage(title: 'Sliver App'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin { | |
TabController _tabController; | |
ScrollController _scrollController; | |
int _selectedIndex = 0; | |
@override | |
void initState() { | |
_tabController = TabController(vsync: this, length: 2); | |
_scrollController = ScrollController(); | |
_scrollController.addListener(_didScroll); | |
super.initState(); | |
} | |
@override | |
void dispose() { | |
_tabController.dispose(); | |
_scrollController.dispose(); | |
super.dispose(); | |
} | |
void _onItemTapped(int index) { | |
setState(() { | |
_selectedIndex = index; | |
_tabController.index = index; | |
}); | |
} | |
void _didScroll() { | |
print("didScroll"); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: DefaultTabController( | |
length: _tabController.length, | |
child: NestedScrollView( | |
controller: _scrollController, | |
key: PageStorageKey(widget.title), | |
headerSliverBuilder: (context, innerBoxScrolled) => [ | |
SliverAppBar( | |
title: Text(widget.title), | |
floating: true, | |
pinned: true, | |
snap: true, | |
bottom: const PreferredSize( | |
child: SizedBox(), preferredSize: Size.fromHeight(0.0)), | |
), | |
], | |
body: TabBarView( | |
physics: NeverScrollableScrollPhysics(), | |
controller: _tabController, | |
children: <Widget>[ | |
LeftTab(), | |
RightTab(), | |
]), | |
), | |
), | |
bottomNavigationBar: BottomNavigationBar( | |
items: const <BottomNavigationBarItem>[ | |
BottomNavigationBarItem( | |
icon: Icon(Icons.star), | |
title: Text('Left'), | |
), | |
BottomNavigationBarItem( | |
icon: Icon(Icons.star_border), | |
title: Text('Right'), | |
), | |
], | |
currentIndex: _selectedIndex, | |
selectedItemColor: Colors.amber[800], | |
onTap: _onItemTapped, | |
), | |
); | |
} | |
} | |
class LeftTab extends StatefulWidget { | |
LeftTab({Key key}) : super(key: key); | |
@override | |
_LeftTabState createState() => _LeftTabState(); | |
} | |
class _LeftTabState extends State<LeftTab> { | |
@override | |
Widget build(BuildContext context) { | |
return ListView.builder( | |
key: PageStorageKey("Left"), | |
itemCount: 200, | |
itemBuilder: (BuildContext context, int index) { | |
return Align( | |
alignment: Alignment.center, child: Text("Left: $index")); | |
}); | |
} | |
} | |
class RightTab extends StatefulWidget { | |
@override | |
_RightTabState createState() => _RightTabState(); | |
} | |
class _RightTabState extends State<RightTab> { | |
@override | |
Widget build(BuildContext context) { | |
return ListView.builder( | |
key: PageStorageKey("Right"), | |
itemCount: 200, | |
itemBuilder: (BuildContext context, int index) { | |
return Align( | |
alignment: Alignment.center, child: Text("Right: $index")); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment