Flutter 布局方式之線性布局

前言

目前Flutter常見的布局方式主要有4種:1、線性布局 2、流式布局 3、彈性布局 4、層疊布局

今天我們主要介紹線性布局

流式布局

彈性布局

層疊布局

線性布局

所謂線性布局,即指沿水平或垂直方向排布子組件。
** Flutter中通過Row和Column來實現(xiàn)線性布局,類似于Android中的LinearLayout。**
關(guān)鍵字Row、Column

1 水平方向(關(guān)鍵字Row)

class testState extends State<test> with BaseBar {
  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: defaultWidth, height: defaultHeight);
    return Scaffold(
        appBar: AppBaseBar("線性布局水平方向"),
        body: Row(
          children: [
            Container(
              margin: EdgeInsets.only(left: 30,top: 30),
              width: 100,
              height: 100,
              color: Colors.red,
              child: Text("1",style:TextStyle(
                color: Colors.white,
                fontSize: 60,
              )),
            ),
            Container(
              margin: EdgeInsets.only(left: 30,top: 30),
              width: 100,
              height: 100,
              color: Colors.yellow,
              child: Text("3",style:TextStyle(
                color: Colors.white,
                fontSize: 60,
              )),
            ),
            Container(
              margin: EdgeInsets.only(left: 30,top: 30),
              width: 100,
              height: 100,
              color: Colors.blue,
              child: Text("3",style:TextStyle(
                color: Colors.white,
                fontSize: 60,
              )),
            ),
          ],
        )
    );
  }
線性布局水平方向示例圖

2 垂直方向(關(guān)鍵字Column)

class testState extends State<test> with BaseBar {
  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: defaultWidth, height: defaultHeight);
    return Scaffold(
        appBar: AppBaseBar("線性布局水平方向"),
        body: Column(
          children: [
            Container(
              margin: EdgeInsets.only(left: 30,top: 30),
              width: 100,
              height: 100,
              color: Colors.red,
              child: Text("1",style:TextStyle(
                color: Colors.white,
                fontSize: 60,
              )),
            ),
            Container(
              margin: EdgeInsets.only(left: 30,top: 30),
              width: 100,
              height: 100,
              color: Colors.yellow,
              child: Text("3",style:TextStyle(
                color: Colors.white,
                fontSize: 60,
              )),
            ),
            Container(
              margin: EdgeInsets.only(left: 30,top: 30),
              width: 100,
              height: 100,
              color: Colors.blue,
              child: Text("3",style:TextStyle(
                color: Colors.white,
                fontSize: 60,
              )),
            ),
          ],
        )
    );
  }
}
線性布局垂直方向

3 線性布局的注意事項

當(dāng)我們布局超過一行的時候,這時候系統(tǒng)是不會幫我們換行的,也就是說會造成一個問題。UI溢出。


UI溢出

水平排列,使用Row對視圖進(jìn)行水平排列
垂直排列,使用Column對視圖進(jìn)行垂直排列
Flutter中子視圖超過父視圖會報視圖溢出

為了解決這個問題,我們就需要了解到Flutter第二個布局,流式布局

最后編輯于
?著作權(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)容

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