flutter中bottomNavigationBar切換組件保存狀態方案

原文鏈接 https://www.aiprose.com/blog/107

我們在開發的時候用底部導航欄是很常見的,flutter給我們默認帶了bottomNavigationBar,但是發現你直接這樣寫的時候,點擊導航欄切換組建的時候,每次都會刷新狀態,這用戶體驗是很不好的,今天給大家一種效率超高的解決方案,就是用PageView+AutomaticKeepAliveClientMixin組合實現狀態保存,切換組件的時候不刷新。
在這裏插入圖片描述

1.首先在有bottomNavigationBar的組件中加入pageview

/*
   * 存儲的四個頁面,和android 中的 Fragment一樣
   */
  var _pages;
  void initData() {
    _pages = [
      new WebPage(),
      new DiscoverPage(),
      new UserPage(),
    ];
  }
PageView.builder(
 //要點1
 physics: NeverScrollableScrollPhysics(),
 //禁止頁面左右滑動切換
 controller: _pageController,
 onPageChanged: _pageChanged,
 //回調函數
 itemCount: _pages.length,
 itemBuilder: (context, index) => _pages[index]),

在這裏插入圖片描述

class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
  int _tabIndex = 0;
  var tabImages;
  var appBarTitles = ['首頁', '發現', '我的'];
  var _pageController = PageController();
  Text getTabTitle(int curIndex) {
    if (curIndex == _tabIndex) {
      return new Text(appBarTitles[curIndex]);
    } else {
      return new Text(appBarTitles[curIndex]);
    }
  }
  /*
   * 存儲的四個頁面,和Fragment一樣
   */
  var _pages;
  void initData() {
    _pages = [
      new WebPage(),
      new DiscoverPage(),
      new UserPage(),
    ];
  }
  @override
  Widget build(BuildContext context) {
    initData();
    return Scaffold(
      body: SafeArea(
        child: PageView.builder(
            //要點1
            physics: NeverScrollableScrollPhysics(),
            //禁止頁面左右滑動切換
            controller: _pageController,
            onPageChanged: _pageChanged,
            //回調函數
            itemCount: _pages.length,
            itemBuilder: (context, index) => _pages[index]),
      ),
      bottomNavigationBar: new BottomNavigationBar(
        items: <BottomNavigationBarItem>[
          new BottomNavigationBarItem(
              icon: Icon(IconData(0xe71a, fontFamily: "iconfont")),
              title: getTabTitle(0)),
          new BottomNavigationBarItem(
              icon: Icon(IconData(0xe746, fontFamily: "iconfont")),
              title: getTabTitle(1)),
          new BottomNavigationBarItem(
              icon: Icon(IconData(0xe636, fontFamily: "iconfont")),
              title: getTabTitle(2)),
        ],
        type: BottomNavigationBarType.fixed,
        fixedColor: Colors.green,
        currentIndex: _tabIndex,
        onTap: (index) {
          print('onTap');
          _pageController.jumpToPage(index);
        },
      ),
    );
  }
  void _pageChanged(int index) {
    print('_pageChanged');
    setState(() {
      if (_tabIndex != index) _tabIndex = index;
    });
  }
}

這個時候我們發現頁面可以切換了,但是狀態還是沒有保存下來,接下來我們要修改其他的組件了。

2.在組件中實現AutomaticKeepAliveClientMixin

讓我們的state實現with AutomaticKeepAliveClientMixin,必須要重寫一個方法


@override
bool get wantKeepAlive => true;

在這裏插入圖片描述
這兩個必須要組合使用,才能實現保存狀態不刷新,每個需要保存狀態的組件都要with AutomaticKeepAliveClientMixin

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章