Flutter學習--Container組件

一、介紹

Container 組件是一個方便繪制、定位和調(diào)整子組件大小的組件(可以裝別的組件的盒子)

二、創(chuàng)建container組件源碼

Container({
    Key key,
    this.alignment,
    this.padding,
    this.color,
    this.decoration,
    this.foregroundDecoration,
    double width,
    double height,
    BoxConstraints constraints,
    this.margin,
    this.transform,
    this.child,
    this.clipBehavior = Clip.none,
  }) : assert(margin == null || margin.isNonNegative),
       assert(padding == null || padding.isNonNegative),
       assert(decoration == null || decoration.debugAssertIsValid()),
       assert(constraints == null || constraints.debugAssertIsValid()),
       assert(clipBehavior != null),
       assert(decoration != null || clipBehavior == Clip.none),
       assert(color == null || decoration == null,
         'Cannot provide both a color and a decoration\n'
         'To provide both, use "decoration: BoxDecoration(color: color)".'
       ),
       constraints =
        (width != null || height != null)
          ? constraints?.tighten(width: width, height: height)
            ?? BoxConstraints.tightFor(width: width, height: height)
          : constraints,
       super(key: key);

三枷邪、屬性介紹

\color{red}{alignment:Alignment}對齊方式,使用如下

屬性 說明
Alignment.topLeft 垂直靠頂部水平靠左對齊
Alignment.topCenter 垂直靠頂部水平居中對齊
Alignment.topRight 垂直靠頂部水平靠右對齊
Alignment.centerLeft 垂直居中水平靠左對齊
Alignment.center 垂直和水平居中都對齊
Alignment.centerRight 垂直居中水平靠右對齊
Alignment.bottomLeft 垂直靠底部水平靠左對齊
Alignment.bottomCenter 垂直靠底部水平居中對齊
Alignment.bottomRight 垂直靠底部水平靠右對齊
Alignment(double x, double y) 根據(jù)具體的x,y軸偏移量定位

\color{red}{padding:EdgeInsets}內(nèi)邊距诺凡,使用如下

屬性 說明
EdgeInsets.all(double value) 上下左右統(tǒng)一設置內(nèi)邊距
EdgeInsets.fromLTRB(double left,double top,double double right,double bottom) 分別設置左上右下的內(nèi)邊距

\color{red}{color:Color}背景色,不能與 decoration 屬性同時設置
\color{red}{decoration:Decoration}裝飾,也就是設置背景色践惑、邊框腹泌、圓角等,不能和color同時使用尔觉,Decoration是抽象類凉袱,一般使用BoxDecoration的實現(xiàn)類(FlutterLogoDecoration、ShapeDecoration侦铜、UnderlineTabIndicator也是Decoration的實現(xiàn)類)

BoxDecoration的源碼

const BoxDecoration({
    this.color,//背景填充顏色
    this.image,//使用圖片作為裝飾
    this.border,//可以設置邊框顏色专甩、邊框?qū)挾取⑦吙驑邮?    this.borderRadius,//邊框圓角
    this.boxShadow,//陰影效果
    this.gradient,//設置成漸變效果的背景钉稍,會覆蓋 color
    this.backgroundBlendMode,//混合模式(暫不了解)
    this.shape = BoxShape.rectangle,
  }) : assert(shape != null),
       assert(
         backgroundBlendMode == null || color != null || gradient != null,
         "backgroundBlendMode applies to BoxDecoration's background color or "
         'gradient, but no color or gradient was provided.'
       );

BoxDecoration屬性

屬性 說明
Color?color 背景填充顏色
DecorationImage?image 使用圖片作為裝飾
Border?border 可以設置邊框顏色涤躲、邊框?qū)挾取⑦吙驑邮?/td>
BorderRadius?borderRadius 邊框圓角
BoxShadow?boxShadow 陰影效果
LinearGradient?gradient 設置成漸變效果的背景贡未,會覆蓋 color
BlendMode?backgroundBlendMode 混合模式

\color{red}{foregroundDecoration:Decoration}裝飾种樱,繪制在child之上,使用方法同decoration
\color{red}{width:double}
\color{red}{height:double}
\color{red}{constraints}約束條件(暫不了解如何使用)
\color{red}{margin:EdgeInsets}外邊距俊卤,使用方法同padding
\color{red}{transform:Matrix4}形狀變化嫩挤,簡單使用如下

屬性 說明
Matrix4.translationValues(double x,double y,double z) 根據(jù)x,y,z的值進行平移
Matrix4.diagonal3Values(double x,double y,double z) 根據(jù)x,y,z的值進行縮放,正值放大消恍,負值縮小
Matrix4.rotationZ(double z) 根據(jù)z軸進行旋轉
Matrix4.rotationY(double y) 根據(jù)y軸進行旋轉
Matrix4.rotationX(double x) 根據(jù)x軸進行旋轉
Matrix4.skew(double x,double y) 根據(jù)x,y軸的值進行扭曲
Matrix4.skewX(double x) 根據(jù)x軸的值進行扭曲
Matrix4.skewY(double y)根據(jù)y軸的值進行扭曲

\color{red}{child:Widget}子組件

demo

