flutter TabBar 简单使用

文章介绍了如何在Flutter中使用TabBar组件,将数据请求放在子页面中,以减少接口调用次数并实现自动保持活力,提高性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TabBar 分成 子页面和父页面,接口只请求一次

父页面


class TabBarPage extends StatefulWidget {
  const TabBarPage({super.key});

  @override
  State<TabBarPage> createState() => _TabBarPageState();
}

class _TabBarPageState extends State<TabBarPage> with SingleTickerProviderStateMixin{

 late  TabController tabController ;
@override
  void initState() {
    super.initState();
    //初始页面,长度设置
    tabController = TabController(length: 2, vsync: this, initialIndex: 0);
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:null,
      body: DefaultTabController(
          length: 2,
          child: Column(
            children: [
              TabBar(
                controller: tabController,
                tabs: const [
                  Tab(text: 'Page 1'),
                  Tab(text: 'Page 2'),
                ],
                indicatorWeight: 3.rpx,
                indicatorPadding: const EdgeInsets.all(0),
                labelPadding: const EdgeInsets.all(0),
                isScrollable: false,
                indicatorSize: TabBarIndicatorSize.label,
                indicatorColor: const Color(0xff1A71FF),
                labelStyle: const TextStyle(fontWeight: FontWeight.w700),
                labelColor: const Color(0xff1A71FF),
                unselectedLabelColor: const Color(0xff2E3346),
                unselectedLabelStyle:
                const TextStyle(fontWeight: FontWeight.w500),
              ),
              Expanded(
                  child: TabBarView(
                    controller: tabController,
                    children: [
                      TabBarWidget1(),
                      TabBarWidget2(),
                    ],
                  ))
            ],
          )),
    );
  }
}

两个子页面,数据请求放进子页面来请求


class TabBarWidget1 extends StatefulWidget {
  const TabBarWidget1({super.key});

  @override
  State<TabBarWidget1> createState() => _TabBarWidget1State();
}

class _TabBarWidget1State extends State<TabBarWidget1>
    with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
  //继承AutomaticKeepAliveClientMixin类
    super.build(context);
    return ListView.builder(
        shrinkWrap: true,
        itemCount: 100,
        itemBuilder: (BuildContext context, int index) {
          return Container(
            margin: const EdgeInsets.all(4),
            width: 100,
            height: 20,
            decoration: BoxDecoration(
                color: Colors.red,
                borderRadius: BorderRadius.circular(10)),
          );
        });
  }
//多次滑动不会重复请求接口
  @override
  bool get wantKeepAlive => true;
}




class TabBarWidget2 extends StatefulWidget {
  const TabBarWidget2({super.key});

  @override
  State<TabBarWidget2> createState() => _TabBarWidget2State();
}

class _TabBarWidget2State extends State<TabBarWidget2>
    with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return ListView.builder(
        shrinkWrap: true,
        itemCount: 100,
        itemBuilder: (BuildContext context, int index) {
          return Container(
            margin: const EdgeInsets.all(4),
            width: 100,
            height: 20,
            decoration: BoxDecoration(
                color: Colors.blueAccent,
                borderRadius: BorderRadius.circular(10)),
          );
        });
  }

  @override
  bool get wantKeepAlive => true;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值