前言:

在 Flutter 应用中,导航栏切换页面后默认情况下会丢失原页面状态,即每次进入页面时都会重新初始化状态,如果在initState中打印日志,会发现每次进入时都会输出,显然这样增加了额外的开销,并且带来了不好的用户体验。
在正文之前,先看一些常见的 App 导航,以喜马拉雅 FM 为例:

Flutter状态管理 - Flutter 三种方式实现页面切换后保持原页面状态 - 图1

它拥有一个固定的底部导航以及首页的顶部导航,可以看到不管是点击底部导航切换页面还是在首页左右侧滑切换页面,之前的页面状态都是始终维持的,下面就具体介绍下如何在 flutter 中实现类似喜马拉雅的导航效果

第一步:实现固定的底部导航

在通过flutter create生成的项目模板中,我们先简化一下代码,将MyHomePage提取到一个单独的home.dart文件,并在Scaffold脚手架中添加bottomNavigationBar底部导航,在body中展示当前选中的子页面。

  1. /// home.dart
  2. import 'package:flutter/material.dart';
  3. import './pages/first_page.dart';
  4. import './pages/second_page.dart';
  5. import './pages/third_page.dart';
  6. class MyHomePage extends StatefulWidget {
  7. @override
  8. _MyHomePageState createState() => _MyHomePageState();
  9. }
  10. class _MyHomePageState extends State<MyHomePage> {
  11. final items = [
  12. BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('首页')),
  13. BottomNavigationBarItem(icon: Icon(Icons.music_video), title: Text('听')),
  14. BottomNavigationBarItem(icon: Icon(Icons.message), title: Text('消息'))
  15. ];
  16. final bodyList = [FirstPage(), SecondPage(), ThirdPage()];
  17. int currentIndex = 0;
  18. void onTap(int index) {
  19. setState(() {
  20. currentIndex = index;
  21. });
  22. }
  23. @override
  24. Widget build(BuildContext context) {
  25. return Scaffold(
  26. appBar: AppBar(
  27. title: Text('demo'),
  28. ),
  29. bottomNavigationBar: BottomNavigationBar(
  30. items: items,
  31. currentIndex: currentIndex,
  32. onTap: onTap
  33. ),
  34. body: bodyList[currentIndex]
  35. );
  36. }
  37. }

其中的三个子页面结构相同,均显示一个计数器和一个加号按钮,以first_page.dart为例:

  1. /// first_page.dart
  2. import 'package:flutter/material.dart';
  3. class FirstPage extends StatefulWidget {
  4. @override
  5. _FirstPageState createState() => _FirstPageState();
  6. }
  7. class _FirstPageState extends State<FirstPage> {
  8. int count = 0;
  9. void add() {
  10. setState(() {
  11. count++;
  12. });
  13. }
  14. @override
  15. Widget build(BuildContext context) {
  16. return Scaffold(
  17. body: Center(
  18. child: Text('First: $count', style: TextStyle(fontSize: 30))
  19. ),
  20. floatingActionButton: FloatingActionButton(
  21. onPressed: add,
  22. child: Icon(Icons.add),
  23. )
  24. );
  25. }
  26. }

当前效果如下:

Flutter状态管理 - Flutter 三种方式实现页面切换后保持原页面状态 - 图2

可以看到,从第二页切换回第一页时,第一页的状态已经丢失

第二步:实现底部导航切换时保持原页面状态

