# 流式布局

本文作者:阳九五 (opens new window)

本站地址:https://blog.56321654.xyz (opens new window)

# Wrap

Wrap是一个流式布局的小部件,它允许子组件沿着主轴(可以是水平或垂直)排列,并在空间不足时自动换行。

属性 说明
direction 方向,用来确定主轴
alignment 主轴的对齐方式
spacing 主轴方向的每个子组件之间的留白
crossAxisAligment 交叉轴的对齐方式
runAlignment 每行(或列)的对齐方式
runSpacing 每行(或列)的间距
textDirection 文本的方向
verticalDirection 子组件的摆放顺序

Wrap组件沿着主轴方向依次排列子组件,当空间不足时便会另起一行或一列。它的每行(或列)被称作一个Run

# 示例

class CustomWidget extends StatelessWidget {
  const CustomWidget({super.key});

  
  Widget build(BuildContext context) {
    return Container(
      width: 200,
      height: 200,
      color: Colors.black26,
      child: Wrap(
        // 主轴方向
        direction: Axis.horizontal,
        // 主轴对齐方式
        alignment: WrapAlignment.center,
        // 主轴每项间距
        spacing: 20,
        // 交叉轴对齐方式
        crossAxisAlignment: WrapCrossAlignment.end,
        // 每行(或列)的对齐方式
        runAlignment: WrapAlignment.end,
        // 每行(或列)的间距
        runSpacing: 40,
        // 文本的方向
        textDirection: TextDirection.rtl,
        // 子组件的摆放顺序, down(从上到下),up(从下到上)
        verticalDirection: VerticalDirection.down,
        children: [
          Container(
            color: Colors.amber,
            width: 50,
            height: 60,
            child: const Center(
              child: Text("1"),
            ),
          ),
          Container(
            color: Colors.amber,
            width: 40,
            height: 40,
            child: const Center(
              child: Text("2"),
            ),
          ),
          Container(
            color: Colors.amber,
            width: 50,
            height: 50,
            child: const Center(
              child: Text("3"),
            ),
          ),
          Container(
            color: Colors.amber,
            width: 30,
            height: 60,
            child: const Center(
              child: Text("4"),
            ),
          ),
          Container(
            color: Colors.amber,
            width: 40,
            height: 30,
            child: const Center(
              child: Text("5"),
            ),
          ),
        ],
      )
    );
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

Image

最近更新: 8/6/2025, 2:39:35 PM
流式布局