【Flutter学习】基本组件之TabBar顶部导航

╰+攻爆jí腚メ 2021-12-23 11:47 514阅读 0赞

一,概述

  TabBar,是材料设计(Material design)中很常用的一种横向标签页。在Android原生开发中,我们常用ViewPage或者一些常用的标签页开源库,来实现并行界面的横向滑动展示,在iOS原生开发中我们可以基于UICollectionView/UIButton来封装实现这一功能,在Flutter的世界中,TabBar是被定义在Material Component中,所以他的使用需要在MaterialApp中。通常,我们会在AppBar的底部部分结合TabBarView来使用TabBar。
    1115039-20190624185801412-1657320295.png

二,Tab关键元素

  • TabController
    这是Tab页的控制器,用于定义Tab标签和内容页的坐标,还可配置标签页的切换动画效果等。
    TabController一般放入有状态控件中使用,以适应标签页数量和内容有动态变化的场景,如果标签页在APP中是静态固定的格局,则可以在无状态控件中加入简易版的DefaultTabController以提高运行效率,毕竟无状态控件要比有状态控件更省资源,运行效率更快。

  • TabBar
    Tab页的Title控件,切换Tab页的入口,一般放到AppBar控件下使用,内部有*Title属性。其子元素按水平横向排列布局,如果需要纵向排列,请使用ColumnListView控件包装一下。子元素为Tab类型的数组

  • TabBarView
    Tab页的内容容器,其内放置Tab页的主体内容。子元素可以是多个各种类型的控件。

三,构造函数  

  • TabController
  1. * **DefalutTabController**
  2. const DefaultTabController({
  3. Key key,
  4. @required this.length,
  5. this.initialIndex = 0,
  6. @required this.child,
  7. }) : assert(initialIndex != null),
  8. assert(length >= 0),
  9. assert(initialIndex >= 0 && initialIndex < length),
  10. super(key: key);
  11. * TabController
  12. TabController({ int initialIndex = 0, @required this.length, @required TickerProvider vsync })
  13. : assert(length != null && length >= 0),
  14. assert(initialIndex != null && initialIndex >= 0 && (length == 0 || initialIndex < length)),
  15. _index = initialIndex,
  16. _previousIndex = initialIndex,
  17. _animationController = AnimationController.unbounded(
  18. value: initialIndex.toDouble(),
  19. vsync: vsync,
  20. );
  • TabBar
  1. /**
  2. const TabBar({
  3. Key key,
  4. @required this.tabs,//显示的标签内容,一般使用Tab对象,也可以是其他的Widget
  5. this.controller,//TabController对象
  6. this.isScrollable = false,//是否可滚动
  7. this.indicatorColor,//指示器颜色
  8. this.indicatorWeight = 2.0,//指示器高度
  9. this.indicatorPadding = EdgeInsets.zero,//底部指示器的Padding
  10. this.indicator,//指示器decoration,例如边框等
  11. this.indicatorSize,//指示器大小计算方式,TabBarIndicatorSize.label跟文字等宽,TabBarIndicatorSize.tab跟每个tab等宽
  12. this.labelColor,//选中label颜色
  13. this.labelStyle,//选中label的Style
  14. this.labelPadding,//每个label的padding值
  15. this.unselectedLabelColor,//未选中label颜色
  16. this.unselectedLabelStyle,//未选中label的Style
  17. }) : assert(tabs != null),
  18. assert(isScrollable != null),
  19. assert(indicator != null || (indicatorWeight != null && indicatorWeight > 0.0)),
  20. assert(indicator != null || (indicatorPadding != null)),
  21. super(key: key);
  22. */
  23. * **Tab**
  24. const Tab({
  25. Key key,
  26. this.text,
  27. this.icon,
  28. this.child,
  29. }) : assert(text != null || child != null || icon != null),
  30. assert(!(text != null && null != child)), // TODO(goderbauer): https://github.com/dart-lang/sdk/issues/34180
  31. super(key: key);
  • TabBarView
  1. const TabBarView({
  2. Key key,
  3. @required this.children, //Tab页内容页组件数组集合
  4. this.controller, //TabController对象
  5. this.physics,
  6. this.dragStartBehavior = DragStartBehavior.start,
  7. }) : assert(children != null),
  8. assert(dragStartBehavior != null),
  9. super(key: key);

