Flutter 组件之AppBar、顶部TabBar、仿今日头条顶部导航练习

亦凉 2023-07-17 11:28 79阅读 0赞

debugShowCheckedModeBanner:false,//去掉debug图标
initialRoute:'/initDemo',//初始化的时候加载的路由

AppBar自定义顶部按钮颜色、图标
  1. appBar:AppBar(
  2. title:Text("AppBarButton"),
  3. centerTitle:true,//标题居中显示
  4. leading:IconButton(//如果有自定义按钮当前页面的默认返回按钮就被覆盖
  5. icon:Icon(Icons.menu),
  6. onPressed:(){
  7. print("点击AppBarButton图标");
  8. }
  9. ),
  10. actions:<Widget>[
  11. IconButton(
  12. icon:Icon(Icons.search),
  13. onPressed:(){
  14. print("点击search图标");
  15. }
  16. ),
  17. IconButton(
  18. icon:Icon(Icons.settings),
  19. onPressed:(){
  20. print("点击settings图标");
  21. }
  22. ),
  23. ]
  24. )
AppBar自定义TabBar实现顶部Tab切换
  1. class MyApp extends StatelessWidget{
  2. @override
  3. Widget build(BuildContext context) {
  4. return new MaterialApp(
  5. debugShowCheckedModeBanner: false,
  6. title: 'TabBar Demo',
  7. home: new Scaffold(
  8. body: DefaultTabController(//顶部TabBar
  9. length: 2,//顶部TabBar数量
  10. initialIndex: 1,//默认选择第一项
  11. child: Scaffold(//这里配置bottom属性来写TabBar
  12. appBar: new AppBar(//顶部内容
  13. title: new Text('TabBar Demo'),
  14. leading: Icon(Icons.menu),
  15. actions: <Widget>[//尾部图标
  16. Icon(Icons.search)
  17. ],
  18. bottom: new TabBar(//这里面写TabBar
  19. tabs: <Widget>[
  20. Tab(text: '选项一',icon: Icon(Icons.add_shopping_cart),),
  21. Tab(text: '选项二',icon: Icon(Icons.wifi_tethering),),
  22. ],
  23. indicatorColor: Colors.black,//指示器颜色
  24. indicatorWeight: 5,指示器高度
  25. //指示器大小计算方式,TabBarIndicatorSize.label跟文字等宽,TabBarIndicatorSize.tab跟每个tab等宽
  26. indicatorSize: TabBarIndicatorSize.label,,//指示器大小,底部边框
  27. labelColor: Colors.limeAccent,//选中label颜色
  28. unselectedLabelColor: Colors.deepOrange,//未选中label颜色
  29. ),
  30. ),
  31. body: new TabBarView(//关联上面的bottomTabBar 这里可以配置多个组件,
  32. //数组数量跟TabBar要一致
  33. children: <Widget>[
  34. Text( 'TabBarView选项一'),
  35. Text( 'TabBarView选项二'),
  36. ],
  37. ),
  38. )
  39. ),
  40. ),
  41. );
  42. }
  43. }
把TabBar放在导航最顶部(不要原生AppBar)

只需要把TabBar 从bottom中移到 appBar -> title 中

  1. class MyApp extends StatelessWidget{
  2. @override
  3. Widget build(BuildContext context) {
  4. return new MaterialApp(
  5. debugShowCheckedModeBanner: false,
  6. title: 'TabBar Demo',
  7. home: new Scaffold(
  8. body: DefaultTabController(//顶部TabBar
  9. length: 8,//顶部TabBar数量
  10. initialIndex: 1,默认选择第一项
  11. child: Scaffold(//这里配置bottom属性来写TabBar
  12. appBar: new AppBar(//顶部内容
  13. title: Row(//title 里面放TabBar组件
  14. children:<Widget>[
  15. Expanded:(
  16. child:TabBar(//这里面写TabBar
  17. isScrollable: true,//允许滚动
  18. indicatorColor: Colors.black,//指示器颜色
  19. indicatorWeight: 5,指示器高度
  20. //指示器大小计算方式,TabBarIndicatorSize.label跟文字等宽,TabBarIndicatorSize.tab跟每个tab等宽
  21. indicatorSize: TabBarIndicatorSize.label,,//指示器大小,底部边框
  22. labelColor: Colors.limeAccent,//选中label颜色
  23. unselectedLabelColor: Colors.deepOrange,//未选中label颜色
  24. tabs: <Widget>[
  25. Tab(text: "热销1"),
  26. Tab(text: "推荐2"),
  27. Tab(text: "推荐3"),
  28. Tab(text: "推荐4") ,
  29. Tab(text: "推荐5"),
  30. Tab(text: "推荐6"),
  31. Tab(text: "推荐7"),
  32. Tab(text: "推荐8")
  33. ],
  34. ),
  35. ),
  36. ]
  37. ),
  38. ),
  39. body: new TabBarView(//关联上面的bottomTabBar 这里可以配置多个组件,
  40. //数组数量跟TabBar要一致
  41. children: <Widget>[
  42. Text( 'TabBarView选项一'),
  43. Text( 'TabBarView选项二'),
  44. Text( 'TabBarView选项3'),
  45. Text( 'TabBarView选项4'),
  46. Text( 'TabBarView选项5'),
  47. Text( 'TabBarView选项6'),
  48. Text( 'TabBarView选项7'),
  49. Text( 'TabBarView选项8'),
  50. ],
  51. ),
  52. )
  53. ),
  54. ),
  55. );
  56. }
  57. }
