https://api.flutter.dev/flutter/material/TabBar-class.html
https://api.flutter.dev/flutter/material/TabBarView-class.html

DefaultTabController、TabBar、TabBarView

TabBar定义:

  1. const TabBar({
  2. Key key,
  3. @required this.tabs, //显示的标签内容,一般使用Tab 对象,也可以是其他的Widget
  4. this.controller, //TabController 对象
  5. this.isScrollable = false, //是否可滚动
  6. this.indicatorColor, //指示器颜色
  7. this.indicatorWeight = 2.0, //指示器高度
  8. this.indicatorPadding = EdgeInsets.zero, //指示器边距
  9. this.indicator, //指示器decoration,例如边框等
  10. this.indicatorSize, //指示器大小计算方式,TabBarIndicatorSize.label 跟文字等宽,
  11. //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 颜色
  17. this.dragStartBehavior = DragStartBehavior.start,
  18. this.mouseCursor,
  19. this.onTap,
  20. this.physics,
  21. })

image.png

bandicam 2020-11-23 17-03-58-063.mp4

DefaultTabController 控制

  1. import "package:flutter/material.dart";
  2. import "package:app1/utils/Base.dart";
  3. class MyHomePage extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return DefaultTabController(
  7. length: 3,
  8. child: Column(
  9. children: [
  10. Container(
  11. color: Colors.grey,
  12. child: TabBar(
  13. tabs: [
  14. Tab(text: 'Cat'),
  15. Tab(text: 'Dog'),
  16. Tab(text: 'Bird'),
  17. ],
  18. ),
  19. ),
  20. Container(
  21. height: 200.0,
  22. child: TabBarView(
  23. children: [
  24. Container(color: Colors.blue, child: Text('Cat')),
  25. Container(color: Colors.red, child: Text('Dog')),
  26. Container(color: Colors.green, child: Text('Bird')),
  27. ],
  28. ),
  29. ),
  30. ],
  31. ),
  32. );
  33. }
  34. }

TabController 控制

  1. import "package:flutter/material.dart";
  2. class TestPage extends StatefulWidget {
  3. @override
  4. _TestPageState createState() => _TestPageState();
  5. }
  6. class _TestPageState extends State<TestPage> with SingleTickerProviderStateMixin {
  7. TabController _tabController;
  8. @override
  9. void initState() {
  10. // TODO: implement initState
  11. super.initState();
  12. _tabController = new TabController(length: 3, vsync: this);
  13. // 监听tab切换
  14. _tabController.addListener(() {
  15. print(_tabController.index);
  16. });
  17. }
  18. @override
  19. Widget build(BuildContext context) {
  20. return Scaffold(
  21. appBar: AppBar(
  22. title: Text('flutter demo'),
  23. bottom: TabBar(
  24. controller: _tabController,
  25. tabs: [
  26. Tab(text: 'Cat'),
  27. Tab(text: 'Dog'),
  28. Tab(text: 'Bird'),
  29. ],
  30. ),
  31. ),
  32. body: Container(
  33. height: 200.0,
  34. child: TabBarView(
  35. controller: _tabController,
  36. children: [
  37. Container(color: Colors.blue, child: Text('Cat')),
  38. Container(color: Colors.red, child: Text('Dog')),
  39. Container(color: Colors.green, child: Text('Bird')),
  40. ],
  41. ),
  42. ),
  43. );
  44. }
  45. }