四,创建标签栏

  • 1.创建TabController

    • 使用默认的DefaultController
  1. /**2.创建Tabbar */
  2. @override
  3. Widget build(BuildContext context) {
  4. // TODO: implement build
  5. return new DefaultTabController(
  6. length: myTabs.length,
  7. child: new Scaffold(
  8. //AppBar
  9. appBar:new AppBar(
  10. title: new Text('顶部标签栏'),
  11. bottom: new TabBar(
  12. tabs: myTabs, //标签数组
  13. indicatorColor: Colors.blue,//指示器的颜色
  14. isScrollable: true,//是否滑动
  15. ),
  16. ) ,
  17. /**3.绑定Tabbar 和 TabBarView */
  18. //body
  19. body: new TabBarView(
  20. children: myTabs.map((Tab tab){
  21. return new Center( child: new Text(tab.text));
  22. }).toList(),
  23. ),
  24. ),
  25. );
  26. }
  27. * 使用自定义的TabController
  28. class TabBarDemoState extends State<TabBarDemo>
  29. with SingleTickerProviderStateMixin {
  30. TabController _tabController; //定义tabcontroller变量
  31. @override
  32. void dispose() {
  33. _tabController.dispose(); //销毁
  34. super.dispose();
  35. }
  36. void initState() {
  37. super.initState();
  38. _tabController = new TabController(vsync: this, length: 3); //创建
  39. }
  40. @override
  41. Widget build(BuildContext context) {
  42. return new Scaffold(
  43. appBar: new AppBar(
  44. title: new Text('顶部tab切换'),
  45. bottom: new TabBar(
  46. tabs: <Widget>[
  47. new Tab(
  48. icon: new Icon(Icons.directions_bike),
  49. ),
  50. new Tab(
  51. icon: new Icon(Icons.directions_boat),
  52. ),
  53. new Tab(
  54. icon: new Icon(Icons.directions_bus),
  55. ),
  56. ],
  57. controller: _tabController, //tabbar与自定义的tabcontroller绑定
  58. ),
  59. ),
  60. body: new TabBarView(
  61. controller: _tabController, //tabbarView与 自定义的tabController绑定
  62. children: <Widget>[
  63. new Center(child: new Text('自行车')),
  64. new Center(child: new Text('船')),
  65. new Center(child: new Text('巴士')),
  66. ],
  67. ),
  68. );
  69. }
  • 2.构建Tab数据/TabBarView数据
  1. /**1. 创建Tab数据 */
  2. final List<Tab> myTabs = <Tab>[
  3. new Tab(icon: new Icon(Icons.home),
  4. text:'首页',
  5. ),
  6. new Tab(
  7. icon: new Icon(Icons.message),
  8. text:'个人信息',
  9. ),
  10. new Tab(
  11. icon: new Icon(Icons.camera),
  12. text:'朋友圈',
  13. ),
  14. new Tab(
  15. icon: new Icon(Icons.access_alarm),
  16. text: '闹钟',
  17. )
  18. ];
    1. 创建Tabbar
  1. appBar:new AppBar(
  2. title: new Text('顶部标签栏'),
  3. bottom: new TabBar(
  4. tabs: myTabs, //标签数组
  5. indicatorColor: Colors.blue,//指示器的颜色
  6. isScrollable: true,//是否滑动
  7. ),
  8. )
  • 4.绑定TabBar 和 TabBarView
  1. /**3.绑定Tabbar 和 TabBarView */
  2. //body
  3. body: new TabBarView(
  4. children: myTabs.map((Tab tab){
  5. return new Center( child: new Text(tab.text));
  6. }).toList(),
  7. ),
  • 5.全部代码
  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MyApp());
  3. class MyApp extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. // TODO: implement build
  7. return new MaterialApp(
  8. title: '顶部标签栏',
  9. theme: new ThemeData(
  10. primaryColor: Colors.red
  11. ),
  12. home: new App(),
  13. );
  14. }
  15. }
  16. class App extends StatelessWidget {
  17. /**1. 创建Tab数据 */
  18. final List<Tab> myTabs = <Tab>[
  19. new Tab(icon: new Icon(Icons.home),
  20. text:'首页',
  21. ),
  22. new Tab(
  23. icon: new Icon(Icons.message),
  24. text:'个人信息',
  25. ),
  26. new Tab(
  27. icon: new Icon(Icons.camera),
  28. text:'朋友圈',
  29. ),
  30. new Tab(
  31. icon: new Icon(Icons.access_alarm),
  32. text: '闹钟',
  33. )
  34. ];
  35. /**2.创建Tabbar */
  36. @override
  37. Widget build(BuildContext context) {
  38. // TODO: implement build
  39. return new DefaultTabController(
  40. length: myTabs.length,
  41. child: new Scaffold(
  42. //AppBar
  43. appBar:new AppBar(
  44. title: new Text('顶部标签栏'),
  45. bottom: new TabBar(
  46. tabs: myTabs, //绑定标签数组
  47. indicatorColor: Colors.blue,//指示器的颜色
  48. isScrollable: true,//是否滑动
  49. ),
  50. ) ,
  51. /**3.绑定Tabbar 和 TabBarView */
  52. //body
  53. body: new TabBarView(
  54. children: myTabs.map((Tab tab){
  55. return new Center( child: new Text(tab.text));
  56. }).toList(),
  57. ),
  58. ),
  59. );
  60. }
  61. }

五,总结

TabBarView和TabBar都有一个TabController的参数,TabbarView和TabBar就是由TabController来控制同步,点击某个Tab后,要同步显示对应的TabBarView,创建TabController有两种方式:

  • 第一种:使用系统自带的DefaultTabController,在Scaffold套一层DefaultTabController,这种方式TabBarView会自动查找这个tabController。
  1. @override
  2. Widget build(BuildContext context) {
  3. return new DefaultTabController();
  4. }
  • 第二种是自己定义一个TabController,实现SingleTickerProviderStateMixin
  1. 参考上面“使用自定tabcontroller代码”

转载于:https://www.cnblogs.com/lxlx1798/p/11078901.html

发表评论

表情:
评论列表 (有 0 条评论,514人围观)

还没有评论,来说两句吧...

相关阅读