23、Flutter AppBar TabBar TabBarView
AppBar自定义顶部按钮图标、颜色

class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: IconButton(
//导航左侧图标
icon: const Icon(Icons.home_max),
onPressed: () {
print("左侧点击了左侧");
},
),
title: const Text("Flutter App"), //标题
centerTitle: true, // 设置标题居中
backgroundColor: Colors.yellow, //导航背景颜色
actions: [
//导航右侧图标 一个或多个
IconButton(
icon: const Icon(Icons.home),
onPressed: () {
print("右侧点击了右侧");
},
),
IconButton(
icon: const Icon(Icons.safety_check),
onPressed: () {
print("右侧点击了右侧");
},
),
],
),
backgroundColor: Colors.red, //主体背景颜色
drawer: const Drawer(
child: Text("左侧侧边栏"),
),
);
}
}

Flutter AppBar结合TabBar实现顶部Tab切换
TabBar常见属性:

Tabbar TabBarView实现类似头条顶部导航
class MyHomeApp extends StatefulWidget {
const MyHomeApp({super.key});
@override
State<MyHomeApp> createState() => _MyHomeAppState();
}
class _MyHomeAppState extends State<MyHomeApp>
with SingleTickerProviderStateMixin {
late TabController _tabController;
//生命周期函数:当组件初始化的时候就会触发
void initState() {
super.initState();
_tabController = TabController(length: 3, vsync: this);
_tabController.addListener(() {
if (_tabController.animation!.value == _tabController.index) {
print(_tabController.index); //获取点击或滑动页面的索引值
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.red, //导航背景颜色
bottom: TabBar(
isScrollable:true, //是否可滚动
indicatorColor:Colors.yellow, //指示器颜色
indicatorWeight:5, //指示器高度
indicatorPadding:EdgeInsets.all(10), //底部指示器的Padding
indicator:BoxDecoration( //指示器decoration,例如边框等
color: Colors.blue,
borderRadius: BorderRadius.circular(10) //圆角
),
labelColor: Colors.black, //选中label颜色
unselectedLabelColor:Colors.yellow, //未选中label颜色
labelStyle: const TextStyle(
fontSize: 20
),//选中label的Style
unselectedLabelStyle:const TextStyle(
fontSize: 15
),//未选中label的Style
indicatorSize:TabBarIndicatorSize.label, //指示器大小计算方式,TabBarIndicatorSize.label跟文字等宽,TabBarIndicatorSize.tab跟每个tab等宽
controller: _tabController, //注意: 配置controller需要去掉TabBar上const
tabs: const [
Tab(
child: Text("关注"),
),
Tab(
child: Text("热门"),
),
Tab(
child: Text("视频"),
),
],
),
),
body: TabBarView(
controller: _tabController,
children: [
const Text("第一个关注"),
ListView(
children: const [
ListTile(
title: Text("第一个热门"),
),
ListTile(
title: Text("第二个热门"),
),
ListTile(
title: Text("第三个热门"),
),
],
),
const ListTile(title: Text("第一个视频")),
],
),
// backgroundColor: Colors.red, //主体背景颜色
);
}
}
BottomNavigationBar 的页面中使用Tabbar
class GrownPage extends StatefulWidget {
const GrownPage({super.key});
@override
State<GrownPage> createState() => _GrownPageState();
}
class _GrownPageState extends State<GrownPage>
with SingleTickerProviderStateMixin {
late TabController _tabController;
//生命周期函数:当组件初始化的时候就会触发
void initState() {
super.initState();
_tabController = TabController(length: 3, vsync: this);
_tabController.addListener(() {
if (_tabController.animation!.value == _tabController.index) {
print(_tabController.index); //获取点击或滑动页面的索引值
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: TabBar(
isScrollable: true, //是否可滚动
controller: _tabController, //注意: 配置controller需要去掉TabBar上const
tabs: const [
Tab(
child: Text("我的关注"),
),
Tab(
child: Text("热门"),
),
Tab(
child: Text("视频"),
),
],
),
centerTitle: true, //是否居中
backgroundColor: Color.fromARGB(255, 46, 233, 90), //导航背景颜色
),
body: TabBarView(
controller: _tabController,
children: [
const Text("第一个成人关注"),
ListView(
children: const [
ListTile(
title: Text("第一个成人热门"),
),
ListTile(
title: Text("第二个热门"),
),
ListTile(
title: Text("第三个成人热门"),
),
],
),
const ListTile(title: Text("第一个成人视频")),
],
),
// backgroundColor: Colors.red, //主体背景颜色
);
}
}
preferredSize组件
Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(50),
child: AppBar(
....
)
),
body: Test(),
)
自定义KeepAliveWrapper 缓存页面
import 'package:flutter/material.dart';
class KeepAliveWrapper extends StatefulWidget {
const KeepAliveWrapper(
{Key? key, @required this.child, this.keepAlive = true})
: super(key: key);
final Widget? child;
final bool keepAlive;
@override
State<KeepAliveWrapper> createState() => _KeepAliveWrapperState();
}
class _KeepAliveWrapperState extends State<KeepAliveWrapper>
with AutomaticKeepAliveClientMixin {
@override
Widget build(BuildContext context) {
return widget.child!;
}
@override
bool get wantKeepAlive => widget.keepAlive;
@override
void didUpdateWidget(covariant KeepAliveWrapper oldWidget) {
if (oldWidget.keepAlive != widget.keepAlive) {
// keepAlive 状态需要更新,实现在 AutomaticKeepAliveClientMixin 中
updateKeepAlive();
}
super.didUpdateWidget(oldWidget);
}
}
监听TabController改变事件
//方法一
//生命周期函数:当组件初始化的时候就会触发
void initState() {
super.initState();
_tabController = TabController(length: 8, vsync: this);
_tabController.addListener(() {
print(_tabController.index); //获取点击或滑动页面的索引值 (两次)
if (_tabController.animation!.value == _tabController.index) {
print(_tabController.index); ////获取点击或滑动页面的索引值(一次)
}
});
} //方法二
controller: _tabController, //注意: 配置controller需要去掉TabBar上const
onTap: (index){ //只能监听点击事件;没法点击滑动
print("点击事件————————————————————:$index");
},
...
23、Flutter AppBar TabBar TabBarView的更多相关文章
- AppBar 自定义顶部导航按钮 图标、颜色 以及 TabBar 定义顶部 Tab 切换
一.Flutter AppBar 自定义顶部按钮图标.颜色 leading 在标题前面显示的一个控件,在首页通常显示应用的 logo:在其他界面通常显示为返回按钮 title 标题,通常显示为当 ...
- Flutter AppBar 自定义顶部导航按钮 图标、颜色 以及 TabBar 定义顶部 Tab 切换
Flutter AppBar 自定义顶部按钮图 标.颜色 属性 描述 leading 在标题前面显示的一个控件,在首页通常显示应用 的 logo;在其他界面通常显示为返回按钮 title 标题,通常显 ...
- 5、Flutter 实现 ViewPager、bottomNavigationBar 界面切换
1.前言 首先我们想一下,如果在 Android 中实现 布局切换,通常的思路是: 做一个 viewpager 一组 Fragment 每个 Fragment 绑定一个 xml 最后填充至 viewp ...
- ABP(现代ASP.NET样板开发框架)系列之23、ABP展现层——异常处理
点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之23.ABP展现层——异常处理 ABP是“ASP.NET Boilerplate Project (ASP.NET ...
- 【译】Java、Kotlin、RN、Flutter 开发出来的 App 大小,你了解过吗?
现在开发 App 的方式非常多,原生.ReactNative.Flutter 都是不错的选择.那你有没有关注过,使用不同的方式,编译生成的 Apk ,大小是否会有什么影响呢?本文就以一个最简单的 He ...
- 移动端跨平台方案对比:React Native、weex、Flutter
跨平台一直是老生常谈的话题,cordova.ionic.react-native.weex.kotlin-native.flutter等跨平台框架百花齐放,颇有一股推倒原生开发者的势头. 为什么我们需 ...
- 最火移动端跨平台方案盘点:React Native、weex、Flutter
1.前言 跨平台一直是老生常谈的话题,cordova.ionic.react-native.weex.kotlin-native.flutter等跨平台框架的百花齐放,颇有一股推倒原生开发者的势头. ...
- 23、自动装配-Aware注入Spring底层组件&原理
23.自动装配-Aware注入Spring底层组件&原理 Aware 接口,提供了类似回调函数的功能 自定义组件想要使用Spring 容器底层的一些组件(Application Context ...
- 热门跨平台方案对比:WEEX、React Native、Flutter和PWA
本文主要对WEEX.React Native.Flutter和PWA几大热门跨平台方案进行简单的介绍和对比.内容选自<WEEX跨平台开发实战> (WEEX项目负责人力荐,从入门到实战,教你 ...
- Flutter 原生TabBar切换标签页示例
效果图: 代码如下: import 'package:flutter/material.dart'; class TabsTestPage extends StatefulWidget { _Tabs ...
随机推荐
- Error in v-on handler: “TypeError: _user.default is not a function“
碰到这个问题一开始以为是方法名重复了,后来检查了一遍也没发现方法名或者属性名重复然后发现是 这个导入方法时没加{}的问题. , 无语.
- AI图形算法的应用之一:通过图片模板对比发现油田漏油
最近研究了一下OPENCV的图像算法,开发了一个小应用. 可以通过图像和模板进行对比,发现油田或其他作业区漏油. 直接上效果,模板如下 自己模拟了一个漏油的现场图片,如下 通过图形化算法,找到漏油点, ...
- 网格布局grid
起因 昨天面试的时候,出了一道面试题,模拟面试公司的列表的元素宽度伸缩变化,根据屏幕大小的不同,一行上放置最多的元素,元素宽度不固定,间距固定,可换行,靠左对齐,当时猜出来用flexjs监听resiz ...
- CF1333A [Little Artem]
Problem 题目简述 给你一个 \(n \times m\) 的方格,构造一个方案,使得方案中 \(B = W + 1\). \(B\):相邻的格子有至少一个白色格子的黑色格子的个数. \(W\) ...
- MyBatis拦截器优雅实现数据脱敏
背景 现代网络环境中,敏感数据的处理是至关重要的.敏感数据包括个人身份信息.银行账号.手机号码等,泄露这些数据可能导致用户隐私泄露.财产损失等严重后果.因此,对敏感数据进行脱敏处理是一种必要的安全措施 ...
- go中的内存逃逸
内存逃逸(memory escape)是指在编写 Go 代码时,某些变量或数据的生命周期超出了其原始作用域的情况.当变量逃逸到函数外部或持续存在于堆上时,会导致内存分配的开销,从而对程序的性能产生负面 ...
- 《最新出炉》系列初窥篇-Python+Playwright自动化测试-26-处理单选和多选按钮-下篇
1.简介 今天这一篇宏哥主要是讲解一下,如何使用Playwright来遍历单选和多选按钮.大致两部分内容:一部分是宏哥在本地弄的一个小demo,另一部分,宏哥是利用JQueryUI网站里的单选和多选按 ...
- Verilog语法基础
FPGA语法 逻辑值: 0:逻辑低电平,条件为假. 1:逻辑高电平,条件为真. z:高阻态,无驱动 x:未知逻辑电平,这既不是0也不是1,只是一个不稳定的状态. 关键字: module:表示模块的开始 ...
- VUE同级组件之前方法调用
实现:Index.vue页面调用nav.vue页面里的getLeftMenu()方法 一.首先先建一个公共文件,命名eventBus.js,内空为: import Vue from 'vue'expo ...
- WPF DataGrid真正意义上开箱即用的原生可动态更新全选状态的DataGridCheckBox
本文由 飞羽流星(Flithor/毛茸茸松鼠先生/Squirrel.Downy)原创,欢迎分享转载,但禁止以原创二次发布原位地址:https://www.cnblogs.com/Flithor/p/1 ...