Flutter 顶部导航 AppBar 的实现

Flutter 实现 AppBar 功能


我们知道,Android 中 Material 布局。只要是在 Material 中使用 AppBar 控件,即可实现 导航条的功能。

AppBar 的属性

想要使用 AppBar 这个 Widget 组件,首先要了解它的属性:

  AppBar({
    Key key,
    this.leading,//导航栏左侧的 Widget,例如常见的 back 图标。
    this.automaticallyImplyLeading = true,//如果 leading!=null,该属性不生效;如果 leading==null 并且该属性为 true,左侧 leading 区域留白(占位);如果 leading==null 且该属性为 false,左侧 leading 区域扩展给 title 区域使用。
    this.title,//导航栏的标题
    this.actions,//List<Widget>类型,导航栏右侧要展示的一组 widgets
    this.flexibleSpace,
    this.bottom,//底部需要展示的widget
    this.elevation,//阴影高度,默认为4
    this.shadowColor,
    this.shape,//ShapeBorder 类型,表示描边形状
    this.backgroundColor,//背景色
    this.brightness,//Brightness类型,表示当前 appbar 主题是亮或暗色调,有 dark 和 light 两个值
    this.iconTheme,//图标主题,可影响包括leading、title、actions中icon
    this.actionsIconTheme,//导航条上右侧widgets主题
    this.textTheme,//导航条上文字主题
    this.primary = true,//true时,appBar会以系统状态栏高度为间距显示在下方;false时,会和状态栏重叠,相当于全屏显示。
    this.centerTitle,//标题是否居中显示
    this.excludeHeaderSemantics = false,
    this.titleSpacing = NavigationToolbar.kMiddleSpacing,//title 区域水平方向与 leading 和 actions 的间距(padding)
    this.toolbarOpacity = 1.0,//toolbar 区域透明度
    this.bottomOpacity = 1.0,//bottom 区域透明度
    this.toolbarHeight,//toolbar 高度
  }) 

AppBar 的使用

AppBar 使用非常简单,只需要按照构造参数传递所需参数即可。

代码如下:

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("这是一个标题"),
        leading: new IconButton(
          icon: new Icon(Icons.arrow_back),
          onPressed: () => Navigator.pop(context),
        ),
        actions: [new IconButton(icon: new Icon(Icons.done), onPressed: () => updateDataAndClose(context))],
        centerTitle: true,
        backgroundColor: Colors.lightGreen,
        toolbarHeight: 60,
      ),
      body: ……
    );
  }

**PS:更多精彩内容,请查看 --> 《Flutter 开发》
**PS:更多精彩内容,请查看 --> 《Flutter 开发》
**PS:更多精彩内容,请查看 --> 《Flutter 开发》

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卜大爷

觉得不错的可以给我加油哦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值