Flutter UI基礎(chǔ)控件和布局方式解析(1)

一湿刽,基礎(chǔ)控件

1,Text

1.1,繼承關(guān)系
Object > Diagnosticable > DiagnosticableTree > Widget > StatelessWidget > Text

1.2 創(chuàng)建Text
Text一共有三種構(gòu)造方法

func 注釋
new Text() 構(gòu)造方法創(chuàng)建劫拗,只能生成一種style
Text.rich() 靜態(tài)方法創(chuàng)建事示,能夠生成多種style
new RichText() 與Text.rich()一樣

1.3 Text

  • 構(gòu)造方法源碼:
 /// Creates a text widget.
  /// If the [style] argument is null, the text will use the style from the closest enclosing [DefaultTextStyle].
  const Text(this.data, {
    Key key,
    this.style,
    this.textAlign,
    this.textDirection,
    this.locale,
    this.softWrap,
    this.overflow,
    this.textScaleFactor,
    this.maxLines,
    this.semanticsLabel,
  }) : assert(data != null), textSpan = null, super(key: key);
  • 用法
new Text(
              '學(xué)習(xí)Text',
              textAlign: TextAlign.center, //文本對(duì)齊方式  居中
              textDirection: TextDirection.ltr, //文本方向
              softWrap: false, //是否自動(dòng)換行 false文字不考慮容器大小  單行顯示   超出;屏幕部分將默認(rèn)截?cái)嗵幚?              overflow: TextOverflow
                  .ellipsis, //文字超出屏幕之后的處理方式  TextOverflow.clip剪裁   TextOverflow.fade 漸隱  TextOverflow.ellipsis省略號(hào)
              textScaleFactor: 2.0, //字體顯示的賠率
              maxLines: 10, //最大行數(shù)
              style: new TextStyle(
                decorationColor: const Color(0xffffffff), //線的顏色
                decoration: TextDecoration
                    .none, //none無(wú)文字裝飾   lineThrough刪除線   overline文字上面顯示線    underline文字下面顯示線
                decorationStyle: TextDecorationStyle
                    .solid, //文字裝飾的風(fēng)格  dashed,dotted虛線(簡(jiǎn)短間隔大小區(qū)分)  double三條線  solid兩條線
                wordSpacing: 0.0, //單詞間隙(負(fù)值可以讓單詞更緊湊)
                letterSpacing: 0.0, //字母間隙(負(fù)值可以讓字母更緊湊)
                fontStyle: FontStyle.italic, //文字樣式豫尽,斜體和正常
                fontSize: 20.0, //字體大小
                fontWeight: FontWeight.w900, //字體粗細(xì)  粗體和正常
                color: const Color(0xffffffff), //文字顏色
              )

1.4 Text.rich()鳖宾、new RichText()

  • 構(gòu)造方法源碼:
/// Creates a text widget with a [TextSpan].
  const Text.rich(this.textSpan, {
    Key key,
    this.style,
    this.textAlign,
    this.textDirection,
    this.locale,
    this.softWrap,
    this.overflow,
    this.textScaleFactor,
    this.maxLines,
    this.semanticsLabel,
  }): assert(textSpan != null), data = null, super(key: key);

  /// Creates a paragraph of rich text.
  const RichText({
    Key key,
    @required this.text,
    this.textAlign = TextAlign.start,
    this.textDirection,
    this.softWrap = true,
    this.overflow = TextOverflow.clip,
    this.textScaleFactor = 1.0,
    this.maxLines,
    this.locale,
  }) : assert(text != null) ,assert(textAlign != null), assert(softWrap != null), assert(overflow != null),
       assert(textScaleFactor != null), assert(maxLines == null || maxLines > 0), super(key: key);
  • 用法:
new TextSpan(
                text: '拼接1',
              ),
              new TextSpan(
                text: '拼接2',
              ),
              new TextSpan(
                text: '拼接3',
              ),
              new TextSpan(
                text: '拼接4',
              ),
              new TextSpan(
                text: '拼接5',
              ),
              new TextSpan(
                text: '拼接6',
              ),
              new TextSpan(
                  text: '拼接7',
                  style: new TextStyle(
                    color: Colors.yellow,
                  ),
                  recognizer:new TapGestureRecognizer()..onTap=(){//增加一個(gè)點(diǎn)擊事件
                    print('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@');
                  },
              ),

2吼砂,Image

2.1,繼承關(guān)系
Object > Diagnosticablet > DiagnosticableTreet > Widgett > StatefulWidgett > Image

2.2,支持類型
支持 JPEG、PNG鼎文、GIF渔肩、Animated GIF、WebP拇惋、Animated WebP周偎、BMP 和 WBMP 等格式

2.3, 創(chuàng)建Image
Image一共有五種創(chuàng)建方式

func 注釋
Image() 構(gòu)造方法創(chuàng)建
Image.asset() 加載資源圖片
Image.file() 加載本地圖片
Image. network() 加載網(wǎng)絡(luò)圖片
Image.memory() 加載Uint8List資源圖片

2.4, 創(chuàng)建Image

  • 構(gòu)造方法源碼:

  const Image({
    Key key,
    @required this.image,
    this.semanticLabel,
    this.excludeFromSemantics = false,
    this.width,
    this.height,
    this.color,
    this.colorBlendMode,
    this.fit,
    this.alignment = Alignment.center,
    this.repeat = ImageRepeat.noRepeat,
    this.centerSlice,
    this.matchTextDirection = false,
    this.gaplessPlayback = false,
    this.filterQuality = FilterQuality.low,
  }) : assert(image != null),
       assert(alignment != null),
       assert(repeat != null),
       assert(filterQuality != null),
       assert(matchTextDirection != null),
       super(key: key);
  • 用法
// 資源圖片
new Image.asset('imgs/logo.jpeg'),
//網(wǎng)絡(luò)圖片
new Image.network(
    'https://flutter.io/images/homepage/header-illustration.png'),
// 本地文件圖片
new Image.file(new File("/Users/gs/Downloads/1.jpeg")),
// Uint8List圖片
new Image.memory(bytes),
//使用ImageProvider加載圖片
new Image(image: new NetworkImage("https://flutter.io/images/homepage/screenshot-2.png"))

tips:
關(guān)于本地圖片資源使用這里我需要說(shuō)一下,首先在項(xiàng)目最頂部創(chuàng)建一個(gè)images文件夾放入一張圖片xxx.jpeg撑帖,然后在pubspec.yaml添加幾行代碼蓉坎,表示引用images文件夾下的xxx.jpeg圖片,另外還可以設(shè)置2x和3x圖片

image

2.5, 屬性解析

  • alignment → AlignmentGeometry - 圖像邊界內(nèi)對(duì)齊圖像。

  • centerSlice → Rect - 九片圖像的中心切片胡嘿。

  • color → Color - 該顏色與每個(gè)圖像像素混合colorBlendMode蛉艾。

  • colorBlendMode → BlendMode - 用于 color 與此圖像結(jié)合使用。

  • fit → BoxFit - 圖像在布局中分配的空間衷敌。

  • gaplessPlayback → bool - 當(dāng)圖像提供者發(fā)生變化時(shí)勿侯,是繼續(xù)顯示舊圖像(true)還是暫時(shí)不顯示(false)。

  • image → ImageProvider - 要顯示的圖像缴罗。

  • matchTextDirection → bool - 是否在圖像的方向上繪制圖像 TextDirection助琐。

  • repeat → ImageRepeat - 未充分容器時(shí),是否重復(fù)圖片面氓。

  • height → double - 圖像的高度弓柱。

  • width → double - 圖像的寬度。

2.6, 圓角圖片

Image 是不支持圓角和陰影的侧但,目前可以通過(guò)使用 CircleAvatar 和 Container 實(shí)現(xiàn)。
  • CircleAvatar
var img = 'https://b-ssl.duitang.com/uploads/item/' +
        '201602/15/20160215235057_EU3tS.thumb.700_0.jpeg';

new CircleAvatar(
    backgroundImage: new NetworkImage(url),
    radius: 100.0,      // --> 半徑越大航罗,圖片越大
),
  • Container
    使用 Container 實(shí)現(xiàn)禀横,其原理是把圖片放在 decoration 里,而不是 child 里粥血,因?yàn)榘褕D片放在 child 里并設(shè)置 borderRadius 時(shí)會(huì)出現(xiàn)一個(gè)圖片穿透的問(wèn)題柏锄,Container 還沒(méi)有 overflow 屬性酿箭。