class ContainerTest extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
        child: Container(
      //對齊方式
      alignment: Alignment.center,
      //內(nèi)邊距
      padding: EdgeInsets.fromLTRB(10.0, 20.0, 10.0, 20.0),
      //背景色
//          color: Colors.yellow,
      //裝飾
      decoration: BoxDecoration(
        //背景色
        color: Colors.red,
        //圖片地址
//        image: DecorationImage(image: NetworkImage("url")),
        //邊框
        border: Border.all(color: Color(0xff000000), width: 5.0),
        //圓角
        borderRadius: BorderRadius.only(
            topLeft: Radius.circular(10.0),
            topRight: Radius.circular(20.0),
            bottomLeft: Radius.circular(30.0),
            bottomRight: Radius.circular(40.0)),
        //陰影
        boxShadow: [BoxShadow(color: Color(0xff000000), blurRadius: 5.0)],
        //漸變色
        gradient: LinearGradient(
            colors: [Colors.amberAccent, Colors.blue, Colors.deepPurple]),
//          backgroundBlendMode: BlendMode.color //混合模式
      ),
      //裝飾岂昭,child之上
//      foregroundDecoration: BoxDecoration(),
      child: Text(
        "我是一個文本",
        style: TextStyle(color: Colors.red),
      ),
      //寬
      width: 300.0,
      //高
      height: 300.0,
      //外邊距
      margin: EdgeInsets.all(10.0),
      //根據(jù)x,y,z的值進行平移
//          transform:Matrix4.translationValues(20.0, 20.0, 20.0),
      //根據(jù)x,y,z的值進行縮放,正值放大狠怨,負值縮小
//            transform: Matrix4.diagonal3Values(-2, -2, 1),
      //根據(jù)z軸進行旋轉
//            transform: Matrix4.rotationZ(1.3),
      //根據(jù)y軸進行旋轉
//        transform: Matrix4.rotationY(1.3),
      //根據(jù)x軸進行旋轉
//      transform: Matrix4.rotationX(1.5),
      //扭曲约啊,根據(jù)x,y軸的值進行扭曲
//        transform: Matrix4.skew(1.5, 0.0),
      //扭曲,根據(jù)x軸的值進行扭曲
//        transform: Matrix4.skewX(1.3),
      //扭曲取董,根據(jù)y軸的值進行扭曲
      transform: Matrix4.skewY(-0.5),
    ));
  }
}
企業(yè)微信截圖_16056317458073.png

(整理出來棍苹,方便個人查找和學習)

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市茵汰,隨后出現(xiàn)的幾起案子枢里,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,270評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件栏豺,死亡現(xiàn)場離奇詭異彬碱,居然都是意外死亡,警方通過查閱死者的電腦和手機奥洼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評論 3 395
  • 文/潘曉璐 我一進店門巷疼,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人灵奖,你說我怎么就攤上這事嚼沿。” “怎么了瓷患?”我有些...
    開封第一講書人閱讀 165,630評論 0 356
  • 文/不壞的土叔 我叫張陵骡尽,是天一觀的道長。 經(jīng)常有香客問我擅编,道長攀细,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,906評論 1 295
  • 正文 為了忘掉前任爱态,我火速辦了婚禮谭贪,結果婚禮上,老公的妹妹穿的比我還像新娘锦担。我一直安慰自己俭识,他們只是感情好,可當我...
    茶點故事閱讀 67,928評論 6 392
  • 文/花漫 我一把揭開白布吆豹。 她就那樣靜靜地躺著鱼的,像睡著了一般。 火紅的嫁衣襯著肌膚如雪痘煤。 梳的紋絲不亂的頭發(fā)上凑阶,一...
    開封第一講書人閱讀 51,718評論 1 305
  • 那天,我揣著相機與錄音衷快,去河邊找鬼宙橱。 笑死,一個胖子當著我的面吹牛蘸拔,可吹牛的內(nèi)容都是我干的师郑。 我是一名探鬼主播,決...
    沈念sama閱讀 40,442評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼调窍,長吁一口氣:“原來是場噩夢啊……” “哼宝冕!你這毒婦竟也來了?” 一聲冷哼從身側響起邓萨,我...
    開封第一講書人閱讀 39,345評論 0 276
  • 序言:老撾萬榮一對情侶失蹤地梨,失蹤者是張志新(化名)和其女友劉穎菊卷,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體宝剖,經(jīng)...
    沈念sama閱讀 45,802評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡洁闰,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,984評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了万细。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片扑眉。...
    茶點故事閱讀 40,117評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖赖钞,靈堂內(nèi)的尸體忽然破棺而出腰素,到底是詐尸還是另有隱情,我是刑警寧澤仁烹,帶...
    沈念sama閱讀 35,810評論 5 346
  • 正文 年R本政府宣布耸弄,位于F島的核電站,受9級特大地震影響卓缰,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜砰诵,卻給世界環(huán)境...
    茶點故事閱讀 41,462評論 3 331
  • 文/蒙蒙 一征唬、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧茁彭,春花似錦总寒、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽妹萨。三九已至年枕,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間乎完,已是汗流浹背熏兄。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留树姨,地道東北人摩桶。 一個月前我還...
    沈念sama閱讀 48,377評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像帽揪,于是被迫代替她去往敵國和親硝清。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,060評論 2 355

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