可能有些小伙伴在搜索后会开始直接使用官方推荐的AutomaticKeepAliveClientMixin,通过在子页面的 State 类重写wantKeepAlivetrue 。 然而,如果你的代码和我上面的类似,body 中并没有使用PageViewTabBarView,很不幸的告诉你,踩到坑了,这样是无效的,原因后面再详述。现在我们先来介绍另外两种方式:
① 使用IndexedStack实现
IndexedStack继承自Stack,它的作用是显示第indexchild,其它child在页面上是不可见的,但所有child的状态都被保持,所以这个Widget可以实现我们的需求,我们只需要将现在的bodyIndexedStack包裹一层即可

  1. /// home.dart
  2. class _MyHomePageState extends State<MyHomePage> {
  3. ...
  4. ...
  5. ...
  6. @override
  7. Widget build(BuildContext context) {
  8. return Scaffold(
  9. appBar: AppBar(
  10. title: Text('demo'),
  11. ),
  12. bottomNavigationBar: BottomNavigationBar(
  13. items: items, currentIndex: currentIndex, onTap: onTap),
  14. // body: bodyList[currentIndex]
  15. body: IndexedStack(
  16. index: currentIndex,
  17. children: bodyList,
  18. ));
  19. }

保存后再次测试一下

Flutter状态管理 - Flutter 三种方式实现页面切换后保持原页面状态 - 图3

② 使用Offstage实现
Offstage的作用十分简单,通过一个参数来控制child是否显示,所以我们同样可以组合使用Offstage来实现该需求,其实现原理与IndexedStack类似

  1. /// home.dart
  2. class _MyHomePageState extends State<MyHomePage> {
  3. ...
  4. ...
  5. ...
  6. @override
  7. Widget build(BuildContext context) {
  8. return Scaffold(
  9. appBar: AppBar(
  10. title: Text('demo'),
  11. ),
  12. bottomNavigationBar: BottomNavigationBar(
  13. items: items, currentIndex: currentIndex, onTap: onTap),
  14. // body: bodyList[currentIndex],
  15. body: Stack(
  16. children: [
  17. Offstage(
  18. offstage: currentIndex != 0,
  19. child: bodyList[0],
  20. ),
  21. Offstage(
  22. offstage: currentIndex != 1,
  23. child: bodyList[1],
  24. ),
  25. Offstage(
  26. offstage: currentIndex != 2,
  27. child: bodyList[2],
  28. ),
  29. ],
  30. ));
  31. }
  32. }

在上面的两种方式中都可以实现保持原页面状态的需求,但这里有一些开销上的问题,有经验的小伙伴应该能发现当应用第一次加载的时候,所有子页状态都被实例化了(>这里的细节并不是因为我直接把子页实例化放在bodyList里…<),如果在子页StateinitState中打印日志,可以在终端看到一次性输出了所有子页的日志。下面就介绍另一种通过继承AutomaticKeepAliveClientMixin的方式来更好的实现保持状态。

第三步:实现首页的顶部导航

首先我们通过配合使用TabBar+TabBarView+AutomaticKeepAliveClientMixin来实现顶部导航(注意:TabBarTabBarView需要提供controller,如果自己没有定义,则必须使用DefaultTabController包裹)。此处也可以选择使用PageView,后面会介绍。
我们先在home.dart文件移除Scaffold脚手架中的appBar顶部工具栏,然后开始重写首页first_page.dart:

  1. /// first_page.dart
  2. import 'package:flutter/material.dart';
  3. import './recommend_page.dart';
  4. import './vip_page.dart';
  5. import './novel_page.dart';
  6. import './live_page.dart';
  7. class _TabData {
  8. final Widget tab;
  9. final Widget body;
  10. _TabData({this.tab, this.body});
  11. }
  12. final _tabDataList = <_TabData>[
  13. _TabData(tab: Text('推荐'), body: RecommendPage()),
  14. _TabData(tab: Text('VIP'), body: VipPage()),
  15. _TabData(tab: Text('小说'), body: NovelPage()),
  16. _TabData(tab: Text('直播'), body: LivePage())
  17. ];
  18. class FirstPage extends StatefulWidget {
  19. @override
  20. _FirstPageState createState() => _FirstPageState();
  21. }
  22. class _FirstPageState extends State<FirstPage> {
  23. final tabBarList = _tabDataList.map((item) => item.tab).toList();
  24. final tabBarViewList = _tabDataList.map((item) => item.body).toList();
  25. @override
  26. Widget build(BuildContext context) {
  27. return DefaultTabController(
  28. length: tabBarList.length,
  29. child: Column(
  30. children: <Widget>[
  31. Container(
  32. width: double.infinity,
  33. height: 80,
  34. padding: EdgeInsets.fromLTRB(20, 24, 0, 0),
  35. alignment: Alignment.centerLeft,
  36. color: Colors.black,
  37. child: TabBar(
  38. isScrollable: true,
  39. indicatorColor: Colors.red,
  40. indicatorSize: TabBarIndicatorSize.label,
  41. unselectedLabelColor: Colors.white,
  42. unselectedLabelStyle: TextStyle(fontSize: 18),
  43. labelColor: Colors.red,
  44. labelStyle: TextStyle(fontSize: 20),
  45. tabs: tabBarList),
  46. ),
  47. Expanded(
  48. child: TabBarView(
  49. children: tabBarViewList,
  50. // physics: NeverScrollableScrollPhysics(), // 禁止滑动
  51. ))
  52. ],
  53. ));
  54. }
  55. }

其中推荐页、VIP 页、小说页、直播页的结构仍和之前的首页结构相同,仅显示一个计数器和一个加号按钮,以推荐页recommend_page.dart为例:

  1. /// recommend_page.dart
  2. import 'package:flutter/material.dart';
  3. class RecommendPage extends StatefulWidget {
  4. @override
  5. _RecommendPageState createState() => _RecommendPageState();
  6. }
  7. class _RecommendPageState extends State<RecommendPage> {
  8. int count = 0;
  9. void add() {
  10. setState(() {
  11. count++;
  12. });
  13. }
  14. @override
  15. void initState() {
  16. super.initState();
  17. print('recommend initState');
  18. }
  19. @override
  20. Widget build(BuildContext context) {
  21. return Scaffold(
  22. body:Center(
  23. child: Text('首页推荐: $count', style: TextStyle(fontSize: 30))
  24. ),
  25. floatingActionButton: FloatingActionButton(
  26. onPressed: add,
  27. child: Icon(Icons.add),
  28. ));
  29. }
  30. }

保存后测试,
Flutter状态管理 - Flutter 三种方式实现页面切换后保持原页面状态 - 图4
可以看到,现在添加了首页顶部导航,且默认支持左右侧滑,接下来再进一步的完善状态保持

第四步:实现首页顶部导航切换时保持原页面状态

③ 使用AutomaticKeepAliveClientMixin实现
写到这里已经很简单了,我们只需要在首页导航内需要保持页面状态的子页State中,继承AutomaticKeepAliveClientMixin并重写wantKeepAlivetrue即可。
notes:Subclasses must implement wantKeepAlive, and their build methods must call super.build (the return value will always return null, and should be ignored)
以首页推荐recommend_page.dart为例:

  1. /// recommend_page.dart
  2. import 'package:flutter/material.dart';
  3. class RecommendPage extends StatefulWidget {
  4. @override
  5. _RecommendPageState createState() => _RecommendPageState();
  6. }
  7. class _RecommendPageState extends State<RecommendPage>
  8. with AutomaticKeepAliveClientMixin {
  9. int count = 0;
  10. void add() {
  11. setState(() {
  12. count++;
  13. });
  14. }
  15. @override
  16. bool get wantKeepAlive => true;
  17. @override
  18. void initState() {
  19. super.initState();
  20. print('recommend initState');
  21. }
  22. @override
  23. Widget build(BuildContext context) {
  24. super.build(context);
  25. return Scaffold(
  26. body:Center(
  27. child: Text('首页推荐: $count', style: TextStyle(fontSize: 30))
  28. ),
  29. floatingActionButton: FloatingActionButton(
  30. onPressed: add,
  31. child: Icon(Icons.add),
  32. ));
  33. }
  34. }

再次保存测试,
Flutter状态管理 - Flutter 三种方式实现页面切换后保持原页面状态 - 图5

现在已经可以看到,不管是切换底部导航还是切换首页顶部导航,所有的页面状态都可以被保持,并且在应用第一次加载时,终端只看到recommend initState的日志,第一次切换首页顶部导航至 vip 页面时,终端输出vip initState,当再次返回推荐页时,不再输出recommend initState
所以,使用TabBarView+AutomaticKeepAliveClientMixin这种方式既实现了页面状态的保持,又具有类似惰性求值的功能,对于未使用的页面状态不会进行实例化,减小了应用初始化时的开销。

更新

前面在底部导航介绍了使用IndexedStackOffstage两种方式实现保持页面状态,但它们的缺点在于第一次加载时便实例化了所有的子页面State。为了进一步优化,下面我们使用PageView+AutomaticKeepAliveClientMixin重写之前的底部导航,其中PageViewTabBarView的实现原理类似,具体选择哪一个并没有强制要求。更新后的home.dart文件如下:

  1. /// home.dart
  2. import 'package:flutter/material.dart';
  3. import './pages/first_page.dart';
  4. import './pages/second_page.dart';
  5. import './pages/third_page.dart';
  6. class MyHomePage extends StatefulWidget {
  7. @override
  8. _MyHomePageState createState() => _MyHomePageState();
  9. }
  10. class _MyHomePageState extends State<MyHomePage> {
  11. final items = [
  12. BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('首页')),
  13. BottomNavigationBarItem(icon: Icon(Icons.music_video), title: Text('听')),
  14. BottomNavigationBarItem(icon: Icon(Icons.message), title: Text('消息'))
  15. ];
  16. final bodyList = [FirstPage(), SecondPage(), ThirdPage()];
  17. final pageController = PageController();
  18. int currentIndex = 0;
  19. void onTap(int index) {
  20. pageController.jumpToPage(index);
  21. }
  22. void onPageChanged(int index) {
  23. setState(() {
  24. currentIndex = index;
  25. });
  26. }
  27. @override
  28. Widget build(BuildContext context) {
  29. return Scaffold(
  30. bottomNavigationBar: BottomNavigationBar(
  31. items: items, currentIndex: currentIndex, onTap: onTap),
  32. // body: bodyList[currentIndex],
  33. body: PageView(
  34. controller: pageController,
  35. onPageChanged: onPageChanged,
  36. children: bodyList,
  37. physics: NeverScrollableScrollPhysics(), // 禁止滑动
  38. ));
  39. }
  40. }

