center == Align - alignment.center
markdown
Align(
// alignment: Alignment.topCenter,
// alignment: Alignment.center,
alignment: Alignment(0, 0),
child: Icon(
Icons.pets,
size: 50,
),
);
複製代碼
Padding(
// padding: EdgeInsets.only(bottom: 5),
// padding: EdgeInsets.all(5),
// padding: EdgeInsets.fromLTRB(5, 5, 10, 10),
padding: EdgeInsets.all(5),
child: Text(
"江山代有才人出",
style: TextStyle(
fontSize: 20,
color: Colors.white,
backgroundColor: Colors.black12),
),
),
複製代碼
注意點:less
child 內邊距
ide
外邊距
佈局
class ContainerWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
// color: Colors.red, //背景顏色
padding: EdgeInsets.all(20), // child 內邊距
margin: EdgeInsets.all(5), //container 外邊距
width: 200,
height: 200,
// 若是 alignment 有的話才建立 align ,就變成 Container -> Align -> Text
// 若是沒有設置 alignment,默認會把 child(Text) 拉滿, 所以看起來 Text 並非中間對齊 Container -> Text
// alignment: Alignment.center, //child 對齊方式 (text 未對齊是由於未填充滿, 默認是 center )
// 使用 裝飾 會和 color 屬性衝突,所以使用該對象就在該對象裏設置背景顏色
decoration: BoxDecoration(
color: Colors.red, //背景顏色
border: Border.all(color: Colors.blue, width: 5), //邊框
borderRadius: BorderRadius.circular(10), //圓角
//陰影 是 list, 可添加多個陰影
boxShadow: [
BoxShadow(
color: Colors.orange, //陰影顏色
offset: Offset(10, 10), //陰影偏移
spreadRadius: 5, //偏移增加(在offset基礎上)
blurRadius: 10, //模糊度
)
]),
// child: Text("我是Joho"),
child: Icon(
Icons.pets,
// size: 50,
color: Colors.white,
),
);
}
}
複製代碼
子類有 Row、Column, 經過設置 direction 區分flex
Row == direction: Axis.horizontal
Column == direction: Axis.vertical
複製代碼
DEMOui
class RowWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Row(
//默認是start,即左邊
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: [
Container(
color: Colors.red,
width: 60,
height: 50,
),
Container(
color: Colors.blue,
width: 80,
height: 100,
),
Container(
color: Colors.purple,
width: 40,
height: 90,
),
Container(
color: Colors.orange,
width: 90,
height: 120,
),
],
);
}
}
複製代碼
經常使用屬性
spa
Flexible(
flex: 1,
fit: FlexFit.tight,
child: Container(
color: Colors.green,
width: 10,
height: 120,
),
),
複製代碼
Expanded(
flex: 1,
child: Container(
color: Colors.black,
width: 100,
height: 150,
)),
複製代碼
class StackWidget extends StatefulWidget {
@override
_StackWidgetState createState() => _StackWidgetState();
}
class _StackWidgetState extends State<StackWidget> {
bool _isfavor = false;
@override
Widget build(BuildContext context) {
return Stack(
children: [
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(10)),
border: Border.all(color: Colors.blue, width: 5), //邊框
),
child: Image.asset(
"images/baidu.png",
width: double.infinity,
fit: BoxFit.cover,
),
),
Positioned(
bottom: 0,
right: 0,
left: 0,
child: Container(
color: Colors.black26,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Joho",
style: TextStyle(color: Colors.white, fontSize: 25),
),
IconButton(
icon: Icon(
Icons.favorite,
color: _isfavor ? Colors.red : Colors.white,
),
onPressed: () {
setState(() {
_isfavor = !_isfavor;
});
})
],
),
))
],
);
}
}
複製代碼