主布局 继承 SingleTickerProviderStateMixin
import 'package:flutter/material.dart';
import 'package:fl_diletai/utils/tire_export.dart';
import 'print_page.dart';
import 'send_page.dart';
class PrintAndSendGoodPage extends StatefulWidget {
@override
_PrintAndSendGoodPageState createState() => _PrintAndSendGoodPageState();
}
class _PrintAndSendGoodPageState extends State<PrintAndSendGoodPage> with SingleTickerProviderStateMixin {
TabController _tabController;
final List<Widget> tabBodies = [PrintPage(), SendGoodPage()];
final List<Tab> tabTitles = [new Tab(text: "打印"), new Tab(text: "发货")];
// 控制器
PageController pageController;
@override
void initState() {
super.initState();
_tabController = new TabController(length: tabBodies.length, vsync: this);
pageController = new PageController();
}
@override
void dispose() {
super.dispose();
_tabController.dispose();
pageController.dispose();
}
@override
Widget build(BuildContext context) {
return WillPopScope(
child: Scaffold(
appBar: CommonAppBar.buildAppBar(
"打印-发货",
tabBar: PreferredSize(
preferredSize: Size.fromHeight(48),
child: Material(
color: Colors.white,
elevation: 1,
child: TabBar(
indicatorColor: CommonColors.mainColor,
indicatorSize: TabBarIndicatorSize.label,
controller: _tabController,
indicatorPadding: EdgeInsets.all(8),
labelColor: CommonColors.mainColor,
unselectedLabelColor: Colors.black,
onTap: ((value) {
pageController.jumpTo(MediaQuery.of(context).size.width * value);
}),
tabs: tabTitles,
),
),
),
),
body: new PageView(
controller: pageController,
children: tabBodies,
onPageChanged: (index) {
_tabController.animateTo(index);
},
),
),
onWillPop: () {
return _onWillPop(context);
},
);
}
Future<bool> _onWillPop(BuildContext context) {
return showDialog(
context: context,
builder: (context) => new AlertDialog(
title: new Text('退出软件'),
actions: <Widget>[
new FlatButton(
onPressed: () => Navigator.of(context).pop(false),
child: new Text(
'取消',
style: TextStyle(color: Colors.black),
),
),
new FlatButton(
onPressed: () async => {
Navigator.of(context).pop(true),
},
child: new Text(
'确定',
style: TextStyle(color: Colors.black),
),
),
],
),
) ??
false;
}
}
子布局
继承此方法 AutomaticKeepAliveClientMixin 实现方法 为true 例如
class _PrintPageState extends State<PrintPage> with AutomaticKeepAliveClientMixin {