Flutter學(xué)習(xí)--Container組件

一、介紹

Container 組件是一個(gè)方便繪制、定位和調(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}對(duì)齊方式,使用如下

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

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

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

\color{red}{color:Color}背景色,不能與 decoration 屬性同時(shí)設(shè)置
\color{red}{decoration:Decoration}裝飾,也就是設(shè)置背景色、邊框、圓角等,不能和color同時(shí)使用,Decoration是抽象類(lèi),一般使用BoxDecoration的實(shí)現(xiàn)類(lèi)(FlutterLogoDecoration、ShapeDecoration、UnderlineTabIndicator也是Decoration的實(shí)現(xiàn)類(lèi))

BoxDecoration的源碼

const BoxDecoration({
    this.color,//背景填充顏色
    this.image,//使用圖片作為裝飾
    this.border,//可以設(shè)置邊框顏色、邊框?qū)挾?、邊框樣?    this.borderRadius,//邊框圓角
    this.boxShadow,//陰影效果
    this.gradient,//設(shè)置成漸變效果的背景,會(huì)覆蓋 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屬性

屬性 說(shuō)明
Color?color 背景填充顏色
DecorationImage?image 使用圖片作為裝飾
Border?border 可以設(shè)置邊框顏色、邊框?qū)挾?、邊框樣?/td>
BorderRadius?borderRadius 邊框圓角
BoxShadow?boxShadow 陰影效果
LinearGradient?gradient 設(shè)置成漸變效果的背景,會(huì)覆蓋 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}形狀變化,簡(jiǎn)單使用如下

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

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

demo

class ContainerTest extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
        child: Container(
      //對(duì)齊方式
      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(
        "我是一個(gè)文本",
        style: TextStyle(color: Colors.red),
      ),
      //寬
      width: 300.0,
      //高
      height: 300.0,
      //外邊距
      margin: EdgeInsets.all(10.0),
      //根據(jù)x,y,z的值進(jìn)行平移
//          transform:Matrix4.translationValues(20.0, 20.0, 20.0),
      //根據(jù)x,y,z的值進(jìn)行縮放,正值放大,負(fù)值縮小
//            transform: Matrix4.diagonal3Values(-2, -2, 1),
      //根據(jù)z軸進(jìn)行旋轉(zhuǎn)
//            transform: Matrix4.rotationZ(1.3),
      //根據(jù)y軸進(jìn)行旋轉(zhuǎn)
//        transform: Matrix4.rotationY(1.3),
      //根據(jù)x軸進(jìn)行旋轉(zhuǎn)
//      transform: Matrix4.rotationX(1.5),
      //扭曲,根據(jù)x,y軸的值進(jìn)行扭曲
//        transform: Matrix4.skew(1.5, 0.0),
      //扭曲,根據(jù)x軸的值進(jìn)行扭曲
//        transform: Matrix4.skewX(1.3),
      //扭曲,根據(jù)y軸的值進(jìn)行扭曲
      transform: Matrix4.skewY(-0.5),
    ));
  }
}
企業(yè)微信截圖_16056317458073.png

(整理出來(lái),方便個(gè)人查找和學(xué)習(xí))

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容