当前位置:   article > 正文

Flutter 组件之 TabBar 和TabBarView,和选项卡自定义_flutter 自定义tabview

flutter 自定义tabview

 好记性不如烂笔头,所以做了一下总结和个人使用方法

1.介绍

  大部分项目都会用到多个Tab切换功能,所以学会灵活使用TabBar 和TabBarView,对做项目会有很大的帮助,。

2.属性介绍

  1. TabBar({
  2. Key key,
  3. @required this.tabs,
  4. this.controller, //控制器
  5. this.isScrollable = false, //是否可以滚动,默认为 false(选项卡过多设置为true)
  6. this.indicatorColor,//指示器颜色
  7. this.indicatorWeight = 2.0,//指示器宽度
  8. this.indicatorPadding = EdgeInsets.zero,//指示器间距
  9. this.indicator,//自定义指示器
  10. this.indicatorSize,//指示器大小
  11. this.labelColor,//文字颜色
  12. this.labelStyle,//文字样式
  13. this.labelPadding,//文字间距
  14. this.unselectedLabelColor,//未选中 title 颜色
  15. this.unselectedLabelStyle,//未选中 title 样式
  16. this.dragStartBehavior = DragStartBehavior.start,//拖拽设置
  17. this.mouseCursor,//鼠标悬停(网页端)
  18. this.onTap,//点击事件
  19. this.physics,//滑动效果,如滑动到末端时,继续拉动,使用 ClampingScrollPhysics 实现Android下//微光效果。使用 BouncingScrollPhysics 实现iOS下弹性效果。
  20. })

3.基本用法,TabBar 使用时需要指定TabController 或者 DefaultTabController,不然会报错,这里主要使用TabController,能更好的做事件的监听。

  1. _tabController.addListener(() {
  2. print(this._tabController.index);//当前页面下标
  3. print(this._tabController.length);//长度
  4. print(this._tabController.previousIndex);//前一个页面的下标
  5. });

上图上代码

  1. class _MinePageState extends State<MinePage>
  2. with SingleTickerProviderStateMixin {//SingleTickerProviderStateMixin 一定要继承这个类,指利用with
  3. TabController _controller;//控制器
  4. @override
  5. void initState() {
  6. // TODO: implement initState
  7. super.initState();
  8. //初始化控制器length一定要对应不然会报错
  9. _controller = new TabController(vsync: this, length: 5);
  10. }
  11. @override
  12. Widget build(BuildContext context) {
  13. return Scaffold(
  14. appBar: AppBar(
  15. title: Text("Tabar"),
  16. bottom: _tabBar(),
  17. ),
  18. body: TabBarView(
  19. controller: _controller,
  20. children: [
  21. Center(
  22. child: Text(
  23. "1",
  24. ),
  25. ),
  26. Center(
  27. child: Text(
  28. "2",
  29. ),
  30. ),
  31. Center(
  32. child: Text(
  33. "3",
  34. ),
  35. ),
  36. Center(
  37. child: Text(
  38. "4",
  39. ),
  40. ),
  41. Center(
  42. child: Text(
  43. "5",
  44. ),
  45. )
  46. ],
  47. ),
  48. );
  49. }
  50. TabBar _tabBar() {
  51. return TabBar(
  52. controller: _controller,
  53. tabs: [
  54. Tab(
  55. text: "tab1",
  56. ),
  57. Tab(
  58. text: "tab2",
  59. ),
  60. Tab(
  61. text: "tab3",
  62. ),
  63. Tab(
  64. text: "tab4",
  65. ),
  66. Tab(
  67. text: "tab5",
  68. ),
  69. ],
  70. );
  71. }
  72. }

4.自定义菜单选项

设计师可能会出一些比较体现自身价值的设计,这个时候就需要自定义一下儿东西了,废话不多说上菜单了。

  1. class _DataPageState extends State<DataPage>
  2. with AutomaticKeepAliveClientMixin, SingleTickerProviderStateMixin {
  3. @override
  4. bool get wantKeepAlive => true;
  5. TabController _tabController;
  6. int _select = 0;//选中tab小标
  7. @override
  8. void initState() {
  9. // TODO: implement initState
  10. super.initState();
  11. _tabController = new TabController(vsync: this, length: list.length);
  12. _tabController.addListener(() {
  13. setState(() {
  14. _select = _tabController.index;
  15. });
  16. });
  17. }
  18. @override
  19. Widget build(BuildContext context) {
  20. return Container(
  21. child: Scaffold(
  22. body: Column(
  23. children: [
  24. Container(
  25. margin: EdgeInsets.only(top: ScreeAdapter.getStatusBarHeight()),
  26. height: ScreeAdapter.setHeight(44),
  27. child: TabBar(
  28. indicatorColor: Colors.transparent,//指示器设置为透明色
  29. indicatorPadding: EdgeInsets.fromLTRB(20, 0, 20, 0),
  30. isScrollable: true,//设置可滑动
  31. controller: _tabController,
  32. onTap: (value) {
  33. setState(() {
  34. _select = value;
  35. });
  36. },
  37. tabs: tabs()),
  38. ),
  39. Expanded(
  40. flex: 1,
  41. child: TabBarView(
  42. controller: _tabController,
  43. children: _tabBarViews(),
  44. ),
  45. )
  46. ],
  47. )),
  48. );
  49. }
  50. //tab标题
  51. List<String> list = [
  52. "直播",
  53. "推荐",
  54. "热门",
  55. "追番",
  56. "影视",
  57. "其他",
  58. ];
  59. List<Widget> tabs() {
  60. List<Widget> listTab = [];
  61. for (int i = 0; i < list.length; i++) {
  62. listTab.add(
  63. Tab(
  64. child: Container(
  65. height: ScreeAdapter.setHeight(44),
  66. child: Stack(
  67. children: [
  68. Align(
  69. child: Text(
  70. "${list[i]}",
  71. style: TextStyle(
  72. color: _select == i ? Colors.black : Colors.black38),
  73. ),
  74. alignment: Alignment.center,
  75. ),
  76. Positioned(
  77. child: Icon(Icons.arrow_drop_up,
  78. color: _select == i ? Colors.red : Colors.white),
  79. bottom: 0,
  80. )
  81. ],
  82. ),
  83. )),
  84. );
  85. }
  86. return listTab;
  87. }
  88. List<Widget> _tabBarViews() {
  89. return list.map<Widget>((value) {
  90. return Center(
  91. child: Text(value),
  92. );
  93. }).toList();
  94. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/431774
推荐阅读
相关标签
  

闽ICP备14008679号