Grid 栅格
把一行内容划分为 24 列,统一处理列宽、间距、偏移和响应式断点。
何时使用
- 表单、仪表盘和卡片需要稳定列宽与响应式换行时。
- 需要 24 列精确控制,或需要 offset、push、pull、order 时使用 Grid。
- 仅需一维排列和间距用 Flex 弹性布局;仅需均匀留白用 Space 间距。
- 页面整体框架用 Layout 布局,不要用 Grid 代替 Layout。
在线体验
基本用法
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/layout"
)
func page(c *ui.Context) {
ui.GridRow(c).Gutter(16, 8).Body(func() {
ui.Col(c, 8).Body(func() { ui.Column(c, func() { ui.Text(c, "左") }, layout.Pad(12)) })
ui.Col(c, 16).Body(func() { ui.Column(c, func() { ui.Text(c, "右") }, layout.Pad(12)) })
})
}一行总宽为 24,Col(c, 8) 占三分之一,Col(c, 16) 占三分之二。Gutter(horizontal, vertical) 设置列间距。
常见用法
等分列
每个 Col 使用相同 span 即可构成等分布局,所有列放进同一个 GridRow.Body。
import "github.com/tangoui/tango/ui"
func page(c *ui.Context) {
ui.GridRow(c).Gutter(12, 12).Body(func() {
for i := 0; i < 4; i++ {
i := i
ui.Col(c, 6).Body(func() { ui.Text(c, "卡片 " + string(rune('A'+i))) })
}
})
}响应式列宽
用 XS、SM、MD、LG、XL、XXL 覆盖对应断点的 span;可选的第二个参数是 offset。
import "github.com/tangoui/tango/ui"
func page(c *ui.Context) {
ui.GridRow(c).Gutter(16, 16).Body(func() {
ui.Col(c, 24).MD(12).LG(6).Body(func() { ui.Text(c, "响应式卡片") })
ui.Col(c, 24).MD(12).LG(6).Body(func() { ui.Text(c, "响应式卡片") })
})
}偏移与对齐
Offset 留出空列;Justify 分配整行剩余空间,Align 控制列在交叉轴的对齐方式。
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/layout"
)
func page(c *ui.Context) {
ui.GridRow(c).Justify(layout.JustifySpaceBetween).Align(layout.AlignCenter).Body(func() {
ui.Col(c, 6).Body(func() { ui.Text(c, "起点") })
ui.Col(c, 6).Offset(6).Body(func() { ui.Text(c, "偏移后") })
})
}Push、Pull 与顺序
Push、Pull 移动列的视觉位置,Order 调整 flex 顺序;优先用结构顺序表达语义,只有布局需要时再改变视觉顺序。
import "github.com/tangoui/tango/ui"
func page(c *ui.Context) {
ui.GridRow(c).Body(func() {
ui.Col(c, 8).Order(2).Body(func() { ui.Text(c, "第二个视觉位置") })
ui.Col(c, 16).Order(1).Body(func() { ui.Text(c, "第一个视觉位置") })
})
}主题与样式
Grid 本身只负责列计算和 gutter;列内容的背景、圆角和内边距由 Col.Body 内的容器负责。Ant、Element Plus、shadcn 的主要差异在断点语义与外层容器样式,TanGo 断点统一使用 XS 至 XXL。
注意事项
span、offset、push、pull 等列数以 24 为基准,合计超过 24 时会换行或产生溢出布局。GutterAt(bp, px)按断点设置水平间距,VGutterAt(bp, px)按断点设置换行后的纵向间距。- Grid 是 flex 布局,列内长文本和控件仍需设置合理宽度或换行策略。
Order改变布局顺序。需要明确阅读顺序时,按语义顺序构建各列。- RTL 下 offset、push、pull 与顺序会按布局方向处理。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.GridRow
func GridRow(c *Context) *GridRowWGridRow starts a grid row; chain options, then Body with Col calls.
返回 *GridRowW,可链式调用的方法见 GridRowW 方法。
ui.Col
func Col(c *Context, span int) *GridColWCol adds a column spanning span/24 of the row; call it inside GridRow.Body.
返回 *GridColW,可链式调用的方法见 GridColW 方法。
GridRowW 方法
ui.GridRowW:GridRowW is a 24-column grid row (Ant Row / Element el-row).
| 方法 | 说明 | 签名 |
|---|---|---|
Align | Align sets vertical alignment of columns (AlignStart, AlignCenter, AlignEnd). | Align(a layout.Align) *GridRowW |
Body | Body builds the row. | Body(fn func()) *Box |
Gutter | Gutter sets the horizontal and vertical spacing between columns. | Gutter(h, v float32) *GridRowW |
GutterAt | GutterAt sets a responsive horizontal gutter per breakpoint. | GutterAt(bp Breakpoint, px float32) *GridRowW |
Justify | Justify distributes free columns: JustifyStart, JustifyEnd, JustifyCenter, JustifySpaceBetween, JustifySpaceAround, JustifySpaceEvenly. | Justify(j layout.Justify) *GridRowW |
NoWrap | NoWrap keeps all columns on one line. | NoWrap() *GridRowW |
VGutterAt | VGutterAt sets a responsive vertical gutter (the gap between wrapped lines) per breakpoint (Ant gutter={[h, {xs: 8, md: 16}]}). | VGutterAt(bp Breakpoint, px float32) *GridRowW |
GridColW 方法
ui.GridColW:GridColW is a column of a GridRow.
| 方法 | 说明 | 签名 |
|---|---|---|
Body | Body sets the column content (built when the row lays out its lines). | Body(fn func()) *GridColW |
Flex | Flex makes the column take free space (flex: n) instead of a span. | Flex(n float32) *GridColW |
LG | LG(span int, offset ...int) *GridColW | |
MD | MD(span int, offset ...int) *GridColW | |
Offset | Offset shifts the column towards the end (right; left in RTL) by n columns. | Offset(n int) *GridColW |
Order | Order sets the flex order of the column within the row (lower first; equal orders keep the call order). | Order(n int) *GridColW |
Pull | Pull moves the column towards the start by n columns (Ant pull). | Pull(n int) *GridColW |
Push | Push moves the column towards the end by n columns without moving its neighbours (Ant push; for reordering columns visually). | Push(n int) *GridColW |
SM | SM(span int, offset ...int) *GridColW | |
Style | Style applies layout options to the column content box. | Style(opts ...layout.Option) *GridColW |
XL | XL(span int, offset ...int) *GridColW | |
XS | XS..XXL set the span (and optional offset) from that breakpoint up. | XS(span int, offset ...int) *GridColW |
XXL | XXL(span int, offset ...int) *GridColW |