Flutter flex布局其实是借鉴CSS flex布局,有一篇非常好的文章推荐给大家。

1. CSS

container

01-container.svg

items

02-items.svg

order

order.svg

flex-redirection

flex-direction.svg

flex-grow

flex-grow.svg

flex-wrap

flex-wrap.svg

justify-content

justify-content.svg

align-self

align-self.svg

align-items

align-items.svg

align-content

align-content.svg

2. Flutter

源码 flex.dartFlexFitMainAxisSizeMainAxisAlignmentCrossAxisAlignment

  1. /// How the child is inscribed into the available space.
  2. ///
  3. /// See also:
  4. ///
  5. /// * [RenderFlex], the flex render object.
  6. /// * [Column], [Row], and [Flex], the flex widgets.
  7. /// * [Expanded], the widget equivalent of [tight].
  8. /// * [Flexible], the widget equivalent of [loose].
  9. enum FlexFit {
  10. /// The child is forced to fill the available space.
  11. ///
  12. /// The [Expanded] widget assigns this kind of [FlexFit] to its child.
  13. tight,
  14. /// The child can be at most as large as the available space (but is
  15. /// allowed to be smaller).
  16. ///
  17. /// The [Flexible] widget assigns this kind of [FlexFit] to its child.
  18. loose,
  19. }
  20. /// How much space should be occupied in the main axis.
  21. ///
  22. /// During a flex layout, available space along the main axis is allocated to
  23. /// children. After allocating space, there might be some remaining free space.
  24. /// This value controls whether to maximize or minimize the amount of free
  25. /// space, subject to the incoming layout constraints.
  26. ///
  27. /// See also:
  28. ///
  29. /// * [Column], [Row], and [Flex], the flex widgets.
  30. /// * [Expanded] and [Flexible], the widgets that controls a flex widgets'
  31. /// children's flex.
  32. /// * [RenderFlex], the flex render object.
  33. /// * [MainAxisAlignment], which controls how the free space is distributed.
  34. enum MainAxisSize {
  35. /// Minimize the amount of free space along the main axis, subject to the
  36. /// incoming layout constraints.
  37. ///
  38. /// If the incoming layout constraints have a large enough
  39. /// [BoxConstraints.minWidth] or [BoxConstraints.minHeight], there might still
  40. /// be a non-zero amount of free space.
  41. ///
  42. /// If the incoming layout constraints are unbounded, and any children have a
  43. /// non-zero [FlexParentData.flex] and a [FlexFit.tight] fit (as applied by
  44. /// [Expanded]), the [RenderFlex] will assert, because there would be infinite
  45. /// remaining free space and boxes cannot be given infinite size.
  46. min,
  47. /// Maximize the amount of free space along the main axis, subject to the
  48. /// incoming layout constraints.
  49. ///
  50. /// If the incoming layout constraints have a small enough
  51. /// [BoxConstraints.maxWidth] or [BoxConstraints.maxHeight], there might still
  52. /// be no free space.
  53. ///
  54. /// If the incoming layout constraints are unbounded, the [RenderFlex] will
  55. /// assert, because there would be infinite remaining free space and boxes
  56. /// cannot be given infinite size.
  57. max,
  58. }
  59. /// How the children should be placed along the main axis in a flex layout.
  60. ///
  61. /// See also:
  62. ///
  63. /// * [Column], [Row], and [Flex], the flex widgets.
  64. /// * [RenderFlex], the flex render object.
  65. enum MainAxisAlignment {
  66. /// Place the children as close to the start of the main axis as possible.
  67. ///
  68. /// If this value is used in a horizontal direction, a [TextDirection] must be
  69. /// available to determine if the start is the left or the right.
  70. ///
  71. /// If this value is used in a vertical direction, a [VerticalDirection] must be
  72. /// available to determine if the start is the top or the bottom.
  73. start,
  74. /// Place the children as close to the end of the main axis as possible.
  75. ///
  76. /// If this value is used in a horizontal direction, a [TextDirection] must be
  77. /// available to determine if the end is the left or the right.
  78. ///
  79. /// If this value is used in a vertical direction, a [VerticalDirection] must be
  80. /// available to determine if the end is the top or the bottom.
  81. end,
  82. /// Place the children as close to the middle of the main axis as possible.
  83. center,
  84. /// Place the free space evenly between the children.
  85. spaceBetween,
  86. /// Place the free space evenly between the children as well as half of that
  87. /// space before and after the first and last child.
  88. spaceAround,
  89. /// Place the free space evenly between the children as well as before and
  90. /// after the first and last child.
  91. spaceEvenly,
  92. }
  93. /// How the children should be placed along the cross axis in a flex layout.
  94. ///
  95. /// See also:
  96. ///
  97. /// * [Column], [Row], and [Flex], the flex widgets.
  98. /// * [RenderFlex], the flex render object.
  99. enum CrossAxisAlignment {
  100. /// Place the children with their start edge aligned with the start side of
  101. /// the cross axis.
  102. ///
  103. /// For example, in a column (a flex with a vertical axis) whose
  104. /// [TextDirection] is [TextDirection.ltr], this aligns the left edge of the
  105. /// children along the left edge of the column.
  106. ///
  107. /// If this value is used in a horizontal direction, a [TextDirection] must be
  108. /// available to determine if the start is the left or the right.
  109. ///
  110. /// If this value is used in a vertical direction, a [VerticalDirection] must be
  111. /// available to determine if the start is the top or the bottom.
  112. start,
  113. /// Place the children as close to the end of the cross axis as possible.
  114. ///
  115. /// For example, in a column (a flex with a vertical axis) whose
  116. /// [TextDirection] is [TextDirection.ltr], this aligns the right edge of the
  117. /// children along the right edge of the column.
  118. ///
  119. /// If this value is used in a horizontal direction, a [TextDirection] must be
  120. /// available to determine if the end is the left or the right.
  121. ///
  122. /// If this value is used in a vertical direction, a [VerticalDirection] must be
  123. /// available to determine if the end is the top or the bottom.
  124. end,
  125. /// Place the children so that their centers align with the middle of the
  126. /// cross axis.
  127. ///
  128. /// This is the default cross-axis alignment.
  129. center,
  130. /// Require the children to fill the cross axis.
  131. ///
  132. /// This causes the constraints passed to the children to be tight in the
  133. /// cross axis.
  134. stretch,
  135. /// Place the children along the cross axis such that their baselines match.
  136. ///
  137. /// If the main axis is vertical, then this value is treated like [start]
  138. /// (since baselines are always horizontal).
  139. baseline,
  140. }