Flutter入門(14):Flutter 組件之 AppBar 詳解

1. 基本介紹

AppBar 提供了常見的導(dǎo)航條功能赡突。

2. 示例代碼

代碼下載地址裳涛。如果對你有幫助的話記得給個關(guān)注,代碼會根據(jù)我的 Flutter 專題不斷更新立美。

3. AppBar 屬性

AppBar屬性 介紹
leading 可以定制左上角按鈕
automaticallyImplyLeading 是否自動導(dǎo)入左上角按鈕,默認(rèn)為 true,例如默認(rèn)導(dǎo)入返回按鈕
title AppBar 標(biāo)題
actions 右上角功能按鈕汪拥,可自定義
flexibleSpace 可折疊的應(yīng)用欄,在改變 appBar 的 size 時有效果
bottom AppBar下方懸浮欄篙耗,可以看下文圖片
elevation 陰影高度迫筑,默認(rèn)為4.0
shadowColor 陰影顏色
shape 陰影 ShapeBorder
backgroundColor AppBar 背景色
brightness Brightness.dark 和 Brightness.light,改變上方電池宗弯,時間等狀態(tài)欄顏色
iconTheme 所有 icon 的主題
actionsIconTheme actions 里的所有 icon 主題
textTheme text 主題
primary AppBar 是否在整個屏幕最上方脯燃,為 true 時,距離 AppBar 貼合狀態(tài)欄下方蒙保,false 時辕棚,貼合整個屏幕最上方
centerTitle title 是否居中
excludeHeaderSemantics Whether the title should be wrapped with header [Semantics]. 默認(rèn)為false,沒太大用
titleSpacing title 距離左側(cè)偏移量
toolbarOpacity AppBar 透明度
bottomOpacity bottom 透明度
toolbarHeight AppBar 高度

4. AppBar 組件

4.1 容器創(chuàng)建

優(yōu)雅的編程邓厕,首先創(chuàng)建一個 appbar.dart 文件逝嚎。

import 'package:flutter/material.dart';

class FMAppBarVC extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: _appBar(),
    );
  }

  AppBar _appBar() {
    return AppBar(
      title: Text(
        'AppBar',
        style: TextStyle(
          color: Colors.red,
        ),
      ),
    );
 }
}
appbar title center.png

4.2 title

使用 Title 屬性給 AppBar 設(shè)置標(biāo)題,具體設(shè)置方法可以參考Flutter 組件之 Text 詳解详恼。
使用 centerTitle 設(shè)置標(biāo)題是否居中补君。

  AppBar _appBar() {
    return AppBar(
      title: Text(
        'AppBar',
        style: TextStyle(
          color: Colors.red,
        ),
      ),
      centerTitle: false,
    );
  }
appbar title left.png

4.3 actions

使用 actions 屬性自定義 AppBar 右側(cè)功能鍵。

  AppBar _appBar() {
    return AppBar(
      title: Text(
        'AppBar',
        style: TextStyle(
          color: Colors.red,
        ),
      ),
      actions: _actions(),
    );
  }

  List<Widget> _actions(){
    return [
      Container(
        width: 50,
        color: Colors.green,
        child: Icon(Icons.image),
      ),
      Container(
        width: 50,
        color: Colors.greenAccent,
        child: Icon(Icons.accessible),
      ),
      Container(
        width: 50,
        color: Colors.grey,
        child: Icon(Icons.backspace),
      ),
      Container(
        width: 50,
        color: Colors.yellowAccent,
        child: Icon(Icons.battery_unknown),
      ),
    ];
  }
appbar actions.png

4.4 flexibleSpace

可折疊的應(yīng)用欄昧互,在改變 appBar 的 size 時有效果挽铁。

  AppBar _appBar() {
    return AppBar(
      title: Text(
        'AppBar',
        style: TextStyle(
          color: Colors.red,
        ),
      ),
      actions: _actions(),
      flexibleSpace: _flexibleSpaceBar(),
    );
  }

  FlexibleSpaceBar _flexibleSpaceBar(){
    return FlexibleSpaceBar(
      title: Text('FlexibleSpaceBar'),
    );
  }
