当前位置:网站首页>【Flutter -- 顶部导航栏】TabBarView 的基本使用
【Flutter -- 顶部导航栏】TabBarView 的基本使用
2022-07-20 11:09:00 【Kevin-Dev】
简介
Flutter 中用于快速实现顶部导航栏的组件库。
TabBar
Tab 页的 Title 控件,切换 Tab 页的入口,一般放到 AppBar 控件下使用,内部有**Title*属性。其子元素按水平横向排列布局,如果需要纵向排列,请使用 Column 或 ListView 控件包装一下。子元素为 Tab 类型的数组。TabBarView
Tab 页的内容容器,其内放置 Tab 页的主体内容。子元素可以是多个各种类型的控件。TabController
这是Tab页的控制器,用于定义Tab标签和内容页的坐标,还可配置标签页的切换动画效果等。
属性
Tab 的构造方法:
const TabBar({
Key? key,
required this.tabs, // 具体的 Tabs,需要我们创建
this.controller,
this.isScrollable = false, // 是否可以滑动
this.padding,
this.indicatorColor,// 指示器颜色,默认是高度为2的一条下划线
this.automaticIndicatorColorAdjustment = true,
this.indicatorWeight = 2.0,// 指示器高度
this.indicatorPadding = EdgeInsets.zero, //指示器padding
this.indicator, // 指示器
this.indicatorSize, // 指示器长度,有两个可选值,一个tab的长度,一个是label长度
this.labelColor, // 选中Tab文字颜色
this.labelStyle, // 选中Tab文字Style
this.labelPadding,
this.unselectedLabelColor, // 未选中Tab中文字颜色
this.unselectedLabelStyle, // 未选中Tab中文字style
this.mouseCursor,
this.onTap,
...
})
实例
1. 效果图
2. 代码
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({
Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
body: Center(
child: AppBarStatefulWidget(),
)
)
);
}
}
class AppBarStatefulWidget extends StatefulWidget {
const AppBarStatefulWidget({
Key? key}) : super(key: key);
@override
_AppBarStatefulWidget createState() => _AppBarStatefulWidget();
}
class _AppBarStatefulWidget extends State with SingleTickerProviderStateMixin {
final List<Tab> _tabs = <Tab>[
new Tab(text: '关注'),
new Tab(text: '推荐'),
new Tab(text: '视频'),
new Tab(text: '游戏'),
new Tab(text: '音乐'),
new Tab(text: '体育'),
new Tab(text: '生活'),
new Tab(text: '图片'),
];
var _tabController;
@override
void initState() {
_tabController = TabController(vsync: this, length: _tabs.length);
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: TabBarView(
controller: _tabController,
children: _tabs.map((Tab tab) {
return new Center(child: Text(tab.text.toString()));
}).toList(),
),
appBar: AppBar(
title: Text("TabBarView 的基本使用"),
centerTitle: true,
bottom: TabBar(
isScrollable: true,
labelColor: Colors.redAccent, // 选中的Widget颜色
indicatorColor:Colors.redAccent, // 选中的指示器颜色
labelStyle: new TextStyle(fontSize: 15.0),// 必须设置,设置 color 没用的,因为 labelColor 已经设置了
unselectedLabelColor: Colors.white,
unselectedLabelStyle: new TextStyle(
fontSize: 15.0), // 设置 color 没用的,因为unselectedLabelColor已经设置了
controller: _tabController,
// tabbar 必须设置 controller 否则报错
indicatorSize: TabBarIndicatorSize.label,
// 有 tab 和 label 两种
tabs: _tabs,
),
),
);
}
}
边栏推荐
- 网络 与vpc实验
- NAT 的几种应用场景
- 【Flutter -- 基础组件】按钮组件 - ElevatedButton、TextButton、OutlinedButton、IconButton
- 鸿蒙HarmonyOS 3官宣:7月27日正式发布;苹果2023年放缓部分团队招聘和支出;俄罗斯对谷歌重罚26亿元|极客头条...
- 笔记。。。。
- [kubernetes]- export after batch modifying the image tag
- Wallys/DR882/QCA9882/ AC/AN MiniPCIE/2 × 2.4GHz 2x5GHz MT7915 MT7975
- promise 基础使用
- (Note)CIE色度图
- C language integer type
猜你喜欢
加密市场中的14个心智模型
使用 LVS+Keepalived 实现 MySQL 双主复制负载均衡高可用
通信教程 | USB、HDMI、DP接口及速度
静态页面和动态页面的原理及区别详解
报表设计工具FastReport Online Designer V2022.1新功能全剖析
ACTF出题(dropper+master_of_dns)
“如今,99.9% 以上的代码都是垃圾!”
[the problem has been solved] - MySQL uses the same yaml and NFS shared storage to report an error
NC | chenghuan Institute zhuyongguan team - potential of marine plastic circles in the process of nitrogen geochemical cycle
MoCo V2:MoCo系列再升级
随机推荐
常用的函数式接口_Predicate接口_默认方法and和Predicate接口练习_集合接口筛选
Anacona环境太多??jupyter中如何查看自己当前在哪个环境
打造数据驱动型组织,成长型企业最需要什么?
共享文件夹时,出现'试图共享XXXXX…
笔记。。。。
Iron nails pierce the head, yogurt bottles "bite" the tongue... Guangdong fire: Summer children's safety can not be ignored
基于Tensorflow GPU2Ascend工具的TensorFlow网络模型迁移
[奇异吸引子]相关知识点
uni-app开发之HBuilderX连接夜神模拟器
如何下载视频文件
DDR 6 memory has been put into research and development
华人女婿小野三太成密西根大学首位亚裔校长,年薪超650万!
[the problem has been solved] - Jenkins traditional packaging and delivery process optimization
【LeetCode】2. 两数相加 - Go 语言题解
ECCV 2022 | 旷视提出半监督目标检测模型Dense Teacher,取得SOTA性能
国内外知名源码商城系统盘点
报表设计工具FastReport Online Designer V2022.1新功能全剖析
What can testers do when there is an online bug?
Huawei computer test: Student matrix
认清影响因子引发的是是非非,善待逆境中顽强崛起的国产期刊