Progress 进度条
展示任务完成比例,支持线型、环形、仪表盘、分段和不确定进度。
何时使用
- 上传、导入、处理等有明确完成比例的长任务。
- 需要在卡片中展示评分、容量或完成度时使用环形进度。
- 无法估计进度时使用
Indeterminate();只表示正在处理时用 Spin。
在线体验
基本用法
import "github.com/tangoui/tango/ui"
func progressPage(c *ui.Context) {
ui.Progress(c, 68)
}百分比范围是 0 到 100,默认显示线型进度和信息文字。
常见用法
状态与文字
func progressStatus(c *ui.Context) {
ui.Progress(c, 100).Status("success")
ui.Progress(c, 72).Status("active")
ui.Progress(c, 40).Status("exception").Format(func(p float64) string { return "失败" })
}Status 支持 active、success、exception、warning;Format 自定义信息文字。
环形与仪表盘
func progressCircle(c *ui.Context) {
ui.Progress(c, 75).Circle(120)
ui.Progress(c, 60).Dashboard(120).GapDegree(90)
}Circle 为完整圆环,Dashboard 为带缺口的仪表盘,参数是直径。
分段、渐变和条纹
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/theme"
)
func progressStyles(c *ui.Context) {
ui.Progress(c, 80).Steps(5).Color(theme.Hex("#1677ff"))
ui.Progress(c, 60).Gradient(theme.Hex("#108ee9"), theme.Hex("#87d068"))
ui.Progress(c, 50).Striped(true).TextInside()
}Steps 分割为固定段数,Gradient 设置渐变,Striped(true) 显示流动条纹,TextInside 将文字放入条内。
不确定进度与无障碍
func progressA11y(c *ui.Context) {
ui.Progress(c, 0).Indeterminate().A11yLabel("正在同步数据")
ui.Progress(c, 45).HideInfo().A11yLabel("文件上传进度")
}无法估计完成比例时不要伪造百分比。A11yLabel 为进度指示器设置可访问名称,HideInfo 隐藏视觉文字但不替代无障碍名称。
主题与样式
Progress 的状态色、轨道色、线宽和动画由主题控制。使用 Color、TrailColor、StrokeWidth 做局部调整;统一品牌色应配置主题令牌。shadcn 线型默认可不显示文字,调用 ShowInfo() 可显式显示;不同主题对圆角、条纹和状态色有对应适配。
注意事项
进度值应来自任务状态并限制在 0–100;大任务更新频率过高会增加重绘,可按合适间隔更新。成功、失败和不确定状态应同时用文字或图标表达,不要只依赖颜色。RTL 下线型方向由组件处理,不要手工反转百分比。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Progress
func Progress(c *Context, percent float64) *ProgressWProgress adds a progress indicator (percent 0..100): a line bar by default, Circle/Dashboard for rings.
返回 *ProgressW,可链式调用的方法见 ProgressW 方法。
ProgressW 方法
ui.ProgressW:ProgressW is the full progress indicator: line / circle / dashboard types, steps, gradients, success segment, inside text, stripes and indeterminate motion.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name (default: the Locale's "progress"). | A11yLabel(s string) *ProgressW |
Circle | Circle is Type("circle") with a diameter. | Circle(size float32) *ProgressW |
Color | Color overrides the bar color. | Color(col theme.Color) *ProgressW |
ColorFunc | ColorFunc picks the bar color from the percentage (Element color function; a color array is a ColorFunc with thresholds). | ColorFunc(fn func(percent float64) theme.Color) *ProgressW |
Dashboard | Dashboard is Type("dashboard") with a diameter. | Dashboard(size float32) *ProgressW |
Format | Format customises the info text. | Format(f func(percent float64) string) *ProgressW |
GapDegree | GapDegree sets the ring opening in degrees (dashboard default 75°; a circle with GapDegree opens too, like antd). | GapDegree(d float64) *ProgressW |
GapPlacement | GapPlacement sets where the ring opening sits: "bottom" (default), "top", "start" or "end" (logical: mirrored in RTL). | GapPlacement(s string) *ProgressW |
Gradient | Gradient paints the bar from a to b. | Gradient(a, b theme.Color) *ProgressW |
HideInfo | HideInfo hides the percentage text / status icon. | HideInfo() *ProgressW |
Indeterminate | Indeterminate shows an endlessly moving segment (Element). | Indeterminate() *ProgressW |
Layout | Layout applies layout options. | Layout(o ...layout.Option) *ProgressW |
PercentPosition | PercentPosition places the info text of a line bar (antd 6 percentPosition): align "start", "center" or "end" (default); typ "outer" (default; center puts it under the bar) or "inner" (inside the filled part, like Element text-inside). | PercentPosition(align, typ string) *ProgressW |
RailColor | RailColor sets the color of the unfilled rail (antd railColor). | RailColor(col theme.Color) *ProgressW |
ShowInfo | ShowInfo shows the percentage text / status icon. | ShowInfo() *ProgressW |
Small | Small uses the compact size. | Small() *ProgressW |
Square | Square uses butt line caps instead of round ones. | Square() *ProgressW |
Status | Status: "active" (Ant shimmer), "success", "exception", "warning". | Status(s string) *ProgressW |
Steps | Steps splits the bar (or ring) into n segments. | Steps(n int) *ProgressW |
StepsGap | StepsGap sets the gap between step segments in px (antd steps.gap; default 2). | StepsGap(px float32) *ProgressW |
Striped | Striped draws diagonal stripes; flow animates them. | Striped(flow bool) *ProgressW |
StrokeWidth | StrokeWidth sets the bar thickness / ring width in px. | StrokeWidth(w float32) *ProgressW |
Success | Success shows the first pct percent in the success color (Ant success). | Success(pct float64) *ProgressW |
TextInside | TextInside draws the percentage inside the bar (Element text-inside). | TextInside() *ProgressW |
TrailColor | TrailColor sets the rail color. | TrailColor(col theme.Color) *ProgressW |
Type | Type selects "line", "circle" or "dashboard". | Type(t string) *ProgressW |
Width | Width sets the line length (or circle diameter). | Width(px float32) *ProgressW |