Created
April 29, 2019 09:15
-
-
Save CaiJingLong/205482e9ae3556e0b833681bde1e7d2a to your computer and use it in GitHub Desktop.
AutomaticKeepAliveClientMixin for tabbar example
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 { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
// This is the theme of your application. | |
// | |
// Try running your application with "flutter run". You'll see the | |
// application has a blue toolbar. Then, without quitting the app, try | |
// changing the primarySwatch below to Colors.green and then invoke | |
// "hot reload" (press "r" in the console where you ran "flutter run", | |
// or simply save your changes to "hot reload" in a Flutter IDE). | |
// Notice that the counter didn't reset back to zero; the application | |
// is not restarted. | |
primarySwatch: Colors.blue, | |
), | |
home: HomePage(), | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
@override | |
_HomePageState createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> | |
with SingleTickerProviderStateMixin { | |
TabController controller; | |
@override | |
void initState() { | |
super.initState(); | |
controller = TabController(length: 4, vsync: this); | |
} | |
@override | |
void dispose() { | |
controller?.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return DefaultTabController( | |
initialIndex: 0, | |
child: Scaffold( | |
appBar: AppBar( | |
bottom: TabBar( | |
controller: controller, | |
tabs: <Widget>[ | |
Tab(text: "1"), | |
Tab(text: "2"), | |
Tab(text: "3"), | |
Tab(text: "4"), | |
], | |
), | |
), | |
body: TabBarView( | |
controller: controller, | |
children: <Widget>[ | |
SubPage(), | |
SubPage(), | |
SubPage(), | |
SubPage(), | |
], | |
), | |
), | |
length: 4, | |
); | |
} | |
} | |
class SubPage extends StatefulWidget { | |
@override | |
_SubPageState createState() => _SubPageState(); | |
} | |
class _SubPageState extends State<SubPage> with AutomaticKeepAliveClientMixin { | |
@override | |
Widget build(BuildContext context) { | |
super.build(context); | |
return ListViewWidget(); | |
} | |
@override | |
bool get wantKeepAlive => true; | |
} | |
class ListViewWidget extends StatefulWidget { | |
@override | |
_ListViewWidgetState createState() => _ListViewWidgetState(); | |
} | |
class _ListViewWidgetState extends State<ListViewWidget> { | |
@override | |
Widget build(BuildContext context) { | |
return ListView.builder( | |
itemBuilder: _buildItem, | |
itemCount: 50, | |
); | |
} | |
Widget _buildItem(BuildContext context, int index) { | |
return ListTile( | |
title: Text(index.toString()), | |
); | |
} | |
} |
thank you are great . you saved my time
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wow thats very simple and neat thank you