• Flutter组件--TabBar使用详情(分段控制器)


    TabBar介绍

     

    一个显示水平行选项卡的Widget。 通常创建为 AppBar 的 AppBar.bottom 部分并与 TabBarView 结合使用

    在什么情况下使用TabBar

    当你的app内容类别比较多的时候,我们常常会用到TabBar,例如网易新闻、京东、B站等,所以TabBar是一个使用非常频繁的组件。

    如何创建

    步骤一:创建TabController

    为了使所选的 tab 与它所对应的内容能够同步变化,需要用 TabController 进行控制。我们既可以手动创建一个 TabController ,也能够直接使用 DefaultTabController widget。最简单的选择是使用 DefaultTabController widget,因为它能够创建出一个可供所有子 widgets 使用的 TabController

    1. DefaultTabController(
    2. // 选项卡的数量
    3. length: 3,
    4. child: // 在下一步完成此代码
    5. );

    步骤二:创建tabs

    当我们创建DefaultTabController, 接下来就可以用 TabBar widget 来创建 tabs。下面这个创建了包含三组 Tab widget 的 TabBar(一个),并把它放置于 AppBar widget 中。

    1. DefaultTabController(
    2. length: 3,
    3. child: Scaffold(
    4. appBar: AppBar(
    5. title: Text("TabBar"),
    6. bottom: TabBar(
    7. tabs: [
    8. Tab(icon: Icon(Icons.directions_bike),),
    9. Tab(icon: Icon(Icons.directions_boat),),
    10. Tab(icon: Icon(Icons.directions_car),),
    11. ],
    12. ),
    13. ),
    14. ),
    15. );

    步骤三:为每个Tab创建内容

    现在我们已经成功创建了一些 tabs,接下来要实现的是 tab 被选中时显示其对应的内容。为了实现这个效果,我们将使用 TabBarView widget。

    1. import 'package:flutter/material.dart';
    2. class TabBarExample extends StatefulWidget {
    3. @override
    4. _TabBarExampleState createState() => _TabBarExampleState();
    5. }
    6. class _TabBarExampleState extends State<TabBarExample> {
    7. @override
    8. Widget build(BuildContext context) {
    9. return DefaultTabController(
    10. length: 3,
    11. child: Scaffold(
    12. appBar: AppBar(
    13. title: Text("TabBar"),
    14. bottom: TabBar(
    15. tabs: [
    16. Tab(icon: Icon(Icons.directions_bike),),
    17. Tab(icon: Icon(Icons.directions_boat),),
    18. Tab(icon: Icon(Icons.directions_car),),
    19. ],
    20. ),
    21. ),
    22. body: TabBarView(
    23. children: [
    24. Icon(Icons.directions_bike),
    25. Icon(Icons.directions_boat),
    26. Icon(Icons.directions_car),
    27. ],
    28. ),
    29. ),
    30. );
    31. }
    32. }

    TabBar效果图


    ​​​​​​​

    注意事项:

               如果TabBar是需要在首页进行展示,而此时首页又有BottomNavigationBarItem的时候,因为TabBar和BottomNavigationBarItem都需要使用到​​​​​​​Scaffold.所以两者会产生冲突异常.这个时候直接在需要使用TabBar的页面重新创建Scaffold即可(只需要将Scaffold下面的appbar中的title直接更换成Tab组件即可).

    步骤四:自定义缓存组件(如果用户在第一个栏位下滑到底部,然后向右滑动到其他栏位,这个时候重新回到第一个栏位的时候,第一个栏位就会重新加载,而不会停留在上次底部位置,为此自定义了缓存功能)

    1. import 'package:flutter/material.dart';
    2. class KeepAliveWrapper extends StatefulWidget {
    3. final Widget? child;
    4. final bool keepAlive;
    5. const KeepAliveWrapper({Key? key,@required this.child,this.keepAlive=true}) : super(key: key);
    6. @override
    7. State createState() => _KeepAliveWrapperState();
    8. }
    9. class _KeepAliveWrapperState extends State<KeepAliveWrapper> with AutomaticKeepAliveClientMixin{
    10. @override
    11. Widget build(BuildContext context) {
    12. return widget.child!;
    13. }
    14. @override
    15. bool get wantKeepAlive => widget.keepAlive;
    16. @override
    17. void didUpdateWidget(covariant KeepAliveWrapper oldWidget) {
    18. // TODO: implement didUpdateWidget
    19. if(oldWidget.keepAlive !=widget.keepAlive)
    20. {
    21. // keepAlive 状态需要更新,实现在 AutomaticKeepAliveClientMixin 中 updateKeepAlive();
    22. updateKeepAlive();
    23. }
    24. super.didUpdateWidget(oldWidget);
    25. }
    26. }

    调用

    1. /*
    2. *在TabBarView直接进行调用 调用详情
    3. * body: TabBarView(
    4. * controller: _tabController,
    5. * children:[
    6. * KeepAliveWrapper(
    7. * child:***
    8. * ),
    9. * ],
    10. * )
    11. * */

    TabBar属性和说明

    序列号字段属性描述
    1tabsList两个多个的Tab列表
    2controllerTabController控制tab的控制器
    3isScrollablebool标签栏是否可以水平滚动
    4indicatorColorColor设置选中Tab指示器的颜色
    5automaticIndicatorColorAdjustmentbool是否自动调整indicatorColor
    6indicatorWeightdouble设置选中Tab指示器线条的粗细
    7indicatorPaddingEdgeInsetsGeometry设置选中Tab指示器间距,默认值为 EdgeInsets.zero
    8indicatorDecoration设置选中Tab指示器的外观
    9indicatorSizeTabBarIndicatorSize设置选中Tab指示器的大小
    10labelColorColor设置选中Tab文字的颜色
    11labelStyleTextStyle设置选中Tab文字的样式
    12labelPaddingEdgeInsetsGeometry设置选中Tab文字的间距
    13unselectedLabelColorColor设置未选中Tab文字的颜色
    14unselectedLabelStyleTextStyle设置未选中Tab文字的样式
    15dragStartBehaviorDragStartBehavior处理拖动开始行为的方式
    16overlayColorMaterialStateProperty定义响应焦点、悬停和飞溅颜色
    17mouseCursorMouseCursor鼠标指针进入或悬停在鼠标指针上时的光标
    18enableFeedbackbool检测到的手势是否应提供声音和/或触觉反馈
    19onTapValueChanged单击Tab时的回调
    20physicsScrollPhysicsTabBar的滚动视图如何响应用户输入

    TabBar属性详细使用

    1.tabs

    由两个多个组成的Tab列表

    1. TabBar(
    2. tabs: [
    3. Tab(icon: Icon(Icons.directions_bike),),
    4. Tab(icon: Icon(Icons.directions_boat),),
    5. Tab(icon: Icon(Icons.directions_car),),
    6. ],
    7. )

    2.controller

    可以控制tab的控制器

    1. TabController _tabController;
    2. @override
    3. void initState() {
    4. // TODO: implement initState
    5. super.initState();
    6. _tabController = TabController(length: 3, vsync: this);
    7. _tabController.addListener(()
    8. {
    9. //如果不做下面判断的话,单击页面的时候此处监听会执行2次
    10. if (_tabController.animation!.value == _tabController.index)
    11. {
    12. print(_tabController.index);
    13. //获取点击或滑动页面的索引值
    14. } });
    15. }
    16. TabBar(
    17. controller: _tabController,
    18. tabs: [
    19. Tab(icon: Icon(Icons.directions_bike),),
    20. Tab(icon: Icon(Icons.directions_boat),),
    21. Tab(icon: Icon(Icons.directions_car),),
    22. ],
    23. )

    3.isScrollable

    标签栏是否可以水平滚动

    1. TabBar(
    2. isScrollable: false,
    3. tabs: [
    4. Tab(icon: Icon(Icons.directions_bike),),
    5. Tab(icon: Icon(Icons.directions_boat),),
    6. Tab(icon: Icon(Icons.directions_car),),
    7. ],
    8. )

    4.indicatorColor

    设置选中Tab指示器的颜色

    1. TabBar(
    2. indicatorColor: Colors.red,
    3. tabs: [
    4. Tab(icon: Icon(Icons.directions_bike),),
    5. Tab(icon: Icon(Icons.directions_boat),),
    6. Tab(icon: Icon(Icons.directions_car),),
    7. ],
    8. )

    5.automaticIndicatorColorAdjustment

    是否自动调整 indicatorColor,如果 automaticIndicatorColorAdjustment 为 true 时,那么indicatorColor 会自动调整成 Colors.white

    1. TabBar(
    2. automaticIndicatorColorAdjustment: false,
    3. tabs: [
    4. Tab(icon: Icon(Icons.directions_bike),),
    5. Tab(icon: Icon(Icons.directions_boat),),
    6. Tab(icon: Icon(Icons.directions_car),),
    7. ],
    8. )

    6.indicatorWeight

    设置选中Tab指示器线条的粗细

    1. TabBar(
    2. indicatorColor: Colors.red,
    3. indicatorWeight: 5,
    4. tabs: [
    5. Tab(icon: Icon(Icons.directions_bike),),
    6. Tab(icon: Icon(Icons.directions_boat),),
    7. Tab(icon: Icon(Icons.directions_car),),
    8. ],
    9. )

    7.indicatorPadding

    设置选中Tab指示器间距,默认值为 EdgeInsets.zero

    1. TabBar(
    2. indicatorColor: Colors.red,
    3. indicatorWeight: 5,
    4. indicatorPadding: EdgeInsets.only(bottom: 2),
    5. tabs: [
    6. Tab(icon: Icon(Icons.directions_bike),),
    7. Tab(icon: Icon(Icons.directions_boat),),
    8. Tab(icon: Icon(Icons.directions_car),),
    9. ],
    10. )

    8.indicator

    设置选中Tab指示器的外观

    1. TabBar(
    2. indicatorColor: Colors.red,
    3. indicatorWeight: 5,
    4. indicatorPadding: EdgeInsets.only(bottom: 2),
    5. indicator: BoxDecoration(
    6. gradient: LinearGradient(colors: [
    7. Colors.orange,
    8. Colors.green
    9. ]),
    10. ),
    11. tabs: [
    12. Tab(icon: Icon(Icons.directions_bike),),
    13. Tab(icon: Icon(Icons.directions_boat),),
    14. Tab(icon: Icon(Icons.directions_car),),
    15. ],
    16. )

    9.indicatorSize

    设置选中Tab指示器的大小,该值是一个枚举类型TabBarIndicatorSize.tab 跟随 Tab 的宽度,TabBarIndicatorSize.label 跟随 Tab 内容文字的宽度。

    1. TabBar(
    2. indicatorColor: Colors.red,
    3. indicatorSize: TabBarIndicatorSize.tab,
    4. tabs: [
    5. Tab(icon: Icon(Icons.directions_bike),),
    6. Tab(icon: Icon(Icons.directions_boat),),
    7. Tab(icon: Icon(Icons.directions_car),),
    8. ],
    9. )

    10.labelColor

    设置选中Tab文字的颜色

    1. TabBar(
    2. indicatorColor: Colors.red,
    3. labelColor: Colors.pink,
    4. tabs: [
    5. Tab(icon: Icon(Icons.directions_bike),),
    6. Tab(icon: Icon(Icons.directions_boat),),
    7. Tab(icon: Icon(Icons.directions_car),),
    8. ],
    9. )

    11.labelStyle

    设置选中Tab文字的样式

    1. TabBar(
    2. labelStyle: TextStyle(
    3. backgroundColor: Colors.green,
    4. fontSize: 20
    5. ),
    6. tabs: [
    7. Tab(icon: Icon(Icons.directions_bike), text: "单车",),
    8. Tab(icon: Icon(Icons.directions_boat), text: "轮船",),
    9. Tab(icon: Icon(Icons.directions_car), text: "汽车",),
    10. ],
    11. )

    12.labelPadding

    设置选中Tab内容的间距

    1. TabBar(
    2. labelStyle: TextStyle(
    3. backgroundColor: Colors.green,
    4. fontSize: 20
    5. ),
    6. labelPadding: EdgeInsets.all(0),
    7. tabs: [
    8. Tab(icon: Icon(Icons.directions_bike), text: "单车",),
    9. Tab(icon: Icon(Icons.directions_boat), text: "轮船",),
    10. Tab(icon: Icon(Icons.directions_car), text: "汽车",),
    11. ],
    12. )

    13.unselectedLabelColor

    设置未选中Tab文字的颜色

    1. TabBar(
    2. unselectedLabelColor: Colors.orange,
    3. tabs: [
    4. Tab(icon: Icon(Icons.directions_bike), text: "单车",),
    5. Tab(icon: Icon(Icons.directions_boat), text: "轮船",),
    6. Tab(icon: Icon(Icons.directions_car), text: "汽车",),
    7. ],
    8. )

    14.unselectedLabelStyle

    设置未选中Tab文字的样式

    1. TabBar(
    2. unselectedLabelColor: Colors.orange,
    3. unselectedLabelStyle: TextStyle(
    4. backgroundColor: Colors.pink
    5. ),
    6. tabs: [
    7. Tab(icon: Icon(Icons.directions_bike), text: "单车",),
    8. Tab(icon: Icon(Icons.directions_boat), text: "轮船",),
    9. Tab(icon: Icon(Icons.directions_car), text: "汽车",),
    10. ],
    11. )

    15.dragStartBehavior

    处理拖动开始行为的方式

    1. TabBar(
    2. unselectedLabelColor: Colors.orange,
    3. unselectedLabelStyle: TextStyle(
    4. backgroundColor: Colors.pink
    5. ),
    6. dragStartBehavior: DragStartBehavior.start,
    7. tabs: [
    8. Tab(icon: Icon(Icons.directions_bike), text: "单车",),
    9. Tab(icon: Icon(Icons.directions_boat), text: "轮船",),
    10. Tab(icon: Icon(Icons.directions_car), text: "汽车",),
    11. ],
    12. )

    16.overlayColor

    定义响应焦点、悬停和飞溅颜色。

    17.mouseCursor

    鼠标指针进入或悬停在鼠标指针上时的光标,针对 Flutter web 应用

    1. TabBar(
    2. unselectedLabelColor: Colors.orange,
    3. unselectedLabelStyle: TextStyle(
    4. backgroundColor: Colors.pink
    5. ),
    6. mouseCursor: SystemMouseCursors.allScroll,
    7. tabs: [
    8. Tab(icon: Icon(Icons.directions_bike), text: "单车",),
    9. Tab(icon: Icon(Icons.directions_boat), text: "轮船",),
    10. Tab(icon: Icon(Icons.directions_car), text: "汽车",),
    11. ],
    12. )

    18.enableFeedback

    检测到的手势是否应提供声音和/或触觉反馈,默认为 true

    1. TabBar(
    2. enableFeedback: true,
    3. tabs: [
    4. Tab(icon: Icon(Icons.directions_bike), text: "单车",),
    5. Tab(icon: Icon(Icons.directions_boat), text: "轮船",),
    6. Tab(icon: Icon(Icons.directions_car), text: "汽车",),
    7. ],
    8. )

    19.onTap

    单击Tab时的回调

    1. TabBar(
    2. enableFeedback: true,
    3. onTap: (index) {
    4. //只能监听到单击事件,无法监听到滑动事件
    5. print(index);
    6. },
    7. tabs: [
    8. Tab(icon: Icon(Icons.directions_bike), text: "单车",),
    9. Tab(icon: Icon(Icons.directions_boat), text: "轮船",),
    10. Tab(icon: Icon(Icons.directions_car), text: "汽车",),
    11. ],
    12. )

    20.physics

    TabBar的滚动视图如何响应用户输入,

    例如,确定在用户停止拖动滚动视图后滚动视图如何继续动画

    1. TabBar(
    2. physics: NeverScrollableScrollPhysics(),
    3. tabs: [
    4. Tab(icon: Icon(Icons.directions_bike), text: "单车",),
    5. Tab(icon: Icon(Icons.directions_boat), text: "轮船",),
    6. Tab(icon: Icon(Icons.directions_car), text: "汽车",),
    7. ],
    8. )

  • 相关阅读:
    spring boot 自定义 starter
    Android AMS——进程优先级更新(十八)
    Nginx显示failed (13: Permission denied)
    API First——微服务架构下API接口驱动设计与开发
    Serverless 架构下的 AI 应用开发
    kubectl应用
    手写数组方法之会改变原数组方法
    51单片机入门——I2C总线与EEPROM
    企业数智化:为什么选择梧桐数据库?
    信号完整性(SI)电源完整性(PI)学习笔记(三十一)电源分配网路(三)
  • 原文地址:https://blog.csdn.net/eastWind1101/article/details/127961345