1 Container

aligment:子部件对齐的位置 — Alignment(x,y),x,y取值-1~1
color:背景颜色
constraints:BoxConstraints
padding (内边距)
margin (外边距)
decoration:BoxDecoration
可用于设置:背景图的渐变,边框,圆角 ,阴影
transform:旋转

Container的使用

  1. @override
  2. Widget build(BuildContext context) {
  3. return Container(
  4. color: Colors.grey[100] ,//背景颜色
  5. child: Row(
  6. children: <Widget>[
  7. Container(
  8. child: Icon(Icons.pool, size:32.0, color:Colors.white),
  9. color: Color.fromRGBO(3, 53, 255, 1.0),
  10. padding: EdgeInsets.only(left: 24.0,right: 8.0),//内边距
  11. margin: EdgeInsets.all(8.0),//外边距
  12. width: 90.0,//宽度
  13. height: 90.0,//高度
  14. ),
  15. ],
  16. ),
  17. );
  18. }

Decoration的使用

  1. Widget build(BuildContext context) {
  2. return Container(
  3. // color: Colors.grey[100] ,//背景颜色
  4. decoration: BoxDecoration(
  5. image: DecorationImage(//背景图片 ,不能与背景色同时使用
  6. image: NetworkImage('https://resources.ninghao.org/images/say-hello-to-barry.jpg'),
  7. alignment: Alignment.topCenter,
  8. repeat: ImageRepeat.repeatY,//是否重复
  9. fit: BoxFit.cover,//填充模式
  10. colorFilter: ColorFilter.mode(//颜色滤镜
  11. Colors.indigoAccent[400].withOpacity(0.5),
  12. BlendMode.hardLight//混合模式
  13. ),
  14. ),
  15. ),
  16. child: Row(
  17. mainAxisAlignment: MainAxisAlignment.center,//主轴对齐方式,显示居中
  18. children: <Widget>[
  19. Container(
  20. child: Icon(Icons.pool, size:32.0, color:Colors.white),
  21. padding: EdgeInsets.only(left: 24.0,right: 8.0),//内边距
  22. margin: EdgeInsets.all(8.0),//外边距
  23. width: 90.0,//宽度
  24. height: 90.0,//高度
  25. decoration: BoxDecoration(
  26. color: Color.fromRGBO(3, 53, 255, 1.0),//这里添加背景设置的话,尚层Container不能设置color
  27. border: Border.all(
  28. color: Colors.indigoAccent[100],
  29. width: 3.0,
  30. style: BorderStyle.solid,
  31. ),
  32. // border: Border(//边框 这里也可以用Border.all()设置四边款统一样式
  33. // top: BorderSide(
  34. // color: Colors.indigoAccent[100],
  35. // width: 3.0,
  36. // style: BorderStyle.solid,
  37. // ),
  38. // bottom: BorderSide(
  39. // color: Colors.red[300],
  40. // width: 3.0,
  41. // style: BorderStyle.solid,
  42. // ),
  43. // left: BorderSide(
  44. // color: Colors.indigoAccent[200],
  45. // width: 3.0,
  46. // style: BorderStyle.solid,
  47. // ),
  48. // right: BorderSide(
  49. // color: Colors.indigoAccent[200],
  50. // width: 3.0,
  51. // style: BorderStyle.solid,
  52. // ),
  53. // ),
  54. // borderRadius: BorderRadius.circular(16.0),//边框圆角 BorderRadius.only(topLeft:16)
  55. boxShadow: [//阴影效果
  56. BoxShadow(
  57. offset: Offset(0, 16.0),//阴影在X轴和Y轴上的偏移
  58. color: Color.fromRGBO(16, 20, 188, 10),//阴影颜色
  59. blurRadius: 25.0 ,//阴影程度
  60. spreadRadius: -9.0, //阴影扩散的程度 取值可以正数,也可以是负数
  61. ),
  62. ],
  63. shape: BoxShape.circle,//装饰器的形状 如果设置成圆形,不能设置borderRadius
  64. // gradient: RadialGradient( //径向渐变效果RadialGradient 线性渐变效果LinearGradient
  65. // colors: [
  66. // Color.fromRGBO(7, 102, 255, 1.0),
  67. // Color.fromRGBO(3, 28, 128, 1.0),
  68. // ]
  69. gradient: LinearGradient( //径向渐变效果RadialGradient 线性渐变效果LinearGradient
  70. colors: [
  71. Color.fromRGBO(7, 102, 255, 1.0),
  72. Color.fromRGBO(3, 28, 128, 1.0),
  73. ],
  74. begin: Alignment.topCenter,
  75. end : Alignment.bottomCenter,
  76. ),
  77. ),
  78. ),
  79. ],
  80. ),
  81. );
  82. }

背景图

  1. Container(
  2. // color: Colors.grey[100] ,//背景颜色
  3. decoration: BoxDecoration(
  4. image: DecorationImage(//背景图片 ,不能与背景色同时使用
  5. image: NetworkImage('https://resources.ninghao.org/images/say-hello-to-barry.jpg'),
  6. alignment: Alignment.topCenter,
  7. repeat: ImageRepeat.repeatY,//是否重复
  8. fit: BoxFit.cover,//填充模式
  9. colorFilter: ColorFilter.mode(//颜色滤镜
  10. Colors.indigoAccent[400].withOpacity(0.5),
  11. BlendMode.hardLight//混合模式
  12. ),
  13. ),
  14. ),
  15. )

阴影和渐变

  1. Container(
  2. child: Icon(Icons.pool, size:32.0, color:Colors.white),
  3. padding: EdgeInsets.only(left: 24.0,right: 8.0),//内边距
  4. margin: EdgeInsets.all(8.0),//外边距
  5. width: 90.0,//宽度
  6. height: 90.0,//高度
  7. decoration: BoxDecoration(
  8. color: Color.fromRGBO(3, 53, 255, 1.0),//这里添加背景设置的话,尚层Container不能设置color
  9. border: Border.all(
  10. color: Colors.indigoAccent[100],
  11. width: 3.0,
  12. style: BorderStyle.solid,
  13. ),
  14. // borderRadius: BorderRadius.circular(16.0),//边框圆角 BorderRadius.only(topLeft:16)
  15. boxShadow: [//阴影效果
  16. BoxShadow(
  17. offset: Offset(0, 16.0),//阴影在X轴和Y轴上的偏移
  18. color: Color.fromRGBO(16, 20, 188, 10),//阴影颜色
  19. blurRadius: 25.0 ,//阴影程度
  20. spreadRadius: -9.0, //阴影扩散的程度 取值可以正数,也可以是负数
  21. ),
  22. ],
  23. shape: BoxShape.circle,//装饰器的形状 如果设置成圆形,不能设置borderRadius
  24. // gradient: RadialGradient( //径向渐变效果RadialGradient 线性渐变效果LinearGradient
  25. // colors: [
  26. // Color.fromRGBO(7, 102, 255, 1.0),
  27. // Color.fromRGBO(3, 28, 128, 1.0),
  28. // ]
  29. gradient: LinearGradient( //径向渐变效果RadialGradient 线性渐变效果LinearGradient
  30. colors: [
  31. Color.fromRGBO(7, 102, 255, 1.0),
  32. Color.fromRGBO(3, 28, 128, 1.0),
  33. ],
  34. begin: Alignment.topCenter,
  35. end : Alignment.bottomCenter,
  36. ),
  37. ),
  38. )

边框和圆角

  1. Container(
  2. child: Icon(Icons.pool, size:32.0, color:Colors.white),
  3. padding: EdgeInsets.only(left: 24.0,right: 8.0),//内边距
  4. margin: EdgeInsets.all(8.0),//外边距
  5. width: 90.0,//宽度
  6. height: 90.0,//高度
  7. decoration: BoxDecoration(
  8. color: Color.fromRGBO(3, 53, 255, 1.0),//这里添加背景设置的话,尚层Container不能设置color
  9. border: Border(//边框 这里也可以用Border.all()设置四边款统一样式
  10. top: BorderSide(
  11. color: Colors.indigoAccent[100],
  12. width: 3.0,
  13. style: BorderStyle.solid,
  14. ),
  15. bottom: BorderSide(
  16. color: Colors.red[300],
  17. width: 3.0,
  18. style: BorderStyle.solid,
  19. ),
  20. left: BorderSide(
  21. color: Colors.indigoAccent[200],
  22. width: 3.0,
  23. style: BorderStyle.solid,
  24. ),
  25. right: BorderSide(
  26. color: Colors.indigoAccent[200],
  27. width: 3.0,
  28. style: BorderStyle.solid,
  29. ),
  30. ),
  31. borderRadius: BorderRadius.circular(16.0),//边框圆角 BorderRadius.only(topLeft:16)
  32. ),
  33. ),

2 Scaffold

可设置的属性:
AppBar
body
bottomNavigationBar:BottomNavigationBar
floatingActionButton
drawer
endDrawer

  1. class ScaffoldRoute extends StatefulWidget {
  2. @override
  3. _ScaffoldRouteState createState() => _ScaffoldRouteState();
  4. }
  5. class _ScaffoldRouteState extends State<ScaffoldRoute> {
  6. int _selectedIndex = 1;
  7. @override
  8. Widget build(BuildContext context) {
  9. return Scaffold(
  10. appBar: AppBar( //导航栏
  11. title: Text("App Name"),
  12. actions: <Widget>[ //导航栏右侧菜单
  13. IconButton(icon: Icon(Icons.share), onPressed: () {}),
  14. ],
  15. ),
  16. drawer: new MyDrawer(), //抽屉
  17. bottomNavigationBar: BottomNavigationBar( // 底部导航
  18. items: <BottomNavigationBarItem>[
  19. BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('Home')),
  20. BottomNavigationBarItem(icon: Icon(Icons.business), title: Text('Business')),
  21. BottomNavigationBarItem(icon: Icon(Icons.school), title: Text('School')),
  22. ],
  23. currentIndex: _selectedIndex,
  24. fixedColor: Colors.blue,
  25. onTap: _onItemTapped,
  26. ),
  27. floatingActionButton: FloatingActionButton( //悬浮按钮
  28. child: Icon(Icons.add),
  29. onPressed:_onAdd
  30. ),
  31. );
  32. }
  33. void _onItemTapped(int index) {
  34. setState(() {
  35. _selectedIndex = index;
  36. });
  37. }
  38. void _onAdd(){
  39. }
  40. }

3 AppBar

  1. AppBar({
  2. Key key,
  3. this.leading, //导航栏最左侧Widget,常见为抽屉菜单按钮或返回按钮。
  4. this.automaticallyImplyLeading = true, //如果leading为null,是否自动实现默认的leading按钮
  5. this.title,// 页面标题
  6. this.actions, // 导航栏右侧菜单
  7. this.bottom, // 导航栏底部菜单,通常为Tab按钮组
  8. this.elevation = 4.0, // 导航栏阴影
  9. this.centerTitle, //标题是否居中
  10. this.backgroundColor,
  11. ... //其它属性见源码注释
  12. })

4 Column (垂直布局) & Row (水平布局)

主轴属性设置mainAxisAlignment

  1. enum MainAxisAlignment {
  2. //将子控件放在主轴的开始位置
  3. start,
  4. //将子控件放在主轴的结束位置
  5. end,
  6. //将子控件放在主轴的中间位置
  7. center,
  8. //将主轴空白位置进行均分,排列子元素,手尾没有空隙
  9. spaceBetween,
  10. //将主轴空白区域均分,使中间各个子控件间距相等,首尾子控件间距为中间子控件间距的一半
  11. spaceAround,
  12. //将主轴空白区域均分,使各个子控件间距相等
  13. spaceEvenly,
  14. }

交叉轴 crossAxisAlignment

在Row(水平排列)控件中,CrossAxisAlignment的方向就是垂直的,在 Column(垂直排列)控件中,CrossAxisAlignment的方向就是水平的。

  1. enum CrossAxisAlignment {
  2. //将子控件放在交叉轴的起始位置
  3. start,
  4. //将子控件放在交叉轴的结束位置
  5. end,
  6. //将子控件放在交叉轴的中间位置
  7. center,
  8. //使子控件填满交叉轴
  9. stretch,
  10. //将子控件放在交叉轴的上,并且与基线相匹配(不常用)
  11. baseline,
  12. }

案例

  1. Column(
  2. //测试Row对齐方式,排除Column默认居中对齐的干扰
  3. crossAxisAlignment: CrossAxisAlignment.start,
  4. children: <Widget>[
  5. Row(
  6. mainAxisAlignment: MainAxisAlignment.center,
  7. children: <Widget>[
  8. Text(" hello world "),
  9. Text(" I am Jack "),
  10. ],
  11. ),
  12. Row(
  13. mainAxisSize: MainAxisSize.min,
  14. mainAxisAlignment: MainAxisAlignment.center,
  15. children: <Widget>[
  16. Text(" hello world "),
  17. Text(" I am Jack "),
  18. ],
  19. ),
  20. Row(
  21. mainAxisAlignment: MainAxisAlignment.end,
  22. textDirection: TextDirection.rtl,
  23. children: <Widget>[
  24. Text(" hello world "),
  25. Text(" I am Jack "),
  26. ],
  27. ),
  28. Row(
  29. crossAxisAlignment: CrossAxisAlignment.start,
  30. verticalDirection: VerticalDirection.up,
  31. children: <Widget>[
  32. Text(" hello world ", style: TextStyle(fontSize: 30.0),),
  33. Text(" I am Jack "),
  34. ],
  35. ),
  36. ],
  37. );

让里面的Row占满外部Row,可以使用Expanded 组件

  1. class RowDemo extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return Row(
  5. mainAxisAlignment: MainAxisAlignment.start,
  6. crossAxisAlignment: CrossAxisAlignment.baseline,
  7. textBaseline: TextBaseline.ideographic,
  8. //alphabetic 英文字符
  9. //ideographic 中文字符
  10. children: <Widget>[
  11. Expanded(
  12. child: Container(
  13. height: 80,
  14. color: Colors.white,
  15. child: Text(
  16. 'hello',
  17. style: TextStyle(fontSize: 15),
  18. ),
  19. ),
  20. ),
  21. Expanded(
  22. child: Container(
  23. height: 80,
  24. color: Colors.blue,
  25. child: Text(
  26. '嘿嘿12312312',
  27. style: TextStyle(fontSize: 30),
  28. ),
  29. ),
  30. ),
  31. Expanded(
  32. child: Container(
  33. height: 80,
  34. color: Colors.red,
  35. child: Text(
  36. 'heihei',
  37. style: TextStyle(fontSize: 60),
  38. ),
  39. ),
  40. ),
  41. ],
  42. );
  43. }
  44. }

Row里面嵌套Row,或者Column里面再嵌套Column,那么只有最外面的Row或Column会占用尽可能大的空间,里面Row或Column所占用的空间为实际大小

6 Center ( 居中布局)

  1. class Center extends Align {
  2. const Center({ Key key, double widthFactor, double heightFactor, Widget child })
  3. : super(key: key, widthFactor: widthFactor, heightFactor: heightFactor, child: child);
  4. }

Center继承自Align,比Align只少了一个alignment参数
相当于Align的构造函数中alignment值为Alignment.center

  1. DecoratedBox(
  2. decoration: BoxDecoration(color: Colors.red),
  3. child: Center(
  4. child: Text("xxx"),
  5. ),
  6. ),
  7. DecoratedBox(
  8. decoration: BoxDecoration(color: Colors.red),
  9. child: Center(
  10. widthFactor: 1,
  11. heightFactor: 1,
  12. child: Text("xxx"),
  13. ),
  14. )

image.png