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组件

PreferredSize可以改变appBar的高度
Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(50),
child: AppBar(
....
)
),
body: Test(),
)

自定义KeepAliveWrapper 缓存页面

AutomaticKeepAliveClientMixin 可以快速的实现页面缓存功能,但是通过混入的方式实现不是很优
雅, 所以我们有必要对AutomaticKeepAliveClientMixin 混入进行封装
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的更多相关文章

  1. AppBar 自定义顶部导航按钮 图标、颜色 以及 TabBar 定义顶部 Tab 切换

    一.Flutter AppBar 自定义顶部按钮图标.颜色 leading   在标题前面显示的一个控件,在首页通常显示应用的 logo:在其他界面通常显示为返回按钮 title  标题,通常显示为当 ...

  2. Flutter AppBar 自定义顶部导航按钮 图标、颜色 以及 TabBar 定义顶部 Tab 切换

    Flutter AppBar 自定义顶部按钮图 标.颜色 属性 描述 leading 在标题前面显示的一个控件,在首页通常显示应用 的 logo;在其他界面通常显示为返回按钮 title 标题,通常显 ...

  3. 5、Flutter 实现 ViewPager、bottomNavigationBar 界面切换

    1.前言 首先我们想一下,如果在 Android 中实现 布局切换,通常的思路是: 做一个 viewpager 一组 Fragment 每个 Fragment 绑定一个 xml 最后填充至 viewp ...

  4. ABP(现代ASP.NET样板开发框架)系列之23、ABP展现层——异常处理

    点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之23.ABP展现层——异常处理 ABP是“ASP.NET Boilerplate Project (ASP.NET ...

  5. 【译】Java、Kotlin、RN、Flutter 开发出来的 App 大小,你了解过吗?

    现在开发 App 的方式非常多,原生.ReactNative.Flutter 都是不错的选择.那你有没有关注过,使用不同的方式,编译生成的 Apk ,大小是否会有什么影响呢?本文就以一个最简单的 He ...

  6. 移动端跨平台方案对比:React Native、weex、Flutter

    跨平台一直是老生常谈的话题,cordova.ionic.react-native.weex.kotlin-native.flutter等跨平台框架百花齐放,颇有一股推倒原生开发者的势头. 为什么我们需 ...

  7. 最火移动端跨平台方案盘点:React Native、weex、Flutter

    1.前言 跨平台一直是老生常谈的话题,cordova.ionic.react-native.weex.kotlin-native.flutter等跨平台框架的百花齐放,颇有一股推倒原生开发者的势头. ...

  8. 23、自动装配-Aware注入Spring底层组件&原理

    23.自动装配-Aware注入Spring底层组件&原理 Aware 接口,提供了类似回调函数的功能 自定义组件想要使用Spring 容器底层的一些组件(Application Context ...

  9. 热门跨平台方案对比:WEEX、React Native、Flutter和PWA

    本文主要对WEEX.React Native.Flutter和PWA几大热门跨平台方案进行简单的介绍和对比.内容选自<WEEX跨平台开发实战> (WEEX项目负责人力荐,从入门到实战,教你 ...

  10. Flutter 原生TabBar切换标签页示例

    效果图: 代码如下: import 'package:flutter/material.dart'; class TabsTestPage extends StatefulWidget { _Tabs ...

随机推荐

  1. 如何使用Java创建数据透视表并导出为PDF

    摘要:本文由葡萄城技术团队原创并首发.转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 前言 数据透视分析是一种强大的工具,可以帮助我们从大量数据中提取有用信 ...

  2. go语言 包依赖管理-构建完整的依赖项目:目录结构及包的调用

    目录结构: <home>/ |-- greetings/ |-- hello/1.分别进入对应目录创建以上目录结构 //bash切换到用户主目录 cd $HOMEPAHT$ //bash新 ...

  3. linux文件、目录权限和所有者

    文件.目录权限和所有者 简介:用户对一个文件或目录具有访问权限,这些访问权限决定了谁能访问,以及如何范围这些文件和目录.通过设置权限可以限制或允许以下三种用户访问: 文件的用户所有者(属主) 文件的组 ...

  4. NPOI在EXCEL中插入图片和超链接

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. 计算机的数值转化与网络的IP地址分类与地址划分

    数值转换 数字系统由来 远古时代是没有数字系统非位置化数字系统: 罗马数字 (I-1.II-2.III-3.IV-4.V-5.VI-6.VII-7.VIII-8.IX-9.X-10) 位置话数字化系统 ...

  6. 关于 React 性能优化和数栈产品中的实践

    我们是袋鼠云数栈 UED 团队,致力于打造优秀的一站式数据中台产品.我们始终保持工匠精神,探索前端道路,为社区积累并传播经验价值. 本文作者:的卢 引入 在日常开发过程中,我们会使用很多性能优化的 A ...

  7. 业务出海、高效传输、动态加速,尽在云栖大会「CDN与边缘计算」专场

    2023杭州·云栖大会,即将热力来袭. 一场云计算盛会,500+前沿话题,3000+科技展品,与阿里云一起,共赴72小时的Tech沉浸之旅. 今日,「CDN与边缘计算」Tech专场,重磅议题抢先知晓! ...

  8. AttributeError: module 'sqlalchemy' has no attribute '__all__'

    升级组件 pip install --upgrade flask-sqlalchemy

  9. 将.View.dll文件反编译出来的*Views*.cs文件转换成.cshtml

    先使用反编译工具将.View.dll文件反编译放入文件夹,然后将文件夹整体复制进\src\viewcs2cshtml\viewcs2cshtml\bin\Debug\net6.0\viewcs 复制完 ...

  10. 《最新出炉》系列初窥篇-Python+Playwright自动化测试-31-JavaScript的调用执行-上篇

    1.简介 在做web自动化时,有些情况playwright的api无法完成以及无法应对,需要通过或者借助第三方手段比如js来完成实现,比如:去改变某些元素对象的属性或者进行一些特殊的操作,本文讲解pl ...