当前位置:   article > 正文

flutter TabBar 简单使用_flutter tabbar可以直接使用吗

flutter tabbar可以直接使用吗

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(),
                    ],
                  ))
            ],
          )),
    );
  }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

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


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;
}



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

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;
}



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/431785
推荐阅读
相关标签
  

闽ICP备14008679号