• Flutter组件--AppBar相关属性


    AppBar介绍

     

    AppBar是基于Material Design设计风格的应用栏,一般使用在Scaffold内部,作为顶部导航栏。

    为什么需要AppBar

    1、因为导航栏里面一般由左侧功能键(返回键、菜单键)、标题、右侧功能键组成,而AppBar里面内置封装了这些组件,使用起来非常方便。

    2、可以做一些特殊的导航栏,比如可滚动的导航栏。

    3、根据环境 MediaQuery 的填充插入内容,以避免系统  UI 入侵。

    AppBar的效果图​​​​​​​


    ​​​​​​​

     

    AppBar属性和说明

    序列号字段属性描述
    1keyKey当组件在组件树中移动时使用Key可以保持组件之前状态
    2leadingWidget通常情况下返回一个返回键(IconButton)
    3leadingWidthdouble左侧leading的宽度,默认56
    4automaticallyImplyLeadingbool和leading配合使用,如果为true并且leading为空的情况下,会自动配置返回键
    5titleWidget导航栏的标题
    6centerTitlebool标题是否居中,不同操作系统默认显示位置不一样
    7actionsList一个Widget列表
    8bottomPreferredSizeWidget出现在导航栏底部的控件
    9elevationdouble控制导航栏下方阴影的大小
    10shadowColorColor控制导航栏下方阴影的颜色
    11shapeShapeBorder导航栏的形状以及阴影
    12backgroundColorColor导航栏的背景颜色
    13foregroundColor(只有当14属性设置为flase的时候,该属性才会生效)Color导航栏中文本和图标的颜色
    14backwardsCompatibilitybool与foregroundColor配合使用
    15iconThemeIconThemeData导航栏图标的颜色、透明度、大小的配置
    16actionsIconThemeIconThemeData导航栏右侧图标的颜色、透明度、大小的配置
    17textThemeTextTheme导航栏文本的排版样式
    18primarybool导航栏是否显示在屏幕顶部
    19excludeHeaderSemanticsbool标题是否应该用 [Semantics] 包裹,默认false
    20titleSpacingdoubletitle内容的间距
    21toolbarOpacitydouble导航栏的透明度
    22bottomOpacitydouble导航栏底部的透明度
    23toolbarHeightdouble导航栏的高度,默认kToolbarHeight
    24toolbarTextStyleTextStyle导航栏图标的颜色
    25titleTextStyleTextStyle导航栏标题的默认颜色
    26flexibleSpaceWidget堆叠在工具栏和选项卡栏的后面
    27systemOverlayStyleSystemUiOverlayStyle叠加层的样式
    28brightnessBrightness导航栏的亮度,改属性已废弃,用systemOverlayStyle代替

    AppBar详细使用

    1.key

    key 是用来作为Widget 、Element 和 SemanticsNode 的标识,当组件在组件树中移动时使用Key可以保持组件之前状态

    1. GlobalKey _appBarKey = GlobalKey();
    2. @override
    3. Widget build(BuildContext context) {
    4. return Scaffold(
    5. appBar: AppBar(
    6. key: _appBarKey,
    7. ),
    8. );
    9. }

    2.leading

    appBar 左侧显示的一个 Widget,一般显示返回键 Icon 或 IconButton

    1. @override
    2. Widget build(BuildContext context) {
    3. return Scaffold(
    4. appBar: AppBar(
    5. leading: IconButton(
    6. onPressed: (){
    7. Navigator.pop(context);
    8. },
    9. icon: Icon(Icons.arrow_back_sharp, color: Colors.white,)
    10. ),
    11. ),
    12. );
    13. }

    3.leadingWidth

    左侧leading的宽度,默认56

    1. @override
    2. Widget build(BuildContext context) {
    3. return Scaffold(
    4. appBar: AppBar(
    5. leading: IconButton(
    6. onPressed: (){
    7. Navigator.pop(context);
    8. },
    9. icon: Icon(Icons.arrow_back_sharp, color: Colors.white,)
    10. ),
    11. leadingWidth: 60,
    12. ),
    13. );
    14. }

    4.automaticallyImplyLeading

    leading 未配置时,在二级页面下会自动展示一个返回键,默认值为 true

    1. @override
    2. Widget build(BuildContext context) {
    3. return Scaffold(
    4. appBar: AppBar(
    5. automaticallyImplyLeading: false,
    6. ),
    7. );
    8. }

    5.title

    导航栏的标题,一般是显示当前页面的标题文字

    1. @override
    2. Widget build(BuildContext context) {
    3. return Scaffold(
    4. appBar: AppBar(
    5. title: Text("AppBarExample"),
    6. ),
    7. );
    8. }

    6.centerTitle

    标题是否居中,不同操作系统默认显示位置不一样,安卓默认显示在左侧,苹果默认显示在中间

    1. @override
    2. Widget build(BuildContext context) {
    3. return Scaffold(
    4. appBar: AppBar(
    5. title: Text("AppBarExample"),
    6. centerTitle: true,
    7. ),
    8. );
    9. }

    7.actions

    一个 Widget 列表,代表 Toolbar 中所显示的菜单,对于常用的菜单,通常使用 IconButton 来表示;对于不常用的菜单通常使用 PopupMenuButton 来显示为三个点,点击后弹出二级菜单

    1. @override
    2. Widget build(BuildContext context) {
    3. return Scaffold(
    4. appBar: AppBar(
    5. actions: [
    6. IconButton(
    7. onPressed: (){
    8. },
    9. tooltip: "扫一扫",
    10. icon: Icon(Icons.qr_code_scanner),
    11. ),
    12. IconButton(
    13. onPressed: (){
    14. },
    15. tooltip: "添加",
    16. icon: Icon(Icons.add),
    17. )
    18. ],
    19. ),
    20. );
    21. }

    8.bottom

    出现在应用栏底部的控件,一般是 TabBar

    1. import 'package:flutter/material.dart';
    2. class AppBarExample extends StatefulWidget {
    3. @override
    4. _AppBarExampleState createState() => _AppBarExampleState();
    5. }
    6. class _AppBarExampleState extends State<AppBarExample> with SingleTickerProviderStateMixin{
    7. TabController _tabController;
    8. @override
    9. void initState() {
    10. // TODO: implement initState
    11. super.initState();
    12. _tabController = TabController(length: 2, vsync: this);
    13. }
    14. @override
    15. Widget build(BuildContext context) {
    16. return Scaffold(
    17. appBar: AppBar(
    18. bottom: TabBar(
    19. controller: _tabController,
    20. tabs: [
    21. Tab(text: "火车", icon: Icon(Icons.bus_alert),),
    22. Tab(text: "汽车", icon: Icon(Icons.bus_alert),)
    23. ],
    24. ),
    25. ),
    26. );
    27. }
    28. }

    9.elevation

    控制应用栏下方阴影的大小,这个值不能是一个负值

    1. @override
    2. Widget build(BuildContext context) {
    3. return Scaffold(
    4. appBar: AppBar(
    5. elevation: 10,
    6. ),
    7. );
    8. }

    10.shadowColor

    控制导航栏下方阴影的颜色

    1. @override
    2. Widget build(BuildContext context) {
    3. return Scaffold(
    4. appBar: AppBar(
    5. elevation: 10,
    6. shadowColor: Colors.green,
    7. ),
    8. );
    9. }

    11.shape

    导航栏的形状以及阴影

    1. @override
    2. Widget build(BuildContext context) {
    3. return Scaffold(
    4. appBar: AppBar(
    5. elevation: 10,
    6. shadowColor: Colors.green,
    7. shape: RoundedRectangleBorder(
    8. side: BorderSide(
    9. color: Colors.red,
    10. width: 5
    11. )
    12. )
    13. ),
    14. );
    15. }

    12.backgroundColor

    导航栏的背景颜色

    1. @override
    2. Widget build(BuildContext context) {
    3. return Scaffold(
    4. appBar: AppBar(
    5. backgroundColor: Colors.orange,
    6. ),
    7. );
    8. }

    13.foregroundColor

    导航栏中文本和图标的颜色

    1. @override
    2. Widget build(BuildContext context) {
    3. return Scaffold(
    4. appBar: AppBar(
    5. foregroundColor: Colors.black,
    6. ),
    7. );
    8. }

    14.backwardsCompatibility

    与foregroundColor配合使用

    1. @override
    2. Widget build(BuildContext context) {
    3. return Scaffold(
    4. appBar: AppBar(
    5. foregroundColor: Colors.black,
    6. backwardsCompatibility: true,
    7. ),
    8. );
    9. }

    15.iconTheme

    导航栏图标的颜色、透明度、大小的配置

    1. @override
    2. Widget build(BuildContext context) {
    3. return Scaffold(
    4. appBar: AppBar(
    5. leading: IconButton(
    6. onPressed: (){
    7. Navigator.pop(context);
    8. },
    9. icon: Icon(Icons.arrow_back_sharp, color: Colors.white,)
    10. ),
    11. iconTheme: IconThemeData(
    12. color: Colors.orange,
    13. opacity: 1,
    14. size: 50
    15. ),
    16. ),
    17. );
    18. }

    16.actionsIconTheme

    导航栏右侧图标的颜色、透明度、大小的配置

    1. @override
    2. Widget build(BuildContext context) {
    3. return Scaffold(
    4. appBar: AppBar(
    5. actions: [
    6. IconButton(
    7. onPressed: (){
    8. },
    9. tooltip: "扫一扫",
    10. icon: Icon(Icons.qr_code_scanner),
    11. ),
    12. IconButton(
    13. onPressed: (){
    14. },
    15. tooltip: "添加",
    16. icon: Icon(Icons.add),
    17. )
    18. ],
    19. actionsIconTheme: IconThemeData(
    20. color: Colors.purple,
    21. ),
    22. ),
    23. );
    24. }

    17.textTheme

    导航栏文本的排版样式,默认使用ThemeData.primaryTextTheme

    18.primary

    导航栏是否显示在屏幕顶部

    1. @override
    2. Widget build(BuildContext context) {
    3. return Scaffold(
    4. appBar: AppBar(
    5. backrogoundColor: Colors.black,
    6. primary: true,
    7. ),
    8. );
    9. }

    19.excludeHeaderSemantics

    标题是否应该用 [Semantics] 包裹,默认false

    1. @override
    2. Widget build(BuildContext context) {
    3. return Scaffold(
    4. appBar: AppBar(
    5. backrogoundColor: Colors.black,
    6. primary: true,
    7. excludeHeaderSemantics: true,
    8. ),
    9. );
    10. }

    20.titleSpacing

    标题内容的间距,如果为0,将占用全部空间

    1. @override
    2. Widget build(BuildContext context) {
    3. return Scaffold(
    4. appBar: AppBar(
    5. title: Text("AppBarExample"),
    6. centerTitle: true,
    7. titleSpacing: 0,
    8. ),
    9. );
    10. }

    21.toolbarOpacity

    导航栏的透明度

    1. @override
    2. Widget build(BuildContext context) {
    3. return Scaffold(
    4. appBar: AppBar(
    5. backrogoundColor: Colors.black,
    6. toolbarOpacity: 0.5,
    7. ),
    8. );
    9. }

    22.bottomOpacity

    导航栏底部的透明度

    1. import 'package:flutter/material.dart';
    2. class AppBarExample extends StatefulWidget {
    3. @override
    4. _AppBarExampleState createState() => _AppBarExampleState();
    5. }
    6. class _AppBarExampleState extends State<AppBarExample> with SingleTickerProviderStateMixin{
    7. TabController _tabController;
    8. @override
    9. void initState() {
    10. // TODO: implement initState
    11. super.initState();
    12. _tabController = TabController(length: 2, vsync: this);
    13. }
    14. @override
    15. Widget build(BuildContext context) {
    16. return Scaffold(
    17. appBar: AppBar(
    18. bottom: TabBar(
    19. controller: _tabController,
    20. tabs: [
    21. Tab(text: "火车", icon: Icon(Icons.bus_alert),),
    22. Tab(text: "汽车", icon: Icon(Icons.bus_alert),)
    23. ],
    24. ),
    25. bottomOpacity: 0.5,
    26. ),
    27. );
    28. }
    29. }

    23.toolbarHeight

    导航栏的高度,默认kToolbarHeight

    1. @override
    2. Widget build(BuildContext context) {
    3. return Scaffold(
    4. appBar: AppBar(
    5. backrogoundColor: Colors.black,
    6. toolbarHeight: 200,
    7. ),
    8. );
    9. }

    24.toolbarTextStyle

    导航栏图标的颜色

    1. @override
    2. Widget build(BuildContext context) {
    3. return Scaffold(
    4. appBar: AppBar(
    5. leading: IconButton(
    6. onPressed: (){
    7. Navigator.pop(context);
    8. },
    9. icon: Icon(Icons.arrow_back_sharp, color: Colors.white,)
    10. ),
    11. toolbarTextStyle: TextStyle(
    12. color: Colors.black
    13. ),
    14. ),
    15. );
    16. }

    25.titleTextStyle

    导航栏标题的默认颜色

    1. @override
    2. Widget build(BuildContext context) {
    3. return Scaffold(
    4. appBar: AppBar(
    5. title: Text("AppBarExample"),
    6. centerTitle: true,
    7. titleSpacing: 0,
    8. titleTextStyle: TextStyle(
    9. color: Colors.red
    10. ),
    11. ),
    12. );
    13. }

    26.flexibleSpace,systemOverlayStyle,brightness

    flexibleSpace 以及 systemOverlayStyle 一般都是在配合 SliverAppBar 使用的,这里不做过多的描述。而 brightness 已经废弃,用 systemOverlayStyle 代替

    总结

    以上是针对 AppBar 的所有使用方法,最常用的属有leadingtitleactionscenterTitlebottombackgroundColor,其他属性都是在特定的情况才会使用。

  • 相关阅读:
    Windows下载AOSP
    11.(vue3.x+vite)组件间通信方式之ref与$parent、$children
    开源.NetCore通用工具库Xmtool使用连载 - OSS文件上传篇
    使用数学的力量来简化多级比较
    算法与数据结构 --- 遍历二叉树和线索二叉树
    cs夏令营经验分享
    【虹科案例】基于3D相机组装家具
    【2023.10.25练习】数据库-函数2
    Mish-撼动深度学习ReLU激活函数的新继任者
    idea怎么将克隆的代码上传到自己的gitee(保姆级教程)
  • 原文地址:https://blog.csdn.net/eastWind1101/article/details/127960431