AppBar中自定义TabBar实现Tabs的另一种方法

controller的好处就是可以再initState 初始化时执行一些自定义事件。比如 _tabController.addListener();

步骤:

  1. 继承 SingleTickerProviderStateMixin
  2. TabController _tabController; 配置controller参数
  3. 重写void initState() 初始化方法
  4. _tabController = new TabController(length: 8, vsync: this); 实例化TabController
  5. TabBar、TabBarView中配置 controller: this._tabController,

示例代码:

  1. import 'package:flutter/material.dart';
  2. /* * StatefulWidget Template */
  3. class TabContollerPage extends StatefulWidget {
  4. TabContollerPage({ Key key}) : super(key: key);
  5. @override
  6. _TabContollerPageState createState() => _TabContollerPageState();
  7. }
  8. //这里要继承 SingleTickerProviderStateMixin
  9. class _TabContollerPageState extends State<TabContollerPage> with SingleTickerProviderStateMixin{
  10. TabController _tabController; //配置controller参数
  11. @override
  12. void initState() { //初始化已加载触发的方法
  13. // TODO: implement initState
  14. super.initState();
  15. // TabController 两个参数一个tabBar 个数,另一个固定写法
  16. _tabController = new TabController(length: 8, vsync: this);
  17. //定义事件
  18. _tabController.addListener((){
  19. print(_tabController.index);
  20. });
  21. }
  22. //其他写法与刚才Scaffold 里的写法一样
  23. //需要注意的就是要在TabBar 和 TabView中配置controller
  24. @override
  25. Widget build(BuildContext context) {
  26. return Scaffold(
  27. appBar: AppBar(
  28. backgroundColor: Colors.black26,
  29. title: Row(
  30. mainAxisAlignment: MainAxisAlignment.center,
  31. children: <Widget>[
  32. Expanded(
  33. child: TabBar(
  34. controller: this._tabController,//必须要配置这个包括下面的tabView
  35. indicatorColor: Colors.blue,
  36. labelColor: Colors.blue,
  37. unselectedLabelColor: Colors.white,
  38. indicatorSize: TabBarIndicatorSize.label,
  39. isScrollable: true,//允许滚动
  40. tabs: <Widget>[
  41. Tab(text: "热销1"),
  42. Tab(text: "推荐2"),
  43. Tab(text: "推荐3"),
  44. Tab(text: "推荐4") ,
  45. Tab(text: "推荐5"),
  46. Tab(text: "推荐6"),
  47. Tab(text: "推荐7"),
  48. Tab(text: "推荐8")
  49. ],
  50. ),
  51. )
  52. ],
  53. ),
  54. ),
  55. body: TabBarView(
  56. controller: this._tabController,//controller 必须配置
  57. children: <Widget>[
  58. ListView(
  59. children: <Widget>[
  60. ListTile(title: Text("第一个tab")),
  61. ListTile(title: Text("第一个tab")),
  62. ListTile(title: Text("第一个tab"))
  63. ],
  64. ),
  65. ListView(
  66. children: <Widget>[
  67. ListTile(title: Text("第二个tab")),
  68. ListTile(title: Text("第二个tab")),
  69. ListTile(title: Text("第二个tab"))
  70. ],
  71. ),
  72. ListView(
  73. children: <Widget>[
  74. ListTile(title: Text("第3个tab")),
  75. ListTile(title: Text("第3个tab")),
  76. ListTile(title: Text("第一个tab"))
  77. ],
  78. ),
  79. ListView(
  80. children: <Widget>[
  81. ListTile(title: Text("第4个tab")),
  82. ListTile(title: Text("第二个tab")),
  83. ListTile(title: Text("第二个tab"))
  84. ],
  85. ),
  86. ListView(
  87. children: <Widget>[
  88. ListTile(title: Text("第5个tab")),
  89. ListTile(title: Text("第二个tab")),
  90. ListTile(title: Text("第二个tab"))
  91. ],
  92. ),
  93. ListView(
  94. children: <Widget>[
  95. ListTile(title: Text("第6个tab")),
  96. ListTile(title: Text("第二个tab")),
  97. ListTile(title: Text("第二个tab"))
  98. ],
  99. ),
  100. ListView(
  101. children: <Widget>[
  102. ListTile(title: Text("第7个tab")),
  103. ListTile(title: Text("第二个tab")),
  104. ListTile(title: Text("第二个tab"))
  105. ],
  106. ),
  107. ListView(
  108. children: <Widget>[
  109. ListTile(title: Text("第8个tab")),
  110. ListTile(title: Text("第二个tab")),
  111. ListTile(title: Text("第二个tab"))
  112. ],
  113. )
  114. ],
  115. )
  116. );
  117. }
  118. }

发表评论

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

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

相关阅读