然后在bodyList的子页State中继承AutomaticKeepAliveClientMixin并重写wantKeepAlive,以second_page.dart为例:

  1. /// second_page.dart
  2. import 'package:flutter/material.dart';
  3. class SecondPage extends StatefulWidget {
  4. @override
  5. _SecondPageState createState() => _SecondPageState();
  6. }
  7. class _SecondPageState extends State<SecondPage>
  8. with AutomaticKeepAliveClientMixin {
  9. int count = 0;
  10. void add() {
  11. setState(() {
  12. count++;
  13. });
  14. }
  15. @override
  16. bool get wantKeepAlive => true;
  17. @override
  18. void initState() {
  19. super.initState();
  20. print('second initState');
  21. }
  22. @override
  23. Widget build(BuildContext context) {
  24. super.build(context);
  25. return Scaffold(
  26. body: Center(
  27. child: Text('Second: $count', style: TextStyle(fontSize: 30))
  28. ),
  29. floatingActionButton: FloatingActionButton(
  30. onPressed: add,
  31. child: Icon(Icons.add),
  32. ));
  33. }
  34. }

Ok,更新后保存运行,应用第一次加载时不会输出second initState,仅当第一次点击底部导航切换至该页时,该子页的State被实例化。
至此,如何实现一个类似的 底部 + 首页顶部导航 完结 ~

查看原文