Flex 布局

Flex 布局是基于 CSS Flex 实现的布局效果,它比 Grid 和 HBox 对子节点位置的可控性更强,比用 CSS 类的方式更易用,并且默认使用水平垂直居中的对齐。

基本用法

```schema: scope=”body” { “type”: “flex”, “items”: [{ “style”: { “backgroundColor”: “#1A5CFF”, “width”: 100, “height”: 50, “margin”: 5 }, “type”: “tpl”, “tpl”: “” }, { “style”: { “backgroundColor”: “#46C93A”, “width”: 100, “height”: 50, “margin”: 5 }, “type”: “tpl”, “tpl”: “” }, { “style”: { “backgroundColor”: “#FF4757”, “width”: 100, “height”: 50, “margin”: 5 }, “type”: “tpl”, “tpl”: “” }] }

  1. 其中 `items` 里的每一个都可以是其他 amis 类型。
  2. ## 子节点水平分布
  3. > 严格来说并不一定是水平,因为还能通过 direction 修改方向,不过这里为了简化就这么称呼了
  4. 可以通过 justify 控制水平分布方式,默认是 `center` 居中,其他几种的示例如下:
  5. ```schema
  6. {
  7. "type": "page",
  8. "body": [
  9. "center",
  10. {
  11. "type": "flex",
  12. "justify": "center",
  13. "items": [{
  14. "style": {
  15. "backgroundColor": "#1A5CFF",
  16. "width": 100,
  17. "height": 30,
  18. "margin": 5
  19. },
  20. "type": "tpl",
  21. "tpl": ""
  22. }, {
  23. "style": {
  24. "backgroundColor": "#46C93A",
  25. "width": 100,
  26. "height": 30,
  27. "margin": 5
  28. },
  29. "type": "tpl",
  30. "tpl": ""
  31. }, {
  32. "style": {
  33. "backgroundColor": "#FF4757",
  34. "width": 100,
  35. "height": 30,
  36. "margin": 5
  37. },
  38. "type": "tpl",
  39. "tpl": ""
  40. }]
  41. },
  42. "flex-start",
  43. {
  44. "type": "flex",
  45. "justify": "flex-start",
  46. "items": [{
  47. "style": {
  48. "backgroundColor": "#1A5CFF",
  49. "width": 100,
  50. "height": 30,
  51. "margin": 5
  52. },
  53. "type": "tpl",
  54. "tpl": ""
  55. }, {
  56. "style": {
  57. "backgroundColor": "#46C93A",
  58. "width": 100,
  59. "height": 30,
  60. "margin": 5
  61. },
  62. "type": "tpl",
  63. "tpl": ""
  64. }, {
  65. "style": {
  66. "backgroundColor": "#FF4757",
  67. "width": 100,
  68. "height": 30,
  69. "margin": 5
  70. },
  71. "type": "tpl",
  72. "tpl": ""
  73. }]
  74. },
  75. "flex-end",
  76. {
  77. "type": "flex",
  78. "justify": "flex-end",
  79. "items": [{
  80. "style": {
  81. "backgroundColor": "#1A5CFF",
  82. "width": 100,
  83. "height": 30,
  84. "margin": 5
  85. },
  86. "type": "tpl",
  87. "tpl": ""
  88. }, {
  89. "style": {
  90. "backgroundColor": "#46C93A",
  91. "width": 100,
  92. "height": 30,
  93. "margin": 5
  94. },
  95. "type": "tpl",
  96. "tpl": ""
  97. }, {
  98. "style": {
  99. "backgroundColor": "#FF4757",
  100. "width": 100,
  101. "height": 30,
  102. "margin": 5
  103. },
  104. "type": "tpl",
  105. "tpl": ""
  106. }]
  107. },
  108. "space-around",
  109. {
  110. "type": "flex",
  111. "justify": "space-around",
  112. "items": [{
  113. "style": {
  114. "backgroundColor": "#1A5CFF",
  115. "width": 100,
  116. "height": 30,
  117. "margin": 5
  118. },
  119. "type": "tpl",
  120. "tpl": ""
  121. }, {
  122. "width": 100,
  123. "height": 30,
  124. "style": {
  125. "backgroundColor": "#46C93A",
  126. "margin": 5
  127. },
  128. "type": "tpl",
  129. "tpl": ""
  130. }, {
  131. "style": {
  132. "backgroundColor": "#FF4757",
  133. "width": 100,
  134. "height": 30,
  135. "margin": 5
  136. },
  137. "type": "tpl",
  138. "tpl": ""
  139. }]
  140. },
  141. "space-between",
  142. {
  143. "type": "flex",
  144. "justify": "space-between",
  145. "items": [{
  146. "style": {
  147. "backgroundColor": "#1A5CFF",
  148. "width": 100,
  149. "height": 30,
  150. "margin": 5
  151. },
  152. "type": "tpl",
  153. "tpl": ""
  154. }, {
  155. "style": {
  156. "backgroundColor": "#46C93A",
  157. "width": 100,
  158. "height": 30,
  159. "margin": 5
  160. },
  161. "type": "tpl",
  162. "tpl": ""
  163. }, {
  164. "style": {
  165. "backgroundColor": "#FF4757",
  166. "width": 100,
  167. "height": 30,
  168. "margin": 5
  169. },
  170. "type": "tpl",
  171. "tpl": ""
  172. }]
  173. },
  174. "space-evenly",
  175. {
  176. "type": "flex",
  177. "justify": "space-evenly",
  178. "items": [{
  179. "style": {
  180. "backgroundColor": "#1A5CFF",
  181. "width": 100,
  182. "height": 30,
  183. "margin": 5
  184. },
  185. "type": "tpl",
  186. "tpl": ""
  187. }, {
  188. "style": {
  189. "backgroundColor": "#46C93A",
  190. "width": 100,
  191. "height": 30,
  192. "margin": 5
  193. },
  194. "type": "tpl",
  195. "tpl": ""
  196. }, {
  197. "style": {
  198. "backgroundColor": "#FF4757",
  199. "width": 100,
  200. "height": 30,
  201. "margin": 5
  202. },
  203. "type": "tpl",
  204. "tpl": ""
  205. }]
  206. }
  207. ]
  208. }

垂直方向位置

可以通过设置 alignItems 改变在子节点在垂直方向的位置,默认是 center 居中,其他几个常见设置请参考:

```schema: scope=”body” { “type”: “page”, “body”: [ “center”, { “type”: “flex”, “alignItems”: “center”, “style”: { “height”: 60, “backgroundColor”: “#fff” }, “items”: [{ “style”: { “backgroundColor”: “#1A5CFF”, “width”: 100, “height”: 30, “margin”: 5 }, “type”: “tpl”, “tpl”: “” }, { “style”: { “backgroundColor”: “#46C93A”, “width”: 100, “height”: 30, “margin”: 5 }, “type”: “tpl”, “tpl”: “” }, { “style”: { “backgroundColor”: “#FF4757”, “width”: 100, “height”: 30, “margin”: 5 }, “type”: “tpl”, “tpl”: “” }] }, { “type”: “divider” }, “flex-start”, { “type”: “flex”, “alignItems”: “flex-start”, “style”: { “height”: 60, “backgroundColor”: “#fff” }, “items”: [{ “style”: { “backgroundColor”: “#1A5CFF”, “width”: 100, “height”: 30, “margin”: 5 }, “type”: “tpl”, “tpl”: “” }, { “style”: { “backgroundColor”: “#46C93A”, “width”: 100, “height”: 30, “margin”: 5 }, “type”: “tpl”, “tpl”: “” }, { “style”: { “backgroundColor”: “#FF4757”, “width”: 100, “height”: 30, “margin”: 5 }, “type”: “tpl”, “tpl”: “” }] }, { “type”: “divider” }, “flex-end”, { “type”: “flex”, “alignItems”: “flex-end”, “style”: { “height”: 60, “backgroundColor”: “#fff” }, “items”: [{ “style”: { “backgroundColor”: “#1A5CFF”, “width”: 100, “height”: 30, “margin”: 5 }, “type”: “tpl”, “tpl”: “” }, { “style”: { “backgroundColor”: “#46C93A”, “width”: 100, “height”: 30, “margin”: 5 }, “type”: “tpl”, “tpl”: “” }, { “style”: { “backgroundColor”: “#FF4757”, “width”: 100, “height”: 30, “margin”: 5 }, “type”: “tpl”, “tpl”: “” }] } ] }

  1. ## 布局方向
  2. 默认是行的方式,可以通过 "direction": "column" 改成列的方式。
  3. ```schema: scope="body"
  4. {
  5. "type": "page",
  6. "body": [
  7. "direction: row",
  8. {
  9. "type": "flex",
  10. "items": [{
  11. "style": {
  12. "backgroundColor": "#1A5CFF",
  13. "width": 100,
  14. "height": 30,
  15. "margin": 5
  16. },
  17. "type": "tpl",
  18. "tpl": ""
  19. }, {
  20. "style": {
  21. "backgroundColor": "#46C93A",
  22. "width": 100,
  23. "height": 30,
  24. "margin": 5
  25. },
  26. "type": "tpl",
  27. "tpl": ""
  28. }, {
  29. "style": {
  30. "backgroundColor": "#FF4757",
  31. "width": 100,
  32. "height": 30,
  33. "margin": 5
  34. },
  35. "type": "tpl",
  36. "tpl": ""
  37. }]
  38. },
  39. "direction: column",
  40. {
  41. "type": "flex",
  42. "direction": "column",
  43. "items": [{
  44. "style": {
  45. "backgroundColor": "#1A5CFF",
  46. "width": 100,
  47. "height": 30,
  48. "margin": 5
  49. },
  50. "type": "tpl",
  51. "tpl": ""
  52. }, {
  53. "style": {
  54. "backgroundColor": "#46C93A",
  55. "width": 100,
  56. "height": 30,
  57. "margin": 5
  58. },
  59. "type": "tpl",
  60. "tpl": ""
  61. }, {
  62. "style": {
  63. "backgroundColor": "#FF4757",
  64. "width": 100,
  65. "height": 30,
  66. "margin": 5
  67. },
  68. "type": "tpl",
  69. "tpl": ""
  70. }]
  71. }
  72. ]
  73. }

移动端支持

有时候希望在移动端有不同展现,比如将 direction 改成 column

schema: scope="body" { "type": "flex", "mobile": { "direction": "column" }, "items": [{ "style": { "backgroundColor": "#1A5CFF", "width": 100, "height": 50, "margin": 5 }, "type": "tpl", "tpl": "" }, { "style": { "backgroundColor": "#46C93A", "width": 100, "height": 50, "margin": 5 }, "type": "tpl", "tpl": "" }, { "style": { "backgroundColor": "#FF4757", "width": 100, "height": 50, "margin": 5 }, "type": "tpl", "tpl": "" }] }

其他关于移动端定制的细节请参考这里

属性表

属性名 类型 默认值 说明
type string flex 指定为 Flex 渲染器
className string css 类名
justify string “start”, “flex-start”, “center”, “end”, “flex-end”, “space-around”, “space-between”, “space-evenly”
alignItems string “stretch”, “start”, “flex-start”, “flex-end”, “end”, “center”, “baseline”
style object 自定义样式
items[] SchemaNode