在上一篇里总结AppBar的一些简单用法,但是AppBar除了有前面那些样式属性外,还能实现类似底部的Tab切换。

首先下载并运行前面的项目:

然后在此基础上实现Tab切换。

常见属性

TabBar有一下常见的属性:

  • tabs :显示的标签内容,一般使用 Tab 对象,也可以是其他的 Widget
  • controller :TabController 对象
  • isScrollable :是否可滚动
  • indicatorColor :指示器颜色
  • indicatorWeight :指示器高度
  • indicatorPadding :底部指示器的 Padding
  • indicator :指示器 decoration,例如边框等
  • indicatorSize :指示器大小计算方式,TabBarIndicatorSize.label 跟文字等宽,TabBarIndicatorSize.tab 跟每个 tab 等宽
  • labelColor :选中 label 颜色
  • labelStyle :选中 label 的 Style
  • labelPadding :每个 label 的 padding 值
  • unselectedLabelColor :未选中 label 颜色
  • unselectedLabelStyle :未选中 label 的 Style

基本实现

为了实现顶部的Tabs切换,首先需要在Scaffold的外层定义一个DefaultTabController组件,然后组件里面定义tab的个数,最后将TabBar定义在AppBar里面的bottom属性中。根据这些,我们来修改前面的

AppBarDemo.dart页面。
import 'package:flutter/material.dart';

class AppBarDemoPage extends StatelessWidget {
const AppBarDemoPage({Key key}) : super(key: key); @override
Widget build(BuildContext context) {
return DefaultTabController(
length:2 ,
child: Scaffold(
appBar: AppBar(
title:Text("AppBarDemoPage"),
// backgroundColor: Colors.red,
centerTitle:true,
leading: IconButton(
icon: Icon(Icons.menu),
onPressed: (){
print('menu');
},
),
actions: <Widget>[
IconButton(
icon: Icon(Icons.search),
onPressed: (){
print('search');
},
),
IconButton(
icon: Icon(Icons.settings),
onPressed: (){
print('settings');
},
)
],
bottom: TabBar(
tabs: <Widget>[
Tab(text: "热门"),
Tab(text: "推荐")
],
),
),
body: Text('1111'),
),
);
}
}

为了简化代码,删掉前面关于AppBar的属性设置:

AppBarDemo.dart

import 'package:flutter/material.dart';

class AppBarDemoPage extends StatelessWidget {
const AppBarDemoPage({Key key}) : super(key: key); @override
Widget build(BuildContext context) {
return DefaultTabController(
length:2 ,
child: Scaffold(
appBar: AppBar(
title:Text("AppBarDemoPage"),
centerTitle:true,
bottom: TabBar(
tabs: <Widget>[
Tab(text: "热门"),
Tab(text: "推荐")
],
),
),
body: Text('1111'),
),
);
}
}

现在,只有跳转的按钮,却没有对应的页面组件,所以,还需要在body里面添加tabs切换的页面。

 

目前,是在一个新的页面添加了顶部Tabs切换,那么,如果需要在底部TabBar页面基础上添加Tabs切换,又该如何操作呢?

TabBar中添加顶部Tab切换

按照前面说的,在Scaffold的外层定义一个DefaultTabController组件,先这样修改Category.dart页面:

import 'package:flutter/material.dart';

class CategoryPage extends StatefulWidget {
CategoryPage({Key key}) : super(key: key);
_CategoryPageState createState() => _CategoryPageState();
} class _CategoryPageState extends State<CategoryPage> {
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 4,
child: Scaffold(
appBar: AppBar(
bottom:TabBar(
tabs: <Widget>[
Tab(text: "热销"),
Tab(text: "推荐"),
Tab(text: "推荐"),
Tab(text: "推荐")
],
) ,
),
body:TabBarView(
children: <Widget>[
ListView(
children: <Widget>[
ListTile(title:Text("第一个tab")),
],
),
ListView(
children: <Widget>[
ListTile(title:Text("第二个tab")),
],
),
ListView(
children: <Widget>[
ListTile(title:Text("第三个tab")),
],
),
ListView(
children: <Widget>[
ListTile(title:Text("第四个tab")),
],
)
],
)
),
);
}
}

因为Category.dart是挂载到Tabs.dart中的,而在Tabs.dart中,已经有一个Scaffold组件和AppBar组件了,所以,继续添加顶部Tabs以后,就会有两个Scaffold组件和AppBar组件。

 

为了解决上面的问题,只需要将Tabs切换换个位置,移动到title所在的位置就可以了:

import 'package:flutter/material.dart';

class CategoryPage extends StatefulWidget {
CategoryPage({Key key}) : super(key: key); _CategoryPageState createState() => _CategoryPageState();
} class _CategoryPageState extends State<CategoryPage> {
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 4,
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.black26,
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child:TabBar(
            isScrollable: true, //多个Tab的时候,可以实现滑动和联动
                  indicatorColor:Colors.blue,
labelColor:Colors.blue,
unselectedLabelColor: Colors.white,
indicatorSize:TabBarIndicatorSize.label ,
tabs: <Widget>[
Tab(text: "热销"),
Tab(text: "推荐"),
Tab(text: "推荐"),
Tab(text: "推荐")
],
) ,
)
],
), ),
body:TabBarView(
children: <Widget>[
ListView(
children: <Widget>[
ListTile(title:Text("第一个tab")),
],
),
ListView(
children: <Widget>[
ListTile(title:Text("第二个tab")),
],
),
ListView(
children: <Widget>[
ListTile(title:Text("第三个tab")),
],
),
ListView(
children: <Widget>[
ListTile(title:Text("第四个tab")),
],
)
],
)
),
);
}
}

 

最新文章

  1. C#设计模式之工厂方法
  2. jquery点击元素之外触发事件
  3. mongkeyrunner实现循环随机输入值的方法
  4. 基于Metronic的Bootstrap开发框架经验总结(8)--框架功能总体界面介绍
  5. poj1637 Sightseeing tour
  6. Sublime text插件使用技巧
  7. Java 入门基础
  8. VS调试Ajax
  9. [转载] ubuntu Authentication failure
  10. cer证书签名验证
  11. 快速用Markdown排版一篇文章
  12. 【linux】之查看磁盘占用情况
  13. docker 搭建简易仓库registry
  14. UEditor上传自定义文件夹
  15. 两个1/x类的广义函数
  16. SyntaxHighlighter行号显示错误问题解决方案
  17. nRF52832-GPIOTE部分
  18. FastJson遇见的问题或项目实战中优化的问题,看源码都可以解决
  19. MongoDB之集合管理一
  20. systemd启动多实例

热门文章

  1. 类Runtime
  2. android window(三)lWindow添加流程
  3. layui框架中layer父子页面交互的方法分析
  4. Python笔记(三十)_python标准异常总结
  5. [9期]软WAF上传绕过+webshell免杀
  6. linux下的SSHD被连接端口修改
  7. 嵌入式软件工程师C语言经典笔试1
  8. 键盘按键KeyCode大全
  9. Bootstrap真的总是好的吗
  10. SQL Server 查找字符串中指定字符出现的次数