Flutter 組件樣式

在 Flutter 中的組件樣式,都是通過組件上的 style 屬性進行設(shè)置的,這與 React Native 很類似。

例如,在 Text 組件里設(shè)置樣式。

new Text('Hello',
    style: new TextStyle(
        fontSize: 24.0,
        fontWeight: FontWeight.w900,
        fontFamily: "Georgia",
    )
);

與 React Native 不同的是,有一些樣式不不能在 style 里面設(shè)置的。例如 width,height,color 等屬性。因為 Flutter 認(rèn)為這樣應(yīng)該是組件的屬性而不是樣式。

new Text(
    'Hello',
    style: new TextStyle(
        fontSize: 24.0,
        fontWeight: FontWeight.w900,
        fontFamily: "Georgia",
    ),
    textAlign: TextAlign.center,
)

容器大小

var container = new Container(
    width: 320.0,
    height: 240.0,
);

容器邊距

邊距只要是 padding(內(nèi)邊距) 和 margin(外邊距)兩個設(shè)置。邊距只適用于 Container。

new Container(
    padding: new EdgeInsets.all(20.0),
    // padding: 20px;

    padding: new EdgeInsets.only(left: 30.0, right: 0.0, top: 20.0, bottom: 20.0),
    // padding-left: 30px;
    // padding-right: 0;
    // padding-top: 20px;
    // padding-bottom: 20px;

    padding: new EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
    // padding: 10px 20px;

    // 同理,對于 margin 也是一樣
    margin: new EdgeInsets.all(20.0),
)

位置信息

如果要使用絕對定位,那么需要把內(nèi)容包裹在 Positioned 容器里,而 Positioned 又需要包裹在 Stack 容器里。

var container = new Container(
    child: new Stack(
        children: [
            new Positioned(
                child:  new Container(
                    child: new Text("Lorem ipsum"),
                ),
                left: 24.0,
                top: 24.0,
            ),
        ],
    ),
    width: 320.0,
    height: 240.0,
);

容器邊框

容器的邊框設(shè)置,使用 Border 對象。邊框只適用于 Container。

decoration: new BoxDecoration(
    color: Colors.red[400],
    // 這里設(shè)置
    border: new Border.all(width: 2.0, style: BorderStyle.solid),
),

容器圓角

要設(shè)置容器的圓角,使用 BorderRadius 對象,它只能使用于 Container。

new Container(
    decoration: new BoxDecoration(
        color: Colors.red[400],
        // 這里設(shè)置
        borderRadius: new BorderRadius.all(
            const Radius.circular(8.0),
        ),
    ),
    padding: new EdgeInsets.all(16.0),
),

BorderRadius 有以下的屬性與方法。

  • BorderRadius.all() - 創(chuàng)建所有半徑的邊界半徑 radius。
  • BorderRadius.circular() - 同時設(shè)置四個圓角。
  • BorderRadius.horizo??ntal() - 在水平方向上設(shè)置圓角。
  • BorderRadius.only() - 只設(shè)置某個角。
  • BorderRadius.vertical() - 在垂直方向上設(shè)置圓角。
    borderRadius: new BorderRadius.circular(5.0));

陰影效果

在 Flutter 里設(shè)置陰影效果,需要使用 BoxShadow 對象。陰影效果只適用于 Container。

decoration: new BoxDecoration(
    color: Colors.red,
    boxShadow: <BoxShadow>[
        new BoxShadow (
            offset: new Offset(0.0, 2.0),   // (x, y)
            blurRadius: 4.0,
            color: const Color(0xcc000000),
        ),
        new BoxShadow (
            offset: new Offset(0.0, 6.0),
            blurRadius: 20.0,
            color: const Color(0x80000000),
        ),
    ],
),

等效于 css 上的陰影效果設(shè)置。

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

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

  • 國慶后面兩天在家學(xué)習(xí)整理了一波flutter,基本把能擼過能看到的代碼都過了一遍,此文篇幅較長,建議保存(star...
    Nealyang閱讀 4,447評論 1 17
  • 回顧發(fā)現(xiàn)的新原則,覺得真的很正確,在這里補充一下哈 我們都知道,HTML負(fù)責(zé)結(jié)構(gòu),就像人的頭骨,決定著整體。css...
    春木橙云閱讀 300評論 0 0
  • 不知道在什么時候,拖延它就賴上了我! 媽媽要我?guī)兔ψ龅氖?,總是不能快速的做完,總會想著,不急不急,等?..
    coolyu閱讀 219評論 3 1
  • 一勺砂糖, 一勺牛奶咖啡, 等待的小小玻璃杯, 圍著開水, 慢慢沉醉。 一杯, 洋溢香的氣息, 忘卻了世俗塵灰.。...
    伊凡軒閱讀 215評論 0 0
  • 錦璱年華 名片頁
    錦璱年華閱讀 405評論 0 4

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