appbar flexibleSpaceBar.png

4.5 leading

使用 leading 制定 appbar 左側(cè)按鈕伟桅。

  AppBar _appBar() {
    return AppBar(
      title: Text(
        'AppBar',
        style: TextStyle(
          color: Colors.red,
        ),
      ),
      actions: _actions(),
      // flexibleSpace: _flexibleSpaceBar(),
      leading: _leading(),
    );
  }

  Container _leading(){
    return Container(
      width: 50,
      color: Colors.yellow,
      child: Icon(Icons.favorite),
    );
  }
appbar leading.png

4.6 bottom

  AppBar _appBar() {
    return AppBar(
      title: Text(
        'AppBar',
        style: TextStyle(
          color: Colors.red,
        ),
      ),
      actions: _actions(),
      // flexibleSpace: _flexibleSpaceBar(),
      leading: _leading(),

      bottom: _preferredSize(),
    );
  }

  PreferredSize _preferredSize(){
    return PreferredSize(
      preferredSize: const Size.fromHeight(60),
      child: Container(
        color: Colors.greenAccent,
        height: 60,
      ),
    );
  }
appbar bottom.png

4.7 backgroundColor

  AppBar _appBar() {
    return AppBar(
      title: Text(
        'AppBar',
        style: TextStyle(
          color: Colors.red,
        ),
      ),
      actions: _actions(),
      // flexibleSpace: _flexibleSpaceBar(),
      // leading: _leading(),
      // bottom: _preferredSize(),
      // shadowColor: Colors.black,
      backgroundColor: Colors.grey,
    );
  }
appbar background grey.png

4.8 brightness

通過設(shè)置 brightness 改變上方,電池叽掘、時間...等圖標(biāo)顏色楣铁。

  AppBar _appBar() {
    return AppBar(
      title: Text(
        'AppBar',
        style: TextStyle(
          color: Colors.red,
        ),
      ),
      actions: _actions(),
      backgroundColor: Colors.grey,
      brightness: Brightness.dark,
    );
  }
appbar brightness .png

appbar brightness light.png

4.9 toolbarHeight

通過 toolbarHeight 改變 appbar 高度。

  AppBar _appBar() {
    return AppBar(
      toolbarHeight: 90,
      title: Text(
        'AppBar',
        style: TextStyle(
          color: Colors.red,
        ),
      ),
      actions: _actions(),
      backgroundColor: Colors.grey,
      brightness: Brightness.light,
    );
  }
appbar toolbarHeight.png

4.10 toolbarOpacity

通過 toolbarOpacity 改變 appbar 透明度够掠。

  AppBar _appBar() {
    return AppBar(
      toolbarHeight: 90,
      title: Text(
        'AppBar',
        style: TextStyle(
          color: Colors.red,
        ),
      ),
      actions: _actions(),
      backgroundColor: Colors.grey,
      brightness: Brightness.light,
      toolbarOpacity: 0.5,
    );
  }
appbar toolbarOpacity.png

4.11 iconTheme

使用 iconTheme 改變按鈕主題民褂。

  AppBar _appBar() {
    return AppBar(
      toolbarHeight: 90,
      title: Text(
        'AppBar',
        style: TextStyle(
          color: Colors.red,
        ),
      ),
      actions: _actions(),
      backgroundColor: Colors.grey,
      brightness: Brightness.light,
      iconTheme: _iconThemeData(),
    );
  }

  IconThemeData _iconThemeData(){
    return IconThemeData(
      color: Colors.red,
      size: 40,
      opacity: 0.5,
    );
  }
appbar iconTheme.png

4.12 actionsIconTheme

使用 actionsIconTheme 改變 actions 按鈕主題。注意與 iconTheme 的區(qū)別疯潭,iconTheme 改變了整個 appBar 的按鈕赊堪,而 actions 僅僅改變 actions 按鈕的主題。

  AppBar _appBar() {
    return AppBar(
      toolbarHeight: 90,
      title: Text(
        'AppBar',
        style: TextStyle(
          color: Colors.red,
        ),
      ),
      actions: _actions(),
      backgroundColor: Colors.grey,
      brightness: Brightness.light,
      // iconTheme: _iconThemeData(),
      actionsIconTheme: _iconThemeData(),
    );
  }
