import 'package:flutter/material.dart'; import 'KeepAliveDemo.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.green, //定义主题风格 primarySwatch ), home: KeepAliveDemo(), ); } }
Import 'Package: Flutter / material.dart' ; Import 'Package: flutter_app / MyHomePage.dart' ; class KeepAliveDemo the extends StatefulWidget { _KeepAliveDemoState createState () => _KeepAliveDemoState (); } / * with a dart keyword, meaning mixed meaning, that is to say you can add one or more classes of functionality into their class without having to inherit these classes, to avoid problems caused by multiple inheritance. We mainly SingleTickerProviderStateMixin tabController initialization, the need to use VSYNC, the vertical attribute, and then passed the this * / class _KeepAliveDemoState the extends State <KeepAliveDemo> with SingleTickerProviderStateMixin { tabController _controller; @override void initState(){ super.initState(); _controller = TabController(length:3, vsync: this); } //重写被释放的方法,只释放TabController @override void dispose(){ _controller.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar:AppBar( title:Text('Keep Alive Demo'), bottom:TabBar( controller: _controller, tabs:[ Tab(icon:Icon(Icons.directions_car)), Tab(icon:Icon(Icons.directions_transit)), Tab(icon:Icon(Icons.directions_bike)), ], ) ), body:TabBarView( controller: _controller, children: <Widget>[ MyHomePage(), MyHomePage(), MyHomePage() ], ) ); } }
effect: