https://api.flutter.dev/flutter/material/AppBar-class.html
AppBar是一个Material风格的导航栏,通过它可以设置导航栏标题、导航栏菜单、导航栏底部的Tab标题等。

结构图

AppBar - 图1

定义

  1. AppBar({
  2. Key key,
  3. this.leading, //导航栏最左侧Widget,常见为抽屉菜单按钮或返回按钮。
  4. this.automaticallyImplyLeading = true, //如果leading为null,是否自动实现默认的leading按钮
  5. this.title, // 页面标题
  6. this.actions, // 导航栏右侧菜单
  7. this.flexibleSpace,
  8. this.bottom, //导航栏底部菜单,通常为Tab按钮组, bottom: PreferredSize(child: Text('ss'))
  9. this.elevation, //导航栏阴影
  10. this.shadowColor,
  11. this.shape,
  12. this.backgroundColor, //导航背景色
  13. this.brightness,
  14. this.iconTheme, //图标样式
  15. this.actionsIconTheme,
  16. this.textTheme, //文字样式
  17. this.primary = true,
  18. this.centerTitle, //标题是否居中
  19. this.excludeHeaderSemantics = false,
  20. this.titleSpacing = NavigationToolbar.kMiddleSpacing,
  21. this.toolbarOpacity = 1.0,
  22. this.bottomOpacity = 1.0,
  23. this.toolbarHeight,
  24. this.leadingWidth,
  25. })

AppBar高度的全局变量

  1. /// The height of the toolbar component of the [AppBar].
  2. const double kToolbarHeight = 56.0;

常用参数

leading 左侧图标

通常设置返回图标。
如果leading属性未设置,Scaffold也未设置Drawer,此时如果有前一个路由,则默认显示BackButton。
image.png

  1. appBar: AppBar(
  2. leading: BackButton(),
  3. title: Text('hello flutter'),
  4. ),

如果leading属性未设置,且Scaffold设置了Drawer则显示打开Drawer的菜单图标。
image.png

  1. appBar: AppBar(
  2. title: Text('hello flutter'),
  3. ),
  4. drawer: Text('左抽屉'),

如果我们想自定义菜单图标打开抽屉,也可以手动来设置 leading 。
AppBar - 图4

  1. appBar: AppBar(
  2. leading: Builder(builder: (context) {
  3. return IconButton(
  4. icon: Icon(Icons.dashboard, color: Colors.white), //自定义图标
  5. onPressed: () {
  6. Scaffold.of(context).openDrawer(); // 打开抽屉菜单
  7. },
  8. );
  9. }),
  10. )

代码中打开抽屉菜单的方法在ScaffoldState中,通过Scaffold.of(context)可以获取父级最近的Scaffold 组件的State对象。

  1. // 查找父级最近的Scaffold对应的ScaffoldState对象
  2. // ScaffoldState _state = context.findAncestorStateOfType<ScaffoldState>();
  3. // 或
  4. var _state = Scaffold.of(context);

通过automaticallyImplyLeading属性改变其行为,设置为false将不会自动设置控件。

  1. AppBar(
  2. automaticallyImplyLeading: false,
  3. )

actions 右侧图标组

image.png

  1. appBar: AppBar(
  2. actions: [
  3. Icon(Icons.search),
  4. IconButton(
  5. icon: Icon(Icons.settings),
  6. onPressed: () {
  7. print('1');
  8. },
  9. ),
  10. ],
  11. ),

title 标题

  1. appBar: AppBar(
  2. title: Text('hello flutter'),
  3. centerTitle: true, //标题是否居中
  4. ),

flexibleSpace

flexibleSpace属性在AppBar中一般用不到,此控件和AppBar的height保持一致,只有在改变AppBar的尺寸的时候才会出现效果,因此一般用在SliverAppBar中。
SliverAppBar: https://www.yuque.com/zhuchaoyang/tnyvrp/sg1pql#O6u8K

bottom

通常请求下设置TabBar。
asdf.gif

  1. class _TestPageState extends State<TestPage> with SingleTickerProviderStateMixin {
  2. @override
  3. Widget build(BuildContext context) {
  4. return Scaffold(
  5. // appBar: EmptyNull.appBar(),
  6. appBar: AppBar(
  7. title: Text('hello flutter'),
  8. bottom: TabBar(
  9. tabs: [
  10. Text('语文'),
  11. Text('数学'),
  12. Text('英语'),
  13. Text('体育'),
  14. Text('音乐'),
  15. ],
  16. controller: TabController(length: 5, vsync: this),
  17. ),
  18. ),
  19. );
  20. }
  21. }

设置阴影、形状、背景颜色。

image.png

  1. appBar: AppBar(
  2. title: Text('hello flutter'),
  3. elevation: 10,
  4. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
  5. backgroundColor: Colors.red,
  6. ),

设置icon样式及文字样式

  1. AppBar(
  2. iconTheme:IconThemeData(size: 24),
  3. actionsIconTheme: IconThemeData(size: 24),
  4. textTheme: TextTheme(title: TextStyle(color: Colors.red)),
  5. title: Text('hello flutter'),
  6. )

appBar 常用子类

AppBar - 图8

AppBar 一个设计应用程序栏。
TabBar 显示水平行标签的选项
通常创建为AppBarAppBar.bottom部分并与TabBarView结合使用。
CupertinoTabBar iOS风格的底部导航标签栏
CupertinoNavigationBar iOS风格的导航栏。
导航栏是一个工具栏,最小程度上由工具栏中间的小部件组成,通常是页面标题。
它还支持中间窗口小部件之前和之后的前导尾随窗口 小部件,同时保持中间窗口小部件居中。
PreferredSize 自定义导航栏使用

PreferredSize

设置appBar的高度

使用脚手架Scaffold可以设置AppBar,想要设置高度,在AppBar外包一层PreferredSize,设置preferredSize的属性为想要的高度即可。

  1. Scaffold(
  2. appBar: PreferredSize(
  3. child: AppBar(),
  4. preferredSize: Size.fromHeight(MediaQuery.of(context).size.height * 0.07))
  5. );

应用:Flutter去掉AppBar避免body溢出到状态栏

没有AppBar的Flutter,如果不在Scaffold中使用AppBar,会发现默认是沉浸式。
image.png

  1. return Scaffold(
  2. appBar: PreferredSize(
  3. //preferredSize: Size.fromHeight(MediaQuery.of(context).size.height * 0.07),
  4. preferredSize: Size.fromHeight(MediaQuery.of(context).size.padding.top),
  5. child: SafeArea(
  6. top: true,
  7. child: Offstage(),
  8. ),
  9. ),
  10. body: Text('点击我跳转api-demo'),
  11. );