Spin 加载中
用旋转指示器表示内容正在加载或操作尚未完成。
何时使用
- 短时间内无法立即完成的操作,需要保留当前页面上下文时。
- 局部区域加载时用
Wrap覆盖已有内容;全局复杂加载可考虑 Loading。 - 已知内容结构时用 Skeleton;有明确百分比时用 Progress。
在线体验
基本用法
import "github.com/tangoui/tango/ui"
func spinPage(c *ui.Context) {
ui.Spin(c).Tip("加载中")
}默认显示中等尺寸指示器。Tip 在指示器下方显示辅助文案。
常见用法
尺寸和延迟
import "time"
func spinOptions(c *ui.Context) {
ui.Spin(c).Size(ui.Small)
ui.Spin(c).Size(ui.Large)
ui.Spin(c).Delay(500 * time.Millisecond)
}尺寸支持 ui.Small、ui.Middle、ui.Large。Delay 避免极短请求造成闪烁。
包裹局部内容
var sectionLoading = true
func spinNested(c *ui.Context) {
ui.Spin(c).Spinning(sectionLoading).Tip("正在刷新").Wrap(func() {
ui.Text(c, "这里是待刷新的内容")
})
}Wrap 在加载时让内容变暗并将指示器居中;把 sectionLoading 放在页面状态中,异步结束后再更新。
百分比进度
func spinPercent(c *ui.Context) {
ui.Spin(c).Percent(45).Tip("上传中")
}Percent 用进度环替代旋转指示器,范围为 0–100;任务无法估计比例时不要使用它。
自定义尺寸与停止
var loading = false
func spinControl(c *ui.Context) {
ui.Spin(c).IndicatorSize(28).Spinning(loading)
}IndicatorSize 设置指示器像素尺寸,Spinning(false) 隐藏加载状态。
主题与样式
Spin 的颜色、动画和默认尺寸由主题控制。与内容叠加时,组件自动处理遮罩和层级;不要通过固定颜色实现主题适配。Percent 模式复用进度环的主题语义。
注意事项
加载开始和结束必须由真实业务状态驱动。短请求使用 Delay,长请求应提供可读提示,必要时增加取消操作。包裹区域应保留稳定布局,避免加载期间内容消失造成跳动;键盘焦点不应因为局部 Spin 被意外移走。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Spin
func Spin(c *Context) *SpinWSpin adds a loading indicator; chain options. Wrap turns it into a container that covers its content while spinning:
ui.Spin(c).Size(ui.Large).Description("加载中")
ui.Spin(c).Spinning(loading).Wrap(func() { ... })
ConfigProvider.Defaults("Spin", ...) keys: "size", "delay", "description" ("tip" before antd 6).
返回 *SpinW,可链式调用的方法见 SpinW 方法。
SpinW 方法
ui.SpinW:SpinW is a loading indicator: sizes, description, delayed show, progress percent and a nested mode (Wrap) that dims the wrapped content.
| 方法 | 说明 | 签名 |
|---|---|---|
Delay | Delay shows the indicator only after spinning lasted d (avoids flashes); it hides at once when spinning stops (antd delay). | Delay(d time.Duration) *SpinW |
Description | Description shows a text under the indicator (antd 6 description). | Description(t string) *SpinW |
Indicator | Indicator replaces the indicator with a spinning icon (antd indicator, EP spinner). | Indicator(ic *icon.Icon) *SpinW |
IndicatorSize | IndicatorSize sets a custom indicator size in px. | IndicatorSize(px float32) *SpinW |
Percent | Percent replaces the indicator with a progress ring (0..100); at 0 the indicator still spins (antd). | Percent(p float64) *SpinW |
PercentAuto | PercentAuto shows a simulated progress that slows down as it nears 100 while spinning (antd percent="auto": every 200ms it advances 5% of the rest up to 30, 3% up to 70, 1% up to 96). | PercentAuto() *SpinW |
Size | Size sets small, middle (default) or large. | Size(sz ControlSize) *SpinW |
Spinning | Spinning toggles the indicator (default on). | Spinning(on bool) *SpinW |
Tip | Tip shows a text under the indicator. | Tip(t string) *SpinW |
Wrap | Wrap builds content and, while spinning, dims it and centers the indicator above it (Ant nested Spin). | Wrap(fn func()) *Box |