appbar actionsIconTheme.png

4.13 textTheme

我試了好多種 TextTheme 設(shè)置竖哩,并沒有發(fā)現(xiàn)哪有有變化哭廉,如果有思路歡迎私信。

4.14 titleSpacing

使用 titleSpacing 來控制 title 的左側(cè)偏移量

  AppBar _appBar() {
    return AppBar(
      toolbarHeight: 90,
      titleSpacing: 40,
      title: Text(
        'AppBar',
        style: TextStyle(
          color: Colors.red,
        ),
      ),
      actions: _actions(),
      backgroundColor: Colors.grey,
      brightness: Brightness.light,
      // iconTheme: _iconThemeData(),
      actionsIconTheme: _iconThemeData(),
    );
  }
appbar titleSpacing.png

4.15 其他屬性

其實 AppBar 還有一些屬性相叁,有興趣可以自己研究一下遵绰。

5. 技術(shù)小結(jié)

  • AppBar 整體使用并沒有太多難點,基礎(chǔ)屬性反而是使用最多的增淹。
  • 多試一試各種屬性椿访,就能熟練掌握。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末虑润,一起剝皮案震驚了整個濱河市成玫,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌拳喻,老刑警劉巖哭当,帶你破解...
    沈念sama閱讀 221,406評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異冗澈,居然都是意外死亡钦勘,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,395評論 3 398
  • 文/潘曉璐 我一進(jìn)店門亚亲,熙熙樓的掌柜王于貴愁眉苦臉地迎上來彻采,“玉大人,你說我怎么就攤上這事捌归〖樟粒” “怎么了?”我有些...
    開封第一講書人閱讀 167,815評論 0 360
  • 文/不壞的土叔 我叫張陵陨溅,是天一觀的道長终惑。 經(jīng)常有香客問我,道長门扇,這世上最難降的妖魔是什么雹有? 我笑而不...
    開封第一講書人閱讀 59,537評論 1 296
  • 正文 為了忘掉前任偿渡,我火速辦了婚禮,結(jié)果婚禮上霸奕,老公的妹妹穿的比我還像新娘溜宽。我一直安慰自己,他們只是感情好质帅,可當(dāng)我...
    茶點故事閱讀 68,536評論 6 397
  • 文/花漫 我一把揭開白布适揉。 她就那樣靜靜地躺著,像睡著了一般煤惩。 火紅的嫁衣襯著肌膚如雪嫉嘀。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,184評論 1 308
  • 那天魄揉,我揣著相機與錄音剪侮,去河邊找鬼。 笑死洛退,一個胖子當(dāng)著我的面吹牛瓣俯,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播兵怯,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼彩匕,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了媒区?” 一聲冷哼從身側(cè)響起驼仪,我...
    開封第一講書人閱讀 39,668評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎驻仅,沒想到半個月后谅畅,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體登渣,經(jīng)...
    沈念sama閱讀 46,212評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡噪服,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,299評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了胜茧。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片粘优。...
    茶點故事閱讀 40,438評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖呻顽,靈堂內(nèi)的尸體忽然破棺而出雹顺,到底是詐尸還是另有隱情,我是刑警寧澤廊遍,帶...
    沈念sama閱讀 36,128評論 5 349
  • 正文 年R本政府宣布嬉愧,位于F島的核電站,受9級特大地震影響喉前,放射性物質(zhì)發(fā)生泄漏没酣。R本人自食惡果不足惜王财,卻給世界環(huán)境...
    茶點故事閱讀 41,807評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望裕便。 院中可真熱鬧绒净,春花似錦、人聲如沸偿衰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,279評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽下翎。三九已至缤言,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間漏设,已是汗流浹背墨闲。 一陣腳步聲響...
    開封第一講書人閱讀 33,395評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留郑口,地道東北人鸳碧。 一個月前我還...
    沈念sama閱讀 48,827評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像犬性,于是被迫代替她去往敵國和親瞻离。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,446評論 2 359