Flutter 解决切换 tab 后页面重置问题(TabBar + TabBarView)

一、问题说明

我们在 Android 开发的时候经常会用到 Tab + ViewPager 的页面模式,同样的,Flutter 也提供了这样的开发模式,但不同的是: Android 中的 ViewPager 提供了保留页面状态的支持,而 Flutter 的 TabController 却没有提供,这样的话每次点击 Tab 或左右滑动页面后都会触发重新构建渲染,我们在 initState() 中写的网络请求也会重复请求,这样的用户体验是很不好的 !

二、解决方案

  1. 子页面继承自 StatefulWidget
  2. State 需要继承自 AutomaticKeepAliveClientMixin
  3. 重写 get wantKeepAlive 方法,并返回 true

代码示例:

1、父页面

class OrderListContainerPage extends Widget {
  OrderListContainerPage({Key key}) : super(key: key);

  @override
  BaseState createState() {
    return _OrderListContainerPageState();
  }
}

class _OrderListContainerPageState extends State<OrderListContainerPage> {

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 3,
      child: Scaffold(
        appBar: AppBar(
          title: TabBar(
            tabs: <Widget>[
              Tab(child: Tab(text: 'tab1',),),
              Tab(child: Tab(text: 'tab2',),),
              Tab(child: Tab(text: 'tab3',),),
            ],
          ),
        ),
        body: TabBarView(
          children: <Widget>[
            OrderListPage(),
            OrderListPage(),
            OrderListPage(),
          ],
        ),
      ),
    );
  }
}

2、子页面

class OrderListPage extends StatefulWidget {

  OrderListPage({
    Key key,
  }) : super(key: key);

  @override
  State createState() {
    return _OrderListPageState();
  }

}

class _OrderListPageState extends BaseState<OrderListPage> with AutomaticKeepAliveClientMixin{

  @override
  Widget build(BuildContext context) {
    return Text('Hello World');
  }

  @override
  bool get wantKeepAlive => true;
}

从代码中可以看出:最关键的:继承自 AutomaticKeepAliveClientMixin 和重写 wantKeepAlive 方法。

三、补充注意点

如果你的页面是以下情况需要注意了:

  1. 继承自父页面。
  2. 在父页面中重写了 build 方法去构建公共元素,并向子页面开放了新的 buildChild(名字不一定是这个名字,只是举个例子) 类似功能方法。
  3. 子页面一般是重写父页面的 buildChild 去构建子元素。

如果你是以上情况,需要注意,必须重写其 build 方法,因为继承自 AutomaticKeepAliveClientMixin 后 build 方法就不是父页面的那个 build 了。 如果不重写的话会直接报红:build 不能返回 Null 类型。

 

搞定 !

要从另一个页面跳转到TabBarTabBarView中的页面,您可以使用Flutter的导航机制。以下是一个简单的示例: 假设您有两个页面:HomePage和TabbedPage。HomePage包含一个按钮,点击该按钮将导航到TabbedPage,TabbedPage包含TabBarTabBarView,每个标签对应一个页面。 在HomePage中,您可以使用Navigator.push方法跳转到TabbedPage: ```dart import 'package:flutter/material.dart'; import 'tabbed_page.dart'; class HomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Home'), ), body: Center( child: RaisedButton( child: Text('Go to Tabbed Page'), onPressed: () { Navigator.push( context, MaterialPageRoute(builder: (context) => TabbedPage()), ); }, ), ), ); } } void main() { runApp(MaterialApp( home: HomePage(), )); } ``` 在TabbedPage中,您可以创建一个DefaultTabController来管理TabBarTabBarView,并在TabBar中定义标签。每个标签对应一个页面。 ```dart import 'package:flutter/material.dart'; import 'tab_page.dart'; class TabbedPage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Tabbed Page'), bottom: TabBar( tabs: [ Tab(text: 'Page 1'), Tab(text: 'Page 2'), ], ), ), body: TabBarView( children: [ TabPage(title: 'Page 1'), TabPage(title: 'Page 2'), ], ), ); } } class TabPage extends StatelessWidget { final String title; TabPage({this.title}); @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Text(title), ), ); } } ``` 在上面的示例中,TabbedPage使用TabBarTabBarView来展示两个页面。每个页面TabPage小部件表示。您可以根据实际需求自定义这些页面。 当您从HomePage点击按钮跳转到TabbedPage时,将显示TabBarTabBarView,并且您可以在TabBar中进行不同标签页之间的切换。 希望对您有所帮助!如果有任何其他问题,请随时提问。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值