new Container(
    width: 200.0,
    height: 200.0,
    margin: const EdgeInsets.all(20.0),
    decoration: new BoxDecoration(
        color: Colors.white,
        image: new DecorationImage(image: new NetworkImage(this.imgsrc), fit: BoxFit.cover),
        shape: BoxShape.circle,
    ),
),

上面實(shí)現(xiàn)的都是一個(gè)圓形圖片,下面的實(shí)現(xiàn)一個(gè)真正的圓角圖片趾娃。

new Container(
    width: 200.0,
    height: 200.0,
    margin: const EdgeInsets.all(20.0),
    decoration: new BoxDecoration(
        color: Colors.white,
        image: new DecorationImage(image: new NetworkImage(this.imgsrc), fit: BoxFit.cover),
        shape: BoxShape.rectangle,              // <-- 這里需要設(shè)置為 rectangle
        borderRadius: new BorderRadius.all(
            const Radius.circular(20.0),        // <-- rectangle 時(shí)缭嫡,BorderRadius 才有效
        ),
    ),
),

3,Button

3.1繼承關(guān)系
Object > Diagnosticable > DiagnosticableTree > Widget > StatelessWidget > MaterialButton

在 Flutter 里有很多的 Button抬闷,包括了:MaterialButton妇蛀、RaisedButton、FloatingActionButton笤成、FlatButton评架、IconButton、ButtonBar炕泳、DropdownButton 等纵诞。
一般常用的 Button 是 MaterialButton、IconButton培遵、FloatingActionButton浙芙。

