国产chinesehdxxxx野外,国产av无码专区亚洲av琪琪,播放男人添女人下边视频,成人国产精品一区二区免费看,chinese丰满人妻videos

Flutter實戰(zhàn) Container

2021-03-08 10:36 更新

我們在前面的章節(jié)示例中多次用到過Container組件,本節(jié)我們就詳細(xì)介紹一下Container組件。Container是一個組合類容器,它本身不對應(yīng)具體的RenderObject,它是DecoratedBox、ConstrainedBox、Transform、PaddingAlign等組件組合的一個多功能容器,所以我們只需通過一個Container組件可以實現(xiàn)同時需要裝飾、變換、限制的場景。下面是Container的定義:

Container({
  this.alignment,
  this.padding, //容器內(nèi)補(bǔ)白,屬于decoration的裝飾范圍
  Color color, // 背景色
  Decoration decoration, // 背景裝飾
  Decoration foregroundDecoration, //前景裝飾
  double width,//容器的寬度
  double height, //容器的高度
  BoxConstraints constraints, //容器大小的限制條件
  this.margin,//容器外補(bǔ)白,不屬于decoration的裝飾范圍
  this.transform, //變換
  this.child,
})

Container的大多數(shù)屬性在介紹其它容器時都已經(jīng)介紹過了,不再贅述,但有兩點需要說明:

  • 容器的大小可以通過width、height屬性來指定,也可以通過constraints來指定;如果它們同時存在時,width、height優(yōu)先。實際上 Container 內(nèi)部會根據(jù)widthheight來生成一個constraints。
  • colordecoration是互斥的,如果同時設(shè)置它們則會報錯!實際上,當(dāng)指定color時,Container內(nèi)會自動創(chuàng)建一個decoration。

#實例

我們通過Container來實現(xiàn)如圖5-16所示的卡片:

圖5-16

實現(xiàn)代碼如下:

Container(
  margin: EdgeInsets.only(top: 50.0, left: 120.0), //容器外填充
  constraints: BoxConstraints.tightFor(width: 200.0, height: 150.0), //卡片大小
  decoration: BoxDecoration(//背景裝飾
      gradient: RadialGradient( //背景徑向漸變
          colors: [Colors.red, Colors.orange],
          center: Alignment.topLeft,
          radius: .98
      ),
      boxShadow: [ //卡片陰影
        BoxShadow(
            color: Colors.black54,
            offset: Offset(2.0, 2.0),
            blurRadius: 4.0
        )
      ]
  ),
  transform: Matrix4.rotationZ(.2), //卡片傾斜變換
  alignment: Alignment.center, //卡片內(nèi)文字居中
  child: Text( //卡片文字
    "5.20", style: TextStyle(color: Colors.white, fontSize: 40.0),
  ),
);

可以看到Container具備多種組件的功能,通過查看Container源碼,我們會很容易發(fā)現(xiàn)它正是前面我們介紹過的多種組件組合而成。在 Flutter 中,Container組件也正是組合優(yōu)先于繼承的實例。

#Padding和Margin

接下來我們來研究一下Container組件marginpadding屬性的區(qū)別:

...
Container(
  margin: EdgeInsets.all(20.0), //容器外補(bǔ)白
  color: Colors.orange,
  child: Text("Hello world!"),
),
Container(
  padding: EdgeInsets.all(20.0), //容器內(nèi)補(bǔ)白
  color: Colors.orange,
  child: Text("Hello world!"),
),
...

可以發(fā)現(xiàn),直觀的感覺就是margin的留白是在容器外部,而padding的留白是在容器內(nèi)部,讀者需要記住這個差異。事實上,Container內(nèi)marginpadding都是通過Padding 組件來實現(xiàn)的,上面的示例代碼實際上等價于:

...
Padding(
  padding: EdgeInsets.all(20.0),
  child: DecoratedBox(
    decoration: BoxDecoration(color: Colors.orange),
    child: Text("Hello world!"),
  ),
),
DecoratedBox(
  decoration: BoxDecoration(color: Colors.orange),
  child: Padding(
    padding: const EdgeInsets.all(20.0),
    child: Text("Hello world!"),
  ),
),
...    
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號