Flex佈局是Flutter的五虎上將之一,虎父無犬子,其子Row和Column也能力非凡
你有沒有被mainAxisAlignment,crossAxisAlignment弄得暈頭轉向,本文將助你將他們歸入麾下。
先看一下父子三人在Flutter佈局體系中的位置:多子組件佈局
bash
屬性名 | 類型 | 默認值 | 簡介 |
---|---|---|---|
direction | Axis | @required | 軸向 |
mainAxisAlignment | MainAxisAlignment | start | 主軸方向對齊方式 |
crossAxisAlignment | CrossAxisAlignment | center | 交叉軸方向對齊方式 |
mainAxisSize | MainAxisSize | max | 主軸尺寸 |
textDirection | TextDirection | null | 文本方向 |
verticalDirection | VerticalDirection | down | 豎直方向 |
textBaseline | TextBaseline | null | 基線類型 |
children | List | [] | 內部孩子 |
direction:Axis
enum Axis {
horizontal,//水平
vertical,//豎直
}
複製代碼
也就是水平排放仍是豎直排放,能夠看出默認狀況下都是主軸頂頭,交叉軸居中
好比horizontal下主軸爲水平軸,交叉軸則爲豎直。也就是水平頂頭,豎直居中
這裏使用MultiShower快速展現,更好的對比出不一樣之處,MultiShower詳見微信
var direction =[Axis.horizontal,Axis.vertical];
var show = MultiShower(direction,(e){
return Flex(
direction: e,
children: <Widget>[redBox,blueBox,yellowBox,greenBox],
);
},color: Colors.black12,width: 300,height: 200);
var redBox= Container(
color: Colors.red,
height: 50,
width: 50,
);
var blueBox= Container(
color: Colors.blue,
height: 30,
width: 60,
);
var yellowBox= Container(
color: Colors.yellow,
height: 50,
width: 100,
);
var greenBox= Container(
color: Colors.green,
height: 60,
width: 60,
);
複製代碼
mainAxisAlignment:MainAxisAlignment
主軸方向的排布規則,這裏以水平爲例,主軸爲水平方向。豎直類比便可ide
enum MainAxisAlignment {
start,//頂頭
end,//接尾
center,//居中
spaceBetween,//頂頭接尾,其餘均分
spaceAround,//中間的孩子均分,兩頭的孩子空一半
spaceEvenly,//均勻平分
複製代碼
testMainAxisAlignment(){
var redBox= Container(
color: Colors.red,
height: 50,
width: 50,
);
var blueBox= Container(
color: Colors.blue,
height: 30,
width: 60,
);
var yellowBox= Container(
color: Colors.yellow,
height: 10,
width: 10,
);
var greenBox= Container(
color: Colors.green,
height: 50,
width: 10,
);
var mainAxisAlignment =[
MainAxisAlignment.start,MainAxisAlignment.center,
MainAxisAlignment.end,MainAxisAlignment.spaceAround,
MainAxisAlignment.spaceBetween,MainAxisAlignment.spaceEvenly];
var show = MultiShower(mainAxisAlignment,(e){
return Flex(
direction: Axis.horizontal,
mainAxisAlignment: e,
children: <Widget>[redBox,blueBox,yellowBox,greenBox],
);
},color: Colors.black12,width: 200,height: 150);
return show;
}
複製代碼
crossAxisAlignment:CrossAxisAlignment
enum CrossAxisAlignment {
start,//頂頭
end,//接尾
center,//居中
stretch,//伸展
baseline,//基線
}
複製代碼
仍是水平爲例,交叉軸即是豎軸,這裏能夠看出他們的佈局行爲
其中須要注意的是CrossAxisAlignment.baseline
使用時必須有textBaseline
其中textBaseline肯定對齊的是那種基線,分爲alphabetic
和ideographic
佈局
testCrossAxisAlignment(){
var redBox= Container(
color: Colors.red,
height: 50,
width: 50,
);
var blueBox= Container(
color: Colors.blue,
height: 30,
width: 60,
);
var yellowBox= Container(
color: Colors.yellow,
height: 10,
width: 10,
);
var greenBox= Container(
color: Colors.green,
height: 50,
width: 10,
);
var crossAxisAlignment =[CrossAxisAlignment.start,CrossAxisAlignment.center,
CrossAxisAlignment.end,CrossAxisAlignment.stretch,CrossAxisAlignment.baseline];
var show = MultiShower(crossAxisAlignment,(e){
return Flex(
direction: Axis.horizontal,
crossAxisAlignment: e,
textBaseline: TextBaseline.alphabetic,//基線類型
children: <Widget>[redBox,blueBox,yellowBox,greenBox],
);
},color: Colors.black12,width: 200,height: 140);
return show;
}
複製代碼
enum MainAxisSize {
min,
max,
}
複製代碼
當父容器的寬未約束,Flex默認會將自身儘量延伸,這即是MainAxisSize.maxpost
此時改成MainAxisSize.min時,它不會延伸本身的區域,自會包裹內容ui
testMainAxisSize(){
var redBox= Container(
color: Colors.red,
height: 50,
width: 50,
);
var blueBox= Container(
color: Colors.blue,
height: 30,
width: 60,
);
var yellowBox= Container(
color: Colors.yellow,
height: 10,
width: 10,
);
var greenBox= Container(
color: Colors.green,
height: 50,
width: 10,
);
return Center(child: Flex(
direction: Axis.horizontal,
mainAxisSize: MainAxisSize.max,
children: <Widget>[redBox,blueBox,yellowBox,greenBox],
),);
}
複製代碼
textDirection:TextDirection
enum TextDirection {
ltr,//從左到右
rtl,//從右到左
}
複製代碼
這個很是好理解,很少言了spa
testTextDirection(){
var redBox= Container(
color: Colors.red,
height: 50,
width: 50,
);
var blueBox= Container(
color: Colors.blue,
height: 30,
width: 60,
);
var yellowBox= Container(
color: Colors.yellow,
height: 10,
width: 10,
);
var greenBox= Container(
color: Colors.green,
height: 50,
width: 10,
);
var textDirection =[TextDirection.ltr,TextDirection.rtl];
var show = MultiShower(textDirection,(e){
return Flex(
direction: Axis.horizontal,
textDirection: e,
children: <Widget>[redBox,blueBox,yellowBox,greenBox],
);
},color: Colors.black12,width: 200,height: 140);
return show;
}
複製代碼
verticalDirection:VerticalDirection
enum VerticalDirection{
up,
down,
}
複製代碼
testVerticalDirection(){
var redBox= Container(
color: Colors.red,
height: 50,
width: 50,
);
var blueBox= Container(
color: Colors.blue,
height: 30,
width: 60,
);
var yellowBox= Container(
color: Colors.yellow,
height: 10,
width: 10,
);
var greenBox= Container(
color: Colors.green,
height: 50,
width: 10,
);
var verticalDirection =[VerticalDirection.up,VerticalDirection.down];
var show = MultiShower(verticalDirection,(e){
return Flex(
direction: Axis.vertical,
verticalDirection: e
children: <Widget>[redBox,blueBox,yellowBox,greenBox],
);
},color: Colors.black12,width: 200,height: 140);
return show;
}
複製代碼
textBaseline:TextBaseline
enum TextBaseline {
alphabetic,
ideographic,
}
複製代碼
testTextBaseline(){
var redBox= Text(
"張風捷特烈",style: TextStyle(fontSize: 20,backgroundColor: Colors.red),
);
var blueBox= Text(
"toly",style: TextStyle(fontSize: 50,backgroundColor: Colors.blue),
);
var yellowBox= Text(
"1994",style: TextStyle(fontSize: 30,backgroundColor: Colors.green),
);
var textBaseline =[TextBaseline.alphabetic,TextBaseline.ideographic];
var show = MultiShower(textBaseline,(e){
return Flex(
direction: Axis.horizontal,
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: e,
children: <Widget>[redBox,blueBox,yellowBox],
);
},color: Colors.black12,width: 300,height: 140);
return show;
}
複製代碼
至此,Flutter中的Flex佈局就已經徹底解讀完了。
其中Flutter的孩子Row是direction: Axis.horizontal,
Column是direction: Axis.vertical,
其餘的屬性都是相似的,至關於Flex的簡化版,因此Flex在手,天下我有。3d
還要一點是關於Expanded,也比較保用,它能與Flex佈局結合,變動孩子尺寸code
c1:綠色 c2:紅色 c3:黃色
1).Expanded--c2:c1和c3將不變,c2延伸本身佔滿剩餘部分
2).同時Expanded--c2和c3,最終c2和c3的長度是同樣的
3).同時Expanded--c1,c2和c3,最終c1,c2,c3長度都是同樣的
複製代碼
光說不練假把式,光練不說空把式,下面就用一個佈局來實際看一下Flex的強大cdn
1.由上中下三行,能夠用Column
2.第一行由圖標,文字和文字組成,其中兩頭分處左右,能夠用Expanded處理
3.中間比較複雜由一個Row中包含兩部分,左邊是一個兩行Column的內容,右邊是文字
4.底部是一個Row,主軸對齊是start
複製代碼
showItem() {
var infoStyle = TextStyle(color: Color(0xff999999), fontSize: 13);
var littleStyle = TextStyle(color: Colors.black, fontSize: 16);
var top = Row(//頂部,經過Flex佈局的Row進行橫向排列,Expanded中間
children: <Widget>[
Image.asset("images/icon_head.png", width: 20, height: 20),
Expanded(
child: Padding(
child: Text("張風捷特烈"),
padding: EdgeInsets.only(left: 4),
),
),
Text(
"Flutter/Dart",
style: infoStyle,
)
],
);
var content = Column(//中間文字內容,交叉軸爲start
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"[Flutter必備]-Flex佈局徹底解讀",
style: littleStyle,
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
Padding(
child: Text("也就是水平排放仍是豎直排放,能夠看出默認狀況下都是主軸頂頭,"
"交叉軸居中好比horizontal下主軸爲水平軸,交叉軸則爲豎直。也就是水平頂頭,豎直居中"
"這裏使用MultiShower快速展現,更好的對比出不一樣之處",
style: infoStyle, maxLines: 2, overflow: TextOverflow.ellipsis),
padding: EdgeInsets.only(top: 5),
),
],
);
var center = Row(//中間的部分
children: <Widget>[
Expanded(
child: Padding(
child: content,
padding: EdgeInsets.all(5),
)),
ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(5)),
child: Image.asset("images/wy_300x200.jpg",
width: 80, height: 80, fit: BoxFit.cover),)
],
);
var end = Row(//底部
children: <Widget>[
Icon(
Icons.grade,
color: Colors.green,
size: 20,
),
Text(
"1000W",
style: infoStyle,
),
Padding(child:Icon(Icons.tag_faces, color: Colors.lightBlueAccent, size: 20),
padding: EdgeInsets.symmetric(horizontal: 5),),
Text("2000W", style: infoStyle),
],
);
var result = Card(//整體拼合
child: Container(
height: 160,
color: Colors.white,
padding: EdgeInsets.all(10),
child: Column(children: <Widget>[top, Expanded(child: center), end])));
return result;
}
複製代碼
本文到此接近尾聲了,若是想快速嚐鮮Flutter,《Flutter七日》會是你的必備佳品;若是想細細探究它,那就跟隨個人腳步,完成一次Flutter之旅。
另外本人有一個Flutter微信交流羣,歡迎小夥伴加入,共同探討Flutter的問題,本人微信號:zdl1994328
,期待與你的交流與切磋。