3.2構(gòu)造方法

  • 3.2.1 MaterialButton

  const MaterialButton({
    Key key,
    @required this.onPressed,
    this.onHighlightChanged,
    this.textTheme,
    this.textColor,
    this.disabledTextColor,
    this.color,
    this.disabledColor,
    this.highlightColor,
    this.splashColor,
    this.colorBrightness,
    this.elevation,
    this.highlightElevation,
    this.disabledElevation,
    this.padding,
    this.shape,
    this.clipBehavior = Clip.none,
    this.materialTapTargetSize,
    this.animationDuration,
    this.minWidth,
    this.height,
    this.child,
  }) : super(key: key);

tips:
VoidCallback onPressedValueChanged<bool> onHighlightChanged
VoidCallback onPressed: 點(diǎn)擊激活按鈕時(shí)調(diào)用的方法
ValueChanged<bool> onHighlightChanged: 按下和抬起時(shí)都會(huì)調(diào)用的方法

  • 用法
MaterialButton(
      key: ValueKey("text"),
      child: Text("MaterialButton"),
      onPressed: pressedBtn(context),
      onHighlightChanged: onHighlightChanged(context),
      textTheme: ButtonTextTheme.normal,
      textColor: Colors.blue,
      disabledTextColor: Colors.red,
      color: Color(0xFF82B1FF),
      disabledColor: Colors.grey,
      highlightColor: Colors.grey,
      // 按下的背景色
      splashColor: Colors.green,
      // 水波紋顏色
      colorBrightness: Brightness.light,
      // 主題
      elevation: 10,
      highlightElevation: 10,
      disabledElevation: 10,
      padding: EdgeInsets.all(10),
//       MaterialButton shape 子類才起效
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(20)),
          side: BorderSide(
              color: Color(0xFFFFFFFF), style: BorderStyle.solid, width: 2)),
      clipBehavior: Clip.antiAlias,
      materialTapTargetSize: MaterialTapTargetSize.padded,
      animationDuration: Duration(seconds: 1),
      minWidth: 200,
      height: 50,
    );
  • 3.2.2 RaisedButton
    RaisedButton 與 MaterialButton 類似
  • 構(gòu)造方法
new RaisedButton(
    child: new Text('點(diǎn)我'),
    onPressed: () {},
)
  • 3.2.3 FlatButton
    FlatButton 與 MaterialButton 類似,不同的是它是透明背景的籽腕。如果一個(gè) Container 想要點(diǎn)擊事件時(shí)嗡呼,可以使用 FlatButton 包裹,而不是 MaterialButton节仿。因?yàn)?MaterialButton 默認(rèn)帶背景晤锥,而 FlatButton 默認(rèn)不帶背景。
  • 構(gòu)造方法
