Flutter切换tab后保留tab状态

Flutter切换tab后保留tab状态

概述

Flutter中为了节约内存不会保存widget的状态,widget都是临时变量。当我们使用TabBar,TabBarView是我们就会发现,切换tab后再重新切换回上一页面,这时候tab会重新加载重新创建,体验很不友好。Flutter出于自己的设计考虑并没有延续android的ViewPager这样的缓存页面设计,毕竟控件两端都要开发,目前还在beta版本有很多设计还不够完善,但是设计的拓展性没得说,flutter还是为我们提供了解决办法。我们可以强制widget不显示情况下保留状态,下回再加载时就不用重新创建了。

AutomaticKeepAliveClientMixin

AutomaticKeepAliveClientMixin 是一个抽象状态,使用也很简单,我们只需要用我们自己的状态继承这个抽象状态,并实现 wantKeepAlive 方法即可。

继承这个状态后,widget在不显示之后也不会被销毁仍然保存在内存中,所以慎重使用这个方法。

详细官方文档请看这里
这里还有一个说的比较详细的 demo

class PageContentState extends<PageContent> 
    with AutomaticKeepAliveClientMixin {

    @override
    bool get wantKeepAlive => true;

}

DEMO

// main.dart
// 核心代码如下:

class PageContent extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new PageContentState();
}

class PageContentState extends State<PageContent>
    with AutomaticKeepAliveClientMixin
{

  @override
  bool get wantKeepAlive => true;

  ···
}

class V2EX extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        title: 'Flutter Demo',
        theme: new ThemeData(
            primaryColor: Colors.white),
        home: new DefaultTabController(
          length: choices.length,
          child: new Scaffold(
            appBar: new AppBar(
              title: new Center(
                child: new Container(
                  width: 300.0,
                  child: new TabBar(
                    indicatorColor: Colors.black54,
                    isScrollable: true,
                    tabs: choices.map((Choice choice) {
                      return new Container(
                        width: 125.0,
                        child: new Tab(
                          text: choice.title,
                        ),
                      );
                    }).toList(),
                  ),
                ),
              ),
            ),
            body: new TabBarView(
              key: new Key('Home Page'),
              children: choices.map((Choice choice) {
                return new Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: new ChoiceCard(choice: choice),
                );
              }).toList(),
            ),
          ),
        ));
  }
}

const List<Choice> choices = const <Choice>[
  const Choice(title: '热门'),
  const Choice(title: '最新'),
];

class ChoiceCard extends StatelessWidget {
  const ChoiceCard({Key key, this.choice}) : super(key: key);

  final Choice choice;

  @override
  Widget build(BuildContext context) {
    return new Card(
      color: Colors.white,
      child: PageContent(),
    );
  }
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值