美文网首页工作小记
Flutter开发首页切换状态保存以及Android"再按一次退

Flutter开发首页切换状态保存以及Android"再按一次退

作者: 漂泊_sbb | 来源:发表于2021-02-02 16:19 被阅读0次

app首页切换时,保持页面状态,使用IndexedStack实现

IndexedStack(
          index: _selectedIndex,
          children: pages,
        )

参考
Flutter 三种方式实现页面切换后保持原页面状态

Android在首页时通常返回会有“再按一次退出”以及在其他tab时返回首页tab的要求

      body: WillPopScope(
        child: IndexedStack(
          index: _selectedIndex,
          children: pages,
        ),
        onWillPop: () async {
          print('_selectedIndex:${_selectedIndex}');
          if(_selectedIndex != 0){
            _onItemTapped(0);
            return false;
          }
          if (_lastPressedAt == null ||
              DateTime.now().difference(_lastPressedAt) > Duration(seconds: 1)) {
            //两次点击间隔超过1秒则重新计时
            _lastPressedAt = DateTime.now();
            Fluttertoast.showToast(msg: '再按一次退出');
            return false;
          }
          // 退出app
          await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
          return true;
        },
      ),
    );

相关文章

网友评论

    本文标题:Flutter开发首页切换状态保存以及Android"再按一次退

    本文链接:https://www.haomeiwen.com/subject/oxuytltx.html