AutomaticKeepAliveClientMixin 无效 (配合BottomAppBar) flutter

Flutter的开发过程中,继承了AutomaticKeepAliveClientMixin

并且重载了wantKeepAlive 方法 返回true,甚至super content

但是任然无效,查询过程中发现很多人遇到此问题,所以记录一下

后来发现需配合一些控件才能实现效果 比如PageView ,TabBarView

 

切换之后就不会丢失当前的状态了,也不会再次调用initstate()方法

 

 

 

主要方法

1:PageController

 
 

PageController _pageController; _pageController = PageController(initialPage: this._index, keepPage: true);

2:PageView

 
 

PageView( controller: _pageController, children: _eachView, ),

3:控件继承 AutomaticKeepAliveClientMixin 并重载wantKeepAlive 方法

 

以下是完整代码


import 'package:flutter/material.dart';
import 'package:jxz/widget/my_appbar.dart';

class DemoBottomBarPage extends StatefulWidget {
  @override
  _DemoBottomBarPageState createState() => _DemoBottomBarPageState();
}

class _DemoBottomBarPageState extends State<DemoBottomBarPage> {
  //TabBarView 也可以  但是是 是TabController
  PageController _pageController;
  List<Widget> _eachView;
  int _index = 0;

  @override
  void initState() {
    _eachView = List();
    _eachView..add(EachView())..add(EachView2());

    this._pageController = PageController(initialPage: this._index, keepPage: true);
    super.initState();
  }

  @override
  void dispose() {
    //记得要销毁哦
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(title: '底部导航'),
      floatingActionButton: FloatingActionButton(
          //浮动交互按钮
          onPressed: () {
            Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context) {
              return EachView();
            }));
          },
          tooltip: '新建页', //长按提示
          backgroundColor: Colors.indigoAccent, //背景颜色
          child: Icon(
            Icons.add,
            color: Colors.white,
          )),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      //融合底部工具栏
      bottomNavigationBar: BottomAppBar(
        color: Colors.indigo,
        shape: CircularNotchedRectangle(), //圆形缺口  和  FloatingActionButtonLocation融合
        child: Row(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            IconButton(
              icon: Icon(Icons.home),
              color: Colors.white,
              onPressed: () {
                setState(() {
                  _index = 0;
                  _pageController.jumpToPage(_index);
                });
              },
            ),
            IconButton(
              icon: Icon(Icons.person),
              color: Colors.white,
              onPressed: () {
                setState(() {
                  _index = 1;
                  _pageController.jumpToPage(_index);
                });
              },
            ),
          ],
        ),
      ),
      body: PageView(
        //禁用横向滑动切换
        physics: NeverScrollableScrollPhysics(),
        controller: _pageController,
        children: _eachView,
      ),
    );
  }
}

class EachView extends StatefulWidget {
  @override
  _EachViewState createState() => _EachViewState();
}

/// 继承  AutomaticKeepAliveClientMixin 保持界面状态
class _EachViewState extends State<EachView> with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    return Center(
        child: Container(
            width: 220,
            child: TextFormField(
              decoration: InputDecoration(labelText: '手机号', hintText: '请输入手机号'),
            )));
  }
}

class EachView2 extends StatefulWidget {
  @override
  _EachView2State createState() => _EachView2State();
}

class _EachView2State extends State<EachView2> with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  List devices = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: devices.length,
      itemExtent: 120,
      itemBuilder: (_, index) => Card(child: Center(child: Text('当前Item:${index}'))),
    );
  }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值