Skip to content

Grid 栅格 ​

把一行内容划分为 24 列,统一处理列宽、间距、偏移和响应式断点。

何时使用 ​

  • 表单、仪表盘和卡片需要稳定列宽与响应式换行时。
  • 需要 24 列精确控制,或需要 offset、push、pull、order 时使用 Grid。
  • 仅需一维排列和间距用 Flex 弹性布局;仅需均匀留白用 Space 间距。
  • 页面整体框架用 Layout 布局,不要用 Grid 代替 Layout。

在线体验 ​

Grid 栅格 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

go
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。

go
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。

go
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 控制列在交叉轴的对齐方式。

go
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 顺序;优先用结构顺序表达语义,只有布局需要时再改变视觉顺序。

go
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 入门。

Grid 栅格

gallery 演示 Grid 的具名 Render 函数,源码 page_nav_layout.go#L21。

加载中…

Grid Push / Pull / Order

gallery 演示 GridMore 的具名 Render 函数,源码 page_p2c_display.go#L150。

加载中…

API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

ui.GridRow ​

go
func GridRow(c *Context) *GridRowW

GridRow starts a grid row; chain options, then Body with Col calls.

返回 *GridRowW,可链式调用的方法见 GridRowW 方法。

ui.Col ​

go
func Col(c *Context, span int) *GridColW

Col 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).

方法说明签名
AlignAlign sets vertical alignment of columns (AlignStart, AlignCenter, AlignEnd).Align(a layout.Align) *GridRowW
BodyBody builds the row.Body(fn func()) *Box
GutterGutter sets the horizontal and vertical spacing between columns.Gutter(h, v float32) *GridRowW
GutterAtGutterAt sets a responsive horizontal gutter per breakpoint.GutterAt(bp Breakpoint, px float32) *GridRowW
JustifyJustify distributes free columns: JustifyStart, JustifyEnd, JustifyCenter, JustifySpaceBetween, JustifySpaceAround, JustifySpaceEvenly.Justify(j layout.Justify) *GridRowW
NoWrapNoWrap keeps all columns on one line.NoWrap() *GridRowW
VGutterAtVGutterAt 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.

方法说明签名
BodyBody sets the column content (built when the row lays out its lines).Body(fn func()) *GridColW
FlexFlex makes the column take free space (flex: n) instead of a span.Flex(n float32) *GridColW
LGLG(span int, offset ...int) *GridColW
MDMD(span int, offset ...int) *GridColW
OffsetOffset shifts the column towards the end (right; left in RTL) by n columns.Offset(n int) *GridColW
OrderOrder sets the flex order of the column within the row (lower first; equal orders keep the call order).Order(n int) *GridColW
PullPull moves the column towards the start by n columns (Ant pull).Pull(n int) *GridColW
PushPush moves the column towards the end by n columns without moving its neighbours (Ant push; for reordering columns visually).Push(n int) *GridColW
SMSM(span int, offset ...int) *GridColW
StyleStyle applies layout options to the column content box.Style(opts ...layout.Option) *GridColW
XLXL(span int, offset ...int) *GridColW
XSXS..XXL set the span (and optional offset) from that breakpoint up.XS(span int, offset ...int) *GridColW
XXLXXL(span int, offset ...int) *GridColW

相关链接 ​

以 MIT 许可发布