StatefulWidget需要保持状态,切换页面后,Widget树不会dispose,保持原有的状态:

    • 混入AutomaticKeepAliveClientMixin抽象类,
    • 重写方法wantKeepAlive
    • 在build方法中 调用父类的方法 super.build(context) ```dart

    class ChatPage extends StatefulWidget { @override _ChatPageState createState() => _ChatPageState(); }

    class _ChatPageState extends State with AutomaticKeepAliveClientMixin {

    @override bool get wantKeepAlive => true; @override Widget build(BuildContext context) { super.build(context); return Scaffold( appBar: AppBar( title: Text(‘微信’), backgroundColor: WeChatThemeColor,

    1. ),
    2. );

    } }

    1. 使用bottomNavigationBar的情况下,要保持pages的状态,需使用PageController
    2. ```dart
    3. import 'package:flutter/material.dart';
    4. import 'pages/chat_page.dart';
    5. import 'pages/friends/friends_page.dart';
    6. import 'pages/mine_page.dart';
    7. import 'pages/discover/discover_page.dart';
    8. class RootPage extends StatefulWidget {
    9. @override
    10. _RootPageState createState() => _RootPageState();
    11. }
    12. class _RootPageState extends State<RootPage> {
    13. int _currentIndex = 0;
    14. final PageController _controller = PageController(initialPage: 0);
    15. @override
    16. Widget build(BuildContext context) {
    17. return Scaffold(
    18. body: PageView(
    19. controller: _controller,
    20. physics: NeverScrollableScrollPhysics(), //不滚动
    21. children: [
    22. ChatPage(),
    23. FriendsPage(),
    24. DiscoverPage(),
    25. MinePage()],
    26. ),
    27. bottomNavigationBar: BottomNavigationBar(
    28. onTap: (index) {
    29. setState(() {
    30. _currentIndex = index;
    31. _controller.animateToPage(index, duration: Duration(microseconds: 100), curve: Curves.easeIn);
    32. });
    33. },
    34. selectedFontSize: 12.0, //选中的字体大小
    35. currentIndex: _currentIndex,
    36. fixedColor: Colors.green,
    37. type: BottomNavigationBarType.fixed,
    38. items: [
    39. BottomNavigationBarItem(
    40. icon: Image.asset(
    41. 'images/tabbar_chat.png',
    42. height: 20,
    43. width: 20,
    44. ),
    45. activeIcon: Image.asset(
    46. 'images/tabbar_chat_hl.png',
    47. height: 20,
    48. width: 20,
    49. ),
    50. title: Text('微信'),
    51. ),
    52. BottomNavigationBarItem(
    53. icon: Image.asset(
    54. 'images/tabbar_friends.png',
    55. height: 20,
    56. width: 20,
    57. ),
    58. activeIcon: Image.asset(
    59. 'images/tabbar_friends_hl.png',
    60. height: 20,
    61. width: 20,
    62. ),
    63. title: Text('通讯录'),
    64. ),
    65. BottomNavigationBarItem(
    66. icon: Image.asset(
    67. 'images/tabbar_discover.png',
    68. height: 20,
    69. width: 20,
    70. ),
    71. activeIcon: Image.asset(
    72. 'images/tabbar_discover_hl.png',
    73. height: 20,
    74. width: 20,
    75. ),
    76. title: Text('发现'),
    77. ),
    78. BottomNavigationBarItem(
    79. icon: Image.asset(
    80. 'images/tabbar_mine.png',
    81. height: 20,
    82. width: 20,
    83. ),
    84. activeIcon: Image.asset(
    85. 'images/tabbar_mine_hl.png',
    86. height: 20,
    87. width: 20,
    88. ),
    89. title: Text('我的'),
    90. ),
    91. ],
    92. ),
    93. );
    94. }
    95. }