Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:flutter/widgets.dart';
- void main() => runApp(MyApp());
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'Flutter Demo',
- theme: ThemeData(
- primarySwatch: Colors.blue,
- ),
- home: MyHomePage(title: 'Flutter Demo Home Page'),
- );
- }
- }
- class MyHomePage extends StatefulWidget {
- MyHomePage({Key key, this.title}) : super(key: key);
- final String title;
- @override
- _MyHomePageState createState() => _MyHomePageState();
- }
- class _MyHomePageState extends State<MyHomePage> {
- var _currentTabIndex = 0;
- var _tabChildren = [
- Page1(),
- Page2(),
- Page3(),
- Page4(),
- ];
- @override
- Widget build(BuildContext context) {
- print('MyHomePage: build');
- return DefaultTabController(
- length: _tabChildren.length,
- child: Scaffold(
- appBar: AppBar(
- title: Text(widget.title),
- ),
- body: TabBarView(
- physics: NeverScrollableScrollPhysics(),
- children: _tabChildren,
- ),
- bottomNavigationBar: CustomBottomNavigationBar(),
- ),
- );
- }
- Widget bottomNavigationBar() {
- return Builder(
- builder: (context) => TabBar(
- controller: DefaultTabController.of(context),
- tabs: <Widget>[
- Tab(icon: Icon(Icons.home), text: 'Home'),
- Tab(icon: Icon(Icons.keyboard), text: 'Keyboard'),
- Tab(icon: Icon(Icons.save), text: 'Save'),
- Tab(icon: Icon(Icons.settings), text: 'Settings'),
- ],
- labelColor: Colors.blue,
- unselectedLabelColor: Colors.black,
- indicatorColor: Colors.transparent,
- indicatorSize: TabBarIndicatorSize.tab,
- ),
- );
- }
- }
- class CustomBottomNavigationBar extends StatefulWidget {
- @override
- _CustomBottomNavigationBarState createState() =>
- _CustomBottomNavigationBarState();
- }
- class _CustomBottomNavigationBarState extends State<CustomBottomNavigationBar> {
- var _currentTabIndex = 0;
- @override
- Widget build(BuildContext context) {
- return Builder(
- builder: (context) => BottomNavigationBar(
- currentIndex: _currentTabIndex,
- selectedItemColor: Colors.blue,
- unselectedItemColor: Colors.black,
- items: [
- BottomNavigationBarItem(
- icon: Icon(Icons.home), title: Text('Home')),
- BottomNavigationBarItem(
- icon: Icon(Icons.keyboard), title: Text('Keyboard')),
- BottomNavigationBarItem(
- icon: Icon(Icons.save), title: Text('Save')),
- BottomNavigationBarItem(
- icon: Icon(Icons.settings), title: Text('Settings')),
- ],
- onTap: (tabIndex) {
- setState(() {
- _currentTabIndex = tabIndex;
- DefaultTabController.of(context).animateTo(tabIndex);
- });
- },
- ),
- );
- }
- }
- class Page1 extends StatefulWidget {
- @override
- _Page1State createState() => _Page1State();
- }
- class _Page1State extends State<Page1>
- with AutomaticKeepAliveClientMixin<Page1> {
- @override
- Widget build(BuildContext context) {
- super.build(context);
- print('Page1: build');
- return SingleChildScrollView(
- child: Center(
- child: Column(
- children: <Widget>[Text("Page 1"), TextField()],
- ),
- ),
- );
- }
- @override
- bool get wantKeepAlive => true;
- }
- class Page2 extends StatefulWidget {
- @override
- _Page2State createState() => _Page2State();
- }
- class _Page2State extends State<Page2>
- with AutomaticKeepAliveClientMixin<Page2> {
- @override
- Widget build(BuildContext context) {
- super.build(context);
- print('Page2: build');
- return Center(
- child: Column(
- children: <Widget>[Text("Page 2"), TextField()],
- ),
- );
- }
- @override
- bool get wantKeepAlive => true;
- }
- class Page3 extends StatefulWidget {
- @override
- _Page3State createState() => _Page3State();
- }
- class _Page3State extends State<Page3>
- with AutomaticKeepAliveClientMixin<Page3> {
- @override
- Widget build(BuildContext context) {
- super.build(context);
- print('Page3: build');
- return Center(
- child: Column(
- children: <Widget>[Text("Page 3"), TextField()],
- ),
- );
- }
- @override
- bool get wantKeepAlive => true;
- }
- class Page4 extends StatefulWidget {
- @override
- _Page4State createState() => _Page4State();
- }
- class _Page4State extends State<Page4>
- with AutomaticKeepAliveClientMixin<Page4> {
- @override
- Widget build(BuildContext context) {
- super.build(context);
- print('Page4: build');
- return Center(
- child: Column(
- children: <Widget>[Text("Page 4"), TextField()],
- ),
- );
- }
- @override
- bool get wantKeepAlive => true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement