当前位置:   article > 正文

(七)Flutter 布局 创建小部件 自定义view 关于布局的一些属性 mainAxis SizeBox Alignment stack AspectRatio ConstrainedBox_flutter crossaxisalignment 自定义

flutter crossaxisalignment 自定义

主要内容

  • 创建LayoutDemo小部件
  • 创建可配置图标徽章IconBadege 小部件
  • Row与Column
  • mainAxis:主轴与crossAxis 交叉轴
  • SizeBox 固定尺寸的盒子
  • Alignment 对齐
  • stack 一摞小部件
  • AspectRatio 宽高比
  • ConstrainedBox 带限制的盒子 

 

关于前两个就不多做说明 又java基础的就能看懂 没有java基础的我讲了你也不懂 所以我就直接上代码了

flutter 的Row 和Column

就相当于

android 的orientation 属性 的horizontal 一行 与 vertical 一列

 

mainAxisAlignment 主轴的对齐方式 

这个主轴 是相对于Cloumn 或者Row来说的

在Cloumn中  那么主轴就是垂直 

在row 中   那么主轴就是水平

默认值是start

  1. mainAxisAlignment: MainAxisAlignment.start,
  2. mainAxisAlignment: MainAxisAlignment.end,
  3. mainAxisAlignment: MainAxisAlignment.center,
  4. //把空间分配的小控件的周围
  5. mainAxisAlignment: MainAxisAlignment.spaceAround,
  6. //把空间分配的小控件的之间
  7. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  8. //把空间平均分配的小控件的之间
  9. mainAxisAlignment: MainAxisAlignment.spaceEvenly,

就是靠前显示

 

crossAxisAlignment 交叉轴的对齐方式

这个交叉轴 是相对于主轴的交叉出现的 他们有很多数值 

在Cloumn中 因为主轴就是垂直  那么交叉轴就是水平

在row 中   因为主轴就是水平 那么交叉轴就是垂直

默认值是

crossAxisAlignment: CrossAxisAlignment.center,

还有其他属性

  1. //拉伸
  2. crossAxisAlignment: CrossAxisAlignment.stretch,

start end就不多讲了

上案列  自定义Icon控件 并传入大小

  1. import 'package:flutter/material.dart';
  2. class LayoutDemo extends StatelessWidget{
  3. @override
  4. Widget build(BuildContext context) {
  5. return Container(
  6. child: Column(
  7. mainAxisAlignment: MainAxisAlignment.start,
  8. crossAxisAlignment: CrossAxisAlignment.stretch,
  9. children: <Widget>[
  10. IconBadge(Icons.pool),
  11. IconBadge(Icons.beach_access,size: 64.0,),
  12. IconBadge(Icons.airplanemode_active),
  13. ],
  14. )
  15. );
  16. }
  17. }
  18. class IconBadge extends StatelessWidget{
  19. final IconData icon;
  20. final double size;
  21. IconBadge(this.icon,{this.size=32.0});
  22. @override
  23. Widget build(BuildContext context) {
  24. // TODO: implement build
  25. return Container(
  26. child: Icon(icon,size:size,color: Colors.white),
  27. width: size+60,
  28. height: size+60,
  29. color: Color.fromRGBO(3, 54, 255, 1.0),
  30. );
  31. }
  32. }

sizedBox 这个不仅仅可以设置空间的大小 还可以用来巧妙的设置成间距

  1. return Container(
  2. child: Column(
  3. mainAxisAlignment: MainAxisAlignment.center,
  4. children: <Widget>[
  5. SizedBox(
  6. width: 200.0,
  7. height: 300.0,
  8. child: Container(
  9. decoration: BoxDecoration(
  10. color: Color.fromRGBO(3, 54, 255, 1.0),
  11. borderRadius: BorderRadius.circular(8.0),
  12. ),
  13. child: Icon(Icons.ac_unit,color: Colors.white,size: 32.0,),
  14. ),
  15. ),
  16. SizedBox(
  17. height:20.0
  18. ),
  19. SizedBox(
  20. width: 100.0,
  21. height: 100.0,
  22. child: Container(
  23. decoration: BoxDecoration(
  24. color: Color.fromRGBO(3, 54, 255, 1.0),
  25. borderRadius: BorderRadius.circular(8.0),
  26. ),
  27. child: Icon(Icons.brightness_2,color: Colors.white,size: 32.0,),
  28. ),
  29. )
  30. ],
  31. )
  32. );

alignment 对齐

语法

alignment: Alignment(1.0, 1.0),

取值范围 -1~1

具体应用 就是相当于Android 相对布局中的AliginParent属性

  1. 对于控件的 android:layout_alignParent 属性,只有在该布局的父布局也是RelativeLayout是才有用,此属性的含义为将控件边缘与父控件的边缘对齐
  2. android:layout_alignParentLeft="true"  --将控件的左边缘和父控件的左边缘对齐
  3. android:layout_alignParentTop="true"  --将控件的上边缘和父控件的上边缘对齐
  4. android:layout_alignParentRight="true"  --将控件的右边缘和父控件的右边缘对齐
  5. android:layout_alignParentBottom="true" --将控件的底边缘和父控件的底边缘对齐
  6. android:layout_centerInParent="true"  --将控件置于父控件的中心位置
  7. android:layout_centerHorizontal="true"  --将控件置于水平方向的中心位置
  8. android:layout_centerVertical="true"  --将控件置于垂直方向的中心位置

默认的位置就是0,0

实际应用如下 可以理解为child再SizeBox中的位置 左上是-1,-1 右下是1,1

这个样子的设计我还觉得挺新颖的

  1. SizedBox(
  2. width: 200.0,
  3. height: 300.0,
  4. child: Container(
  5. alignment: Alignment(0.0, 0.0),
  6. decoration: BoxDecoration(
  7. color: Color.fromRGBO(3, 54, 255, 1.0),
  8. borderRadius: BorderRadius.circular(8.0),
  9. ),
  10. child: Icon(Icons.ac_unit,color: Colors.white,size: 32.0,),
  11. ),
  12. ),

当然 Flutter作为后起之秀不是没有原因的,真的是细节做的很好 他为了少让你自己算0,-0.5  还是 -0.5 0,量身定制了api

  alignment: Alignment.topCenter,

我就不一一列举了 自己尝试把

Stack 相当于Android的相对布局

把一堆小空间叠加起来 直接上代码吧

  1. import 'package:flutter/material.dart';
  2. class LayoutDemo extends StatelessWidget{
  3. @override
  4. Widget build(BuildContext context) {
  5. return Container(
  6. child: Column(
  7. mainAxisAlignment: MainAxisAlignment.center,
  8. children: <Widget>[
  9. Stack(children: <Widget>[
  10. SizedBox(
  11. width: 200.0,
  12. height: 300.0,
  13. child: Container(
  14. alignment: Alignment.topCenter,
  15. decoration: BoxDecoration(
  16. color: Color.fromRGBO(3, 54, 255, 1.0),
  17. borderRadius: BorderRadius.circular(8.0),
  18. ),
  19. child: Icon(Icons.ac_unit,color: Colors.white,size: 32.0,),
  20. ),
  21. ),
  22. SizedBox(
  23. height:20.0
  24. ),
  25. SizedBox(
  26. width: 100.0,
  27. height: 100.0,
  28. child: Container(
  29. decoration: BoxDecoration(
  30. color: Color.fromRGBO(3, 54, 255, 1.0),
  31. borderRadius: BorderRadius.circular(8.0),
  32. ),
  33. child: Icon(Icons.brightness_2,color: Colors.white,size: 32.0,),
  34. ),
  35. )
  36. ],)
  37. ],
  38. )
  39. );
  40. }
  41. }
  42. class IconBadge extends StatelessWidget{
  43. final IconData icon;
  44. final double size;
  45. IconBadge(this.icon,{this.size=32.0});
  46. @override
  47. Widget build(BuildContext context) {
  48. // TODO: implement build
  49. return Container(
  50. child: Icon(icon,size:size,color: Colors.white),
  51. width: size+60,
  52. height: size+60,
  53. color: Color.fromRGBO(3, 54, 255, 1.0),
  54. );
  55. }
  56. }

Positioned 控制控件的位置

  1. Positioned(
  2. right: 20.0,
  3. top: 120.0,
  4. child: Icon(Icons.ac_unit,color: Colors.white,size: 16.0,),)
  5. ],)

so 画一个星空图 有月亮还有星辰 

  1. import 'package:flutter/material.dart';
  2. class LayoutDemo extends StatelessWidget {
  3. @override
  4. Widget build(BuildContext context) {
  5. return Container(
  6. child: Column(
  7. mainAxisAlignment: MainAxisAlignment.center,
  8. children: <Widget>[
  9. Stack(
  10. alignment: Alignment.topLeft,
  11. children: <Widget>[
  12. SizedBox(
  13. width: 200.0,
  14. height: 300.0,
  15. child: Container(
  16. alignment: Alignment.topCenter,
  17. decoration: BoxDecoration(
  18. color: Color.fromRGBO(3, 54, 255, 1.0),
  19. borderRadius: BorderRadius.circular(8.0),
  20. ),
  21. ),
  22. ),
  23. SizedBox(height: 20.0),
  24. SizedBox(
  25. width: 100.0,
  26. height: 100.0,
  27. child: Container(
  28. decoration: BoxDecoration(
  29. color: Color.fromRGBO(3, 54, 255, 1.0),
  30. shape: BoxShape.circle,
  31. gradient: RadialGradient(colors: [
  32. Color.fromRGBO(7, 102, 255, 1.0),
  33. Color.fromRGBO(3, 54, 255, 1.0),
  34. ])),
  35. child: Icon(
  36. Icons.brightness_2,
  37. color: Colors.white,
  38. size: 32.0,
  39. ),
  40. ),
  41. ),
  42. Positioned(
  43. right: 20.0,
  44. top: 120.0,
  45. child: Icon(Icons.ac_unit, color: Colors.white, size: 20.0)
  46. ),
  47. Positioned(
  48. right: 70.0,
  49. top: 180.0,
  50. child: Icon(Icons.ac_unit, color: Colors.white, size: 16.0)
  51. ),
  52. Positioned(
  53. right: 30.0,
  54. top: 230.0,
  55. child: Icon(Icons.ac_unit, color: Colors.white, size: 18.0)
  56. ),
  57. Positioned(
  58. right: 90.0,
  59. top: 20.0,
  60. child: Icon(Icons.ac_unit, color: Colors.white, size: 20.0)
  61. ),
  62. Positioned(
  63. right: 4.0,
  64. top: -4.0,
  65. child: Icon(Icons.ac_unit, color: Colors.white, size: 16.0)
  66. ),
  67. ],
  68. )
  69. ],
  70. ));
  71. }
  72. }
  73. class IconBadge extends StatelessWidget {
  74. final IconData icon;
  75. final double size;
  76. IconBadge(this.icon, {this.size = 32.0});
  77. @override
  78. Widget build(BuildContext context) {
  79. // TODO: implement build
  80. return Container(
  81. child: Icon(icon, size: size, color: Colors.white),
  82. width: size + 60,
  83. height: size + 60,
  84. color: Color.fromRGBO(3, 54, 255, 1.0),
  85. );
  86. }
  87. }

AspectRatio 来制造宽高比的容器

  1. class LayoutDemo extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return Container(
  5. child: Column(
  6. mainAxisAlignment: MainAxisAlignment.center,
  7. children: <Widget>[
  8. AspectRatio(
  9. aspectRatio:1.0/1.0 ,
  10. child: Container(
  11. color: Color.fromRGBO(3, 54, 255, 1.0),
  12. ),
  13. ),
  14. ],
  15. )
  16. );
  17. }
  18. }

其中 aspectRatio:1.0/1.0 ,

就是说1比上1  也可以改成16/9  3/2等值进行测试

 

ConstrainedBox 带限制的盒子

可以设置最小高度和最大高度等参数

  1. class LayoutDemo extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return Container(
  5. child: Column(
  6. mainAxisAlignment: MainAxisAlignment.center,
  7. children: <Widget>[ConstrainedBox(
  8. constraints: BoxConstraints(
  9. minHeight: 200.0,
  10. maxWidth: 200.0
  11. ),
  12. child: Container(
  13. color: Color.fromRGBO(3, 54, 255, 1.0),
  14. ),
  15. )],
  16. ));
  17. }
  18. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/289744
推荐阅读
相关标签
  

闽ICP备14008679号