Flutter 的 AppBar widget 需搭配 Scaffold 使用。

其建構子如下:

AppBar({Key key, Widget leading, bool automaticallyImplyLeading: true, Widget title, List actions, Widget flexibleSpace, PreferredSizeWidget bottom, double elevation: 4.0, Color backgroundColor, Brightness brightness, IconThemeData iconTheme, TextTheme textTheme, bool primary: true, bool centerTitle, double titleSpacing: NavigationToolbar.kMiddleSpacing, double toolbarOpacity: 1.0, double bottomOpacity: 1.0 })

屬性如下:

NameTypeDescription
actionsListWidgets to display after the title widget.
automaticallyImplyLeadingboolControls whether we should try to imply the leading widget if null.
backgroundColorColorThe color to use for the app bar’s material. Typically this should be set along with brightness, iconTheme, textTheme.
bottomPreferredSizeWidgetThis widget appears across the bottom of the app bar.
bottomOpacitydoubleHow opaque the bottom part of the app bar is.
brightnessBrightnessThe brightness of the app bar’s material. Typically this is set along with backgroundColor, iconTheme, textTheme.
centerTitleboolWhether the title should be centered.
elevationdoubleThe z-coordinate at which to place this app bar. This controls the size of the shadow below the app bar.
flexibleSpaceWidgetThis widget is stacked behind the toolbar and the tabbar. It’s height will be the same as the app bar’s overall height.
iconThemeIconThemeDataThe color, opacity, and size to use for app bar icons. Typically this is set along with backgroundColor, brightness, textTheme.
leadingWidgetA widget to display before the title.
preferredSizeSizeA size whose height is the sum of kToolbarHeight and the bottom widget’s preferred height.
primaryboolWhether this app bar is being displayed at the top of the screen.
textThemeTextThemeThe typographic styles to use for text in the app bar. Typically this is set along with brightness backgroundColor, iconTheme.
titleWidgetThe primary widget displayed in the appbar.
titleSpacingdoubleThe spacing around title content on the horizontal axis. This spacing is applied even if there is no leading content or actions. If you want title to take all the space available, set this value to 0.0.
toolbarOpacitydoubleHow opaque the toolbar part of the app bar is.
hashCodeintThe hash code for this object.
keyKeyControls how one widget replaces another widget in the tree.
runtimeTypeTypeA representation of the runtime type of the object.

方法如下:

NameReturn TypeDescription
createState()_AppBarStateCreates the mutable state for this widget at a given location in the tree.
createElement()StatefulElementCreates a StatefulElement to manage this widget’s location in the tree.
debugDescribeChildren()ListReturns a list of DiagnosticsNode objects describing this node’s children.
debugFillProperties(DiagnosticPropertiesBuilder properties)voidAdd additional properties associated with the node.
noSuchMethod(Invocation invocation)dynamicInvoked when a non-existent method or property is accessed.
toDiagnosticsNode({String name, DiagnosticsTreeStyle style })DiagnosticsNodeReturns a debug representation of the object that is used by debugging tools and by toStringDeep.
toString({DiagnosticLevel minLevel: DiagnosticLevel.debug })StringReturns a string representation of this object.
toStringDeep({String prefixLineOne: ‘’, String prefixOtherLines, DiagnosticLevel minLevel: DiagnosticLevel.debug })StringReturns a string representation of this node and its descendants.
toStringShallow({String joiner: ‘, ‘, DiagnosticLevel minLevel: DiagnosticLevel.debug })StringReturns a one-line detailed description of the object.
toStringShort()StringA short, textual description of this widget.

主要有 leading/title/actions/bottom 這幾個區塊可供設定。

1.png

import 'package:flutter/material.dart';

void main() {
runApp(new MaterialApp(
home: new DefaultTabController(
length: 2,
child: new Scaffold(
appBar: new AppBar(
leading: new IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () {},
),
title: new Text('Welcome to Flutter'),
actions: [
new IconButton(
icon: new Icon(Icons.add),
),
],
bottom: new TabBar(
tabs: [
new Tab(icon: new Icon(Icons.home)),
new Tab(icon: new Icon(Icons.favorite)),
],
),
),
body: new TabBarView(
children: [
new Icon(Icons.directions_car),
new Icon(Icons.directions_transit)
],
),
))));
}

2.png

3.png