new FlatButton(
    child: new Text('點(diǎn)我'),
    onPressed: () {},
)
  • 3.2.4 IconButton
    IconButton 顧名思義就是 Icon + Button 的復(fù)合體廊宪,當(dāng)某個(gè) Icon 需要點(diǎn)擊事件時(shí)矾瘾,使用 IconButton 最好不過(guò)。
  • 構(gòu)造方法
new IconButton(
    icon: new Icon(Icons.volume_up),
    tooltip: 'Increase volume by 10%',
    onPressed: () {
        // ...
    },
)

tips: 還有已經(jīng)定義好的 Icon Button:CloseButton箭启、BackButton壕翩。他們都有導(dǎo)航返回的能力。

  • 3.2.5 FloatingActionButton
    FloatingActionButton 是一個(gè)浮動(dòng)在頁(yè)面右下角的浮動(dòng)按鈕傅寡。
  • 構(gòu)造方法
new Scaffold(
    // ...
    floatingActionButton: new FloatingActionButton(
        onPressed: () {},
        child: new Icon(Icons.add_a_photo),
        elevation: 3.0,
        highlightElevation: 2.0,
        backgroundColor: Colors.red,        // 紅色
    ),
)
  • 3.2.6 FloatingActionButton
    ButtonBar 是一個(gè)布局組件放妈,可以讓 Button 排列在一行。- 構(gòu)造方法
new ButtonBar(
    children: <Widget>[
        new CloseButton(),
        new BackButton(),
    ],
)

4荐操,Container

4.1繼承關(guān)系
Object > Diagnosticable > DiagnosticableTree > Widget > StatelessWidget > Container
4.2構(gòu)造方法

Container({
    Key key,
    this.alignment,
    this.padding,
    Color color,
    Decoration decoration,
    this.foregroundDecoration,
    double width,
    double height,
    BoxConstraints constraints,
    this.margin,
    this.transform,
    this.child,
  })

4.3用法

new Container(
  constraints: new BoxConstraints.expand(
    height:Theme.of(context).textTheme.display1.fontSize * 1.1 + 200.0,
  ),
  decoration: new BoxDecoration(
    border: new Border.all(width: 2.0, color: Colors.red),
    color: Colors.grey,
    borderRadius: new BorderRadius.all(new Radius.circular(20.0)),
    image: new DecorationImage(
      image: new NetworkImage('http://h.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=0d023672312ac65c67506e77cec29e27/9f2f070828381f30dea167bbad014c086e06f06c.jpg'),
      centerSlice: new Rect.fromLTRB(270.0, 180.0, 1360.0, 730.0),
    ),
  ),
  padding: const EdgeInsets.all(8.0),
  alignment: Alignment.center,
  child: new Text('Hello World',
    style: Theme.of(context).textTheme.display1.copyWith(color: Colors.black)),
  transform: new Matrix4.rotationZ(0.3),
)

4.4 decoration
decoration可以設(shè)置邊框芜抒、背景色、背景圖片托启、圓角等屬性宅倒,非常實(shí)用。對(duì)于transform這個(gè)屬性屯耸,一般有過(guò)其他平臺(tái)開(kāi)發(fā)經(jīng)驗(yàn)的拐迁,都大致了解蹭劈,這種變換,一般不是變換的實(shí)際位置线召,而是變換的繪制效果铺韧,也就是說(shuō)它的點(diǎn)擊以及尺寸、間距等都是按照未變換前的缓淹。

decoration = decoration ?? (color != null ? new BoxDecoration(color: color) : null),

可以看出哈打,對(duì)于顏色的設(shè)置,最后都是轉(zhuǎn)換為decoration來(lái)進(jìn)行繪制的割卖。如果同時(shí)包含decoration和color兩種屬性前酿,則會(huì)報(bào)錯(cuò)。

@override
  Widget build(BuildContext context) {
    Widget current = child;

    if (child == null && (constraints == null || !constraints.isTight)) {
      current = new LimitedBox(
        maxWidth: 0.0,
        maxHeight: 0.0,
        child: new ConstrainedBox(constraints: const BoxConstraints.expand())
      );
    }

    if (alignment != null)
      current = new Align(alignment: alignment, child: current);

    final EdgeInsetsGeometry effectivePadding = _paddingIncludingDecoration;
    if (effectivePadding != null)
      current = new Padding(padding: effectivePadding, child: current);

    if (decoration != null)
      current = new DecoratedBox(decoration: decoration, child: current);

    if (foregroundDecoration != null) {
      current = new DecoratedBox(
        decoration: foregroundDecoration,
        position: DecorationPosition.foreground,
        child: current
      );
    }

    if (constraints != null)
      current = new ConstrainedBox(constraints: constraints, child: current);

    if (margin != null)
      current = new Padding(padding: margin, child: current);

    if (transform != null)
      current = new Transform(transform: transform, child: current);

    return current;
  }
Container的build函數(shù)不長(zhǎng)鹏溯,繪制也是一個(gè)線性的判斷的過(guò)程罢维,一層一層的包裹著widget,去實(shí)現(xiàn)不同的樣式丙挽。
最里層的是child肺孵,如果為空或者其他約束條件,則最里層包含的為一個(gè)LimitedBox颜阐,然后依次是Align平窘、Padding、DecoratedBox凳怨、前景DecoratedBox瑰艘、ConstrainedBox、Padding(實(shí)現(xiàn)margin效果)肤舞、Transform紫新。
Container的源碼本身并不復(fù)雜,復(fù)雜的是它的各種布局表現(xiàn)李剖。我們謹(jǐn)記住一點(diǎn)芒率,如果內(nèi)部不設(shè)置約束,則按照父節(jié)點(diǎn)盡可能的擴(kuò)大篙顺,如果內(nèi)部有約束偶芍,則按照內(nèi)部來(lái)。

5德玫, Row&Column

5.1 Row

  • 5.1.1 繼承關(guān)系
    Object > Diagnosticable > DiagnosticableTree > Widget > RenderObjectWidget > MultiChildRenderObjectWidget > Flex > Row
  • 5.1.2 簡(jiǎn)介
    在Flutter中非常常見(jiàn)的一個(gè)多子節(jié)點(diǎn)控件匪蟀,將children排列成一行。估計(jì)是借鑒了Web中Flex布局宰僧,所以很多屬性和表現(xiàn)萄窜,都跟其相似。但是注意一點(diǎn),自身不帶滾動(dòng)屬性查刻,如果超出了一行,在debug下面則會(huì)顯示溢出的提示凤类。
  • 5.1.3 布局行為
    Row的布局有六個(gè)步驟穗泵,這種布局表現(xiàn)來(lái)自Flex(Row和Column的父類):
    首先按照不受限制的主軸(main axis)約束條件,對(duì)flex為null或者為0的child進(jìn)行布局谜疤,然后按照交叉軸( cross axis)的約束佃延,對(duì)child進(jìn)行調(diào)整;
    按照不為空的flex值夷磕,將主軸方向上剩余的空間分成相應(yīng)的幾等分履肃;
    對(duì)上述步驟flex值不為空的child,在交叉軸方向進(jìn)行調(diào)整坐桩,在主軸方向使用最大約束條件尺棋,讓其占滿步驟2所分得的空間;
    Flex交叉軸的范圍取自子節(jié)點(diǎn)的最大交叉軸绵跷;
    主軸Flex的值是由mainAxisSize屬性決定的膘螟,其中MainAxisSize可以取max、min以及具體的value值碾局;
    每一個(gè)child的位置是由mainAxisAlignment以及crossAxisAlignment所決定荆残。
    Row的布局行為表面上看有這么多個(gè)步驟,其實(shí)也還算是簡(jiǎn)單净当,可以完全參照web中的Flex布局内斯,包括主軸、交叉軸等概念像啼。
布局方式

-5.1.4 構(gòu)造方法

Row({
  Key key,
  MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
  MainAxisSize mainAxisSize = MainAxisSize.max,
  CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
  TextDirection textDirection,
  VerticalDirection verticalDirection = VerticalDirection.down,
  TextBaseline textBaseline,
  List<Widget> children = const <Widget>[],
})
  • 5.1.5 屬性解析
    MainAxisAlignment:主軸方向上的對(duì)齊方式俘闯,會(huì)對(duì)child的位置起作用,默認(rèn)是start埋合。
    其中MainAxisAlignment枚舉值:

center:將children放置在主軸的中心备徐;
end:將children放置在主軸的末尾;
spaceAround:將主軸方向上的空白區(qū)域均分甚颂,使得children之間的空白區(qū)域相等蜜猾,但是首尾child的空白區(qū)域?yàn)?/2;
spaceBetween:將主軸方向上的空白區(qū)域均分振诬,使得children之間的空白區(qū)域相等蹭睡,首尾child都靠近首尾,沒(méi)有間隙赶么;
spaceEvenly:將主軸方向上的空白區(qū)域均分肩豁,使得children之間的空白區(qū)域相等,包括首尾child;
start:將children放置在主軸的起點(diǎn)清钥;

其中spaceAround琼锋、spaceBetween以及spaceEvenly的區(qū)別,就是對(duì)待首尾child的方式祟昭。其距離首尾的距離分別是空白區(qū)域的1/2缕坎、0、1篡悟。
MainAxisSize:在主軸方向占有空間的值谜叹,默認(rèn)是max。
MainAxisSize的取值有兩種:

max:根據(jù)傳入的布局約束條件搬葬,最大化主軸方向的可用空間荷腊;
min:與max相反,是最小化主軸方向的可用空間急凰;

CrossAxisAlignment:children在交叉軸方向的對(duì)齊方式女仰,與MainAxisAlignment略有不同。
CrossAxisAlignment枚舉值有如下幾種:

baseline:在交叉軸方向香府,使得children的baseline對(duì)齊董栽;
center:children在交叉軸上居中展示;
end:children在交叉軸上末尾展示企孩;
start:children在交叉軸上起點(diǎn)處展示锭碳;
stretch:讓children填滿交叉軸方向;

TextDirection:阿拉伯語(yǔ)系的兼容設(shè)置勿璃,一般無(wú)需處理擒抛。
VerticalDirection:定義了children擺放順序,默認(rèn)是down补疑。
VerticalDirection枚舉值有兩種:

down:從top到bottom進(jìn)行布局歧沪;
up:從bottom到top進(jìn)行布局。

top對(duì)應(yīng)Row以及Column的話莲组,就是左邊和頂部诊胞,bottom的話,則是右邊和底部锹杈。
TextBaseline:使用的TextBaseline的方式撵孤,有兩種,前面已經(jīng)介紹過(guò)竭望。

  • 5.1.6 用法
Row(
  children: <Widget>[
    Expanded(
      child: Container(
        color: Colors.red,
        padding: EdgeInsets.all(5.0),
      ),
      flex: 1,
    ),
    Expanded(
      child: Container(
        color: Colors.yellow,
        padding: EdgeInsets.all(5.0),
      ),
      flex: 2,
    ),
    Expanded(
      child: Container(
        color: Colors.blue,
        padding: EdgeInsets.all(5.0),
      ),
      flex: 1,
    ),
  ],
)

5.2 Flex

  • 5.2.1 構(gòu)造函數(shù)
Flex({
  Key key,
  @required this.direction,
  this.mainAxisAlignment = MainAxisAlignment.start,
  this.mainAxisSize = MainAxisSize.max,
  this.crossAxisAlignment = CrossAxisAlignment.center,
  this.textDirection,
  this.verticalDirection = VerticalDirection.down,
  this.textBaseline,
  List<Widget> children = const <Widget>[],
})

tips: 可以看出邪码,F(xiàn)lex的構(gòu)造函數(shù)就比Row和Column的多了一個(gè)參數(shù)。Row跟Column的區(qū)別咬清,正是這個(gè)direction參數(shù)的不同闭专。當(dāng)為Axis.horizontal的時(shí)候奴潘,則是Row,當(dāng)為Axis.vertical的時(shí)候影钉,則是Column画髓。

  • 5.2.2 使用方法
while (child != null) {
  final FlexParentData childParentData = child.parentData;
  totalChildren++;
  final int flex = _getFlex(child);
  if (flex > 0) {
    totalFlex += childParentData.flex;
    lastFlexChild = child;
  } else {
    BoxConstraints innerConstraints;
    if (crossAxisAlignment == CrossAxisAlignment.stretch) {
      switch (_direction) {
        case Axis.horizontal:
          innerConstraints = new BoxConstraints(minHeight: constraints.maxHeight,
                                                maxHeight: constraints.maxHeight);
          break;
        case Axis.vertical:
          innerConstraints = new BoxConstraints(minWidth: constraints.maxWidth,
                                                maxWidth: constraints.maxWidth);
          break;
      }
    } else {
      switch (_direction) {
        case Axis.horizontal:
          innerConstraints = new BoxConstraints(maxHeight: constraints.maxHeight);
          break;
        case Axis.vertical:
          innerConstraints = new BoxConstraints(maxWidth: constraints.maxWidth);
          break;
      }
    }
    child.layout(innerConstraints, parentUsesSize: true);
    allocatedSize += _getMainSize(child);
    crossSize = math.max(crossSize, _getCrossSize(child));
  }
  child = childParentData.nextSibling;
}

5.3 Column
在講解Row的時(shí)候,其實(shí)是按照Flex的一些布局行為來(lái)進(jìn)行的斧拍,包括源碼分析雀扶,也都是在用Flex進(jìn)行分析的。Row和Column都是Flex的子類肆汹,只是direction參數(shù)不同。Column各方面同Row予权,因此在這里不再另行講解昂勉。
在講解Flex的時(shí)候,也說(shuō)過(guò)是參照了web的Flex布局扫腺,如果有相關(guān)開(kāi)發(fā)經(jīng)驗(yàn)的同學(xué)岗照,完全可以參照著去理解,這樣子更容易去理解它們的用法和原理笆环。
關(guān)于row和column轉(zhuǎn)載鏈接:http://www.reibang.com/p/0ce74751d970

6攒至,Text

  • 6.1 用法
appBar: new AppBar(
    title: new Text('首頁(yè)'),
    leading: new Icon(Icons.home),
    backgroundColor: Colors.blue,
    centerTitle: true,
    actions: <Widget>[
        // 非隱藏的菜單
        new IconButton(
            icon: new Icon(Icons.add_alarm),
            tooltip: 'Add Alarm',
            onPressed: () {}
        ),
        // 隱藏的菜單
        new PopupMenuButton<String>(
            itemBuilder: (BuildContext context) => <PopupMenuItem<String>>[
                this.SelectView(Icons.message, '發(fā)起群聊', 'A'),
                this.SelectView(Icons.group_add, '添加服務(wù)', 'B'),
                this.SelectView(Icons.cast_connected, '掃一掃碼', 'C'),
            ],
            onSelected: (String action) {
                // 點(diǎn)擊選項(xiàng)的時(shí)候
                switch (action) {
                    case 'A': break;
                    case 'B': break;
                    case 'C': break;
                }
            },
        ),
    ],
),
圖示
  • 6.2 屬性說(shuō)明
    leading → Widget - 在標(biāo)題前面顯示的一個(gè)控件,在首頁(yè)通常顯示應(yīng)用的 logo躁劣;在其他界面通常顯示為返回按鈕迫吐。
    title → Widget - Toolbar 中主要內(nèi)容,通常顯示為當(dāng)前界面的標(biāo)題文字账忘。
    actions → List - 一個(gè) Widget 列表志膀,代表 Toolbar 中所顯示的菜單,對(duì)于常用的菜單鳖擒,通常使用 IconButton 來(lái)表示溉浙;對(duì)于不常用的菜單通常使用 PopupMenuButton 來(lái)顯示為三個(gè)點(diǎn),點(diǎn)擊后彈出二級(jí)菜單蒋荚。
    bottom → PreferredSizeWidget - 一個(gè) AppBarBottomWidget 對(duì)象戳稽,通常是 TabBar。用來(lái)在 Toolbar 標(biāo)題下面顯示一個(gè) Tab 導(dǎo)航欄期升。
    elevation → double - 控件的 z 坐標(biāo)順序惊奇,默認(rèn)值為 4,對(duì)于可滾動(dòng)的 SliverAppBar吓妆,當(dāng) SliverAppBar 和內(nèi)容同級(jí)的時(shí)候赊时,該值為 0, 當(dāng)內(nèi)容滾動(dòng) SliverAppBar 變?yōu)?Toolbar 的時(shí)候行拢,修改 elevation 的值祖秒。
    flexibleSpace → Widget - 一個(gè)顯示在 AppBar 下方的控件,高度和 AppBar 高度一樣,可以實(shí)現(xiàn)一些特殊的效果竭缝,該屬性通常在 SliverAppBar 中使用房维。
    backgroundColor → Color - Appbar 的顏色,默認(rèn)值為 ThemeData.primaryColor抬纸。改值通常和下面的三個(gè)屬性一起使用咙俩。
    brightness → Brightness - Appbar 的亮度,有白色和黑色兩種主題湿故,默認(rèn)值為 ThemeData.primaryColorBrightness阿趁。
    iconTheme → IconThemeData - Appbar 上圖標(biāo)的顏色、透明度坛猪、和尺寸信息脖阵。默認(rèn)值為 ThemeData.primaryIconTheme。
    textTheme → TextTheme - Appbar 上的文字樣式墅茉。
    centerTitle → bool - 標(biāo)題是否居中顯示命黔,默認(rèn)值根據(jù)不同的操作系統(tǒng),顯示方式不一樣就斤。

toolbarOpacity → double

關(guān)于app的轉(zhuǎn)載鏈接: http://www.reibang.com/p/77f8b7ee8460

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末悍募,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子洋机,更是在濱河造成了極大的恐慌坠宴,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,682評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件槐秧,死亡現(xiàn)場(chǎng)離奇詭異啄踊,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)刁标,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門颠通,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人膀懈,你說(shuō)我怎么就攤上這事顿锰。” “怎么了启搂?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,083評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵硼控,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我胳赌,道長(zhǎng)牢撼,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,763評(píng)論 1 295
  • 正文 為了忘掉前任疑苫,我火速辦了婚禮熏版,結(jié)果婚禮上纷责,老公的妹妹穿的比我還像新娘。我一直安慰自己撼短,他們只是感情好再膳,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,785評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著曲横,像睡著了一般喂柒。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上禾嫉,一...
    開(kāi)封第一講書(shū)人閱讀 51,624評(píng)論 1 305
  • 那天灾杰,我揣著相機(jī)與錄音,去河邊找鬼熙参。 笑死吭露,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的尊惰。 我是一名探鬼主播,決...
    沈念sama閱讀 40,358評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼泥兰,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼弄屡!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起鞋诗,我...
    開(kāi)封第一講書(shū)人閱讀 39,261評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤膀捷,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后削彬,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體全庸,經(jīng)...
    沈念sama閱讀 45,722評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年融痛,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了壶笼。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,030評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡雁刷,死狀恐怖覆劈,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情沛励,我是刑警寧澤责语,帶...
    沈念sama閱讀 35,737評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站目派,受9級(jí)特大地震影響坤候,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜企蹭,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,360評(píng)論 3 330
  • 文/蒙蒙 一白筹、第九天 我趴在偏房一處隱蔽的房頂上張望智末。 院中可真熱鬧,春花似錦遍蟋、人聲如沸吹害。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,941評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)它呀。三九已至,卻和暖如春棒厘,著一層夾襖步出監(jiān)牢的瞬間纵穿,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,057評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工奢人, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留谓媒,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,237評(píng)論 3 371
  • 正文 我出身青樓何乎,卻偏偏與公主長(zhǎng)得像句惯,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子支救,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,976評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)自 Q吹個(gè)大氣球Q 本文主要介紹Flutter布局中的Row各墨、Column控件指孤,詳細(xì)介紹了其布局行為以及使用場(chǎng)景...
    chilim閱讀 2,782評(píng)論 0 1
  • 本文主要介紹Flutter布局中的Row恃轩、Column控件,詳細(xì)介紹了其布局行為以及使用場(chǎng)景黎做,并對(duì)源碼進(jìn)行了分析叉跛。...
    Q吹個(gè)大氣球Q閱讀 79,631評(píng)論 6 41
  • 前言 本文的目的是為了讓讀者掌握不同布局類Widget的布局特點(diǎn),分享一些在實(shí)際使用過(guò)程遇到的一些問(wèn)題引几,在《Flu...
    xqqlv閱讀 5,258評(píng)論 0 18
  • 剛剛和一個(gè)家長(zhǎng)通完電話昧互,兒子便興沖沖推門而入:成績(jī)出來(lái)了,準(zhǔn)備好獎(jiǎng)勵(lì)吧伟桅。爸爸和媽媽幾乎同時(shí)看向小東同學(xué)敞掘。媽媽在...
    Ljc_c10a閱讀 676評(píng)論 2 17
  • 當(dāng)小月哭的稀里嘩啦和我說(shuō),她失戀的時(shí)候楣铁,我不驚訝甚至有些慶幸玖雁。小月認(rèn)識(shí)那個(gè)男生兩年,喜歡了兩年盖腕,正兒八經(jīng)談戀...
    Baby王王王王閱讀 301評(píng)論 0 0