開始嘗試用flutter開發(fā),flutter版本1.0,寫類似微信底部tab切換界面時(shí)發(fā)現(xiàn)界面老被重置,網(wǎng)上找了一圈說保持狀態(tài)需要子頁面mixin AutomaticKeepAliveClientMixin,然后重寫
@override
bool get wantKeepAlive => true;
但發(fā)現(xiàn)需要配合其他組件,不是隨便mixin就有用的,嘗試幾種寫法總結(jié)BottomNavigationBar+List<Widget>+AutomaticKeepAliveClientMixin是沒有用的
- 首先嘗試BottomNavigationBar+List<Widget>實(shí)現(xiàn)的頁面切換保持狀態(tài),一般剛開始學(xué)都會(huì)這么寫:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) => MaterialApp(
title: "demo",
home: MainPage(),
);
}
class MainPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => MainPageState();
}
class MainPageState extends State<MainPage> {
int _currentIndex;
List<Widget> _pages;
@override
void initState() {
super.initState();
_currentIndex = 0;
_pages = List()..add(FirstPage("第一頁"))..add(SecondPage("第二頁"))..add(ThirdPage("第三頁"));
}
@override
Widget build(BuildContext context) => Scaffold(
body: _pages[_currentIndex],
bottomNavigationBar: BottomNavigationBar(
items: getItems(),
currentIndex: _currentIndex,
onTap: onTap,
),
);
List<BottomNavigationBarItem> getItems() {
return [
BottomNavigationBarItem(icon: Icon(Icons.home), title: Text("Home")),
BottomNavigationBarItem(icon: Icon(Icons.adb), title: Text("Adb")),
BottomNavigationBarItem(icon: Icon(Icons.person), title: Text("Person"))
];
}
void onTap(int index) {
setState(() {
_currentIndex = index;
});
}
}
子頁面代碼,三個(gè)界面一樣:
class FirstPage extends StatefulWidget {
String _title;
FirstPage(this._title);
@override
State<StatefulWidget> createState() => FirstPageState();
}
class FirstPageState extends State<FirstPage> with AutomaticKeepAliveClientMixin{
int _count = 0;
@override
bool get wantKeepAlive => true;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget._title),
),
body: Center(
child: Text(widget._title + ":點(diǎn)一下加1:$_count"),
),
floatingActionButton: FloatingActionButton(
heroTag: widget._title, child: Icon(Icons.add), onPressed: add),
);
}
void add() {
setState(() {
_count++;
});
}
}
結(jié)果無法實(shí)現(xiàn)保持頁面

2.第二種BottomNavigationBar+PageView,與android的ViewPager類似,界面小改動(dòng)一下,添加一個(gè)按鈕,點(diǎn)擊跳轉(zhuǎn)到一個(gè)新的界面

代碼如下:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) => MaterialApp(
title: "demo",
home: MainPage(),
);
}
class MainPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => MainPageState();
}
class MainPageState extends State<MainPage> {
int _currentIndex;
List<Widget> _pages;
PageController _controller;
@override
void initState() {
super.initState();
_currentIndex = 0;
_pages = List() ..add(FirstPage("第一頁")) ..add(SecondPage("第二頁")) ..add(ThirdPage("第三頁"));
_controller = PageController(initialPage: 0);
}
@override
void dispose() {
super.dispose();
_controller.dispose();
}
@override
Widget build(BuildContext context) => Scaffold(
body: PageView.builder(
physics: NeverScrollableScrollPhysics(),//viewPage禁止左右滑動(dòng)
onPageChanged: _pageChange,
controller: _controller,
itemCount: _pages.length,
itemBuilder: (context, index) => _pages[index]),
bottomNavigationBar: BottomNavigationBar(
items: getItems(),
currentIndex: _currentIndex,
onTap: onTap,
),
);
List<BottomNavigationBarItem> getItems() {
return [
BottomNavigationBarItem(icon: Icon(Icons.home), title: Text("Home")),
BottomNavigationBarItem(icon: Icon(Icons.adb), title: Text("Adb")),
BottomNavigationBarItem(icon: Icon(Icons.person), title: Text("Person"))
];
}
void onTap(int index) {
_controller.jumpToPage(index);
}
void _pageChange(int index) {
if (index != _currentIndex) {
setState(() {
_currentIndex = index;
});
}
}
}
子界面:
class FirstPage extends StatefulWidget {
String _title;
FirstPage(this._title);
@override
State<StatefulWidget> createState() => FirstPageState();
}
class FirstPageState extends State<FirstPage>
with AutomaticKeepAliveClientMixin {
int _count = 0;
@override
bool get wantKeepAlive => true;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget._title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(widget._title + ":點(diǎn)一下加1:$_count"),
MaterialButton(
child: Text("跳轉(zhuǎn)"),
color: Colors.pink,
onPressed: () => Navigator.push(context,
MaterialPageRoute(builder: (context) => NewPage())))
],
),
),
floatingActionButton: FloatingActionButton(
heroTag: widget._title, child: Icon(Icons.add), onPressed: add),
);
}
void add() {
setState(() {
_count++;
});
}
}
需要跳轉(zhuǎn)的一個(gè)界面:
class NewPage extends StatelessWidget {
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: Text("新的界面"),
),
body: Center(
child: Text("我是一個(gè)新的界面"),
),
);
}

猛一看效果出來了,左右切換界面沒有問題,結(jié)果跳轉(zhuǎn)新界面時(shí)又出現(xiàn)新問題,當(dāng)?shù)谝豁撎D(zhuǎn)新的界面再返回,再切第二、三頁發(fā)現(xiàn)重置了,再切回第一頁發(fā)現(xiàn)頁被重置了。
發(fā)生這種情況需要在重寫Widget build(BuildContext context)時(shí)調(diào)用下父類build(context)方法,局部代碼:
@override
Widget build(BuildContext context) {
//在這邊加上super.build(context);
super.build(context);
return Scaffold(
appBar: AppBar(
title: Text(widget._title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(widget._title + ":點(diǎn)一下加1:$_count"),
MaterialButton(
child: Text("跳轉(zhuǎn)"),
color: Colors.pink,
onPressed: () => Navigator.push(context,
MaterialPageRoute(builder: (context) => NewPage())))
],
),
),
floatingActionButton: FloatingActionButton(
heroTag: widget._title, child: Icon(Icons.add), onPressed: add),
);
}

這種布局樣式網(wǎng)上還有一種用的比較多的是BottomNavigationBar+IndexedStack( ),這邊就不貼出來了
- 經(jīng)過長期測試BottomNavigationBar+TabBarView方案行不通,后期會(huì)遇到其他問題,目前最好用還是viewpage和IndexedStack。
最后像這種多頁面使用FloatingActionButton,用它跳轉(zhuǎn)新界面是一定要設(shè)置heroTag,要不然跳轉(zhuǎn)會(huì)黑屏報(bào)錯(cuò)