Skip to content

Progress 进度条 ​

展示任务完成比例,支持线型、环形、仪表盘、分段和不确定进度。

何时使用 ​

  • 上传、导入、处理等有明确完成比例的长任务。
  • 需要在卡片中展示评分、容量或完成度时使用环形进度。
  • 无法估计进度时使用 Indeterminate();只表示正在处理时用 Spin。

在线体验 ​

Progress 进度条 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

go
import "github.com/tangoui/tango/ui"

func progressPage(c *ui.Context) {
    ui.Progress(c, 68)
}

百分比范围是 0 到 100,默认显示线型进度和信息文字。

常见用法 ​

状态与文字 ​

go
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 自定义信息文字。

环形与仪表盘 ​

go
func progressCircle(c *ui.Context) {
    ui.Progress(c, 75).Circle(120)
    ui.Progress(c, 60).Dashboard(120).GapDegree(90)
}

Circle 为完整圆环,Dashboard 为带缺口的仪表盘,参数是直径。

分段、渐变和条纹 ​

go
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 将文字放入条内。

不确定进度与无障碍 ​

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

Progress 进度条

gallery 演示 Progress 的 Render 函数,源码 page_data_misc.go#L59。

加载中…

Progress 文字位置与缺口方向

gallery 演示 ProgressAlign 的 Render 函数,源码 page_b2g.go#L135。

加载中…

API ​

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

入口函数 ​

ui.Progress ​

go
func Progress(c *Context, percent float64) *ProgressW

Progress 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.

方法说明签名
A11yLabelA11yLabel sets the accessible name (default: the Locale's "progress").A11yLabel(s string) *ProgressW
CircleCircle is Type("circle") with a diameter.Circle(size float32) *ProgressW
ColorColor overrides the bar color.Color(col theme.Color) *ProgressW
ColorFuncColorFunc 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
DashboardDashboard is Type("dashboard") with a diameter.Dashboard(size float32) *ProgressW
FormatFormat customises the info text.Format(f func(percent float64) string) *ProgressW
GapDegreeGapDegree sets the ring opening in degrees (dashboard default 75°; a circle with GapDegree opens too, like antd).GapDegree(d float64) *ProgressW
GapPlacementGapPlacement sets where the ring opening sits: "bottom" (default), "top", "start" or "end" (logical: mirrored in RTL).GapPlacement(s string) *ProgressW
GradientGradient paints the bar from a to b.Gradient(a, b theme.Color) *ProgressW
HideInfoHideInfo hides the percentage text / status icon.HideInfo() *ProgressW
IndeterminateIndeterminate shows an endlessly moving segment (Element).Indeterminate() *ProgressW
LayoutLayout applies layout options.Layout(o ...layout.Option) *ProgressW
PercentPositionPercentPosition 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
RailColorRailColor sets the color of the unfilled rail (antd railColor).RailColor(col theme.Color) *ProgressW
ShowInfoShowInfo shows the percentage text / status icon.ShowInfo() *ProgressW
SmallSmall uses the compact size.Small() *ProgressW
SquareSquare uses butt line caps instead of round ones.Square() *ProgressW
StatusStatus: "active" (Ant shimmer), "success", "exception", "warning".Status(s string) *ProgressW
StepsSteps splits the bar (or ring) into n segments.Steps(n int) *ProgressW
StepsGapStepsGap sets the gap between step segments in px (antd steps.gap; default 2).StepsGap(px float32) *ProgressW
StripedStriped draws diagonal stripes; flow animates them.Striped(flow bool) *ProgressW
StrokeWidthStrokeWidth sets the bar thickness / ring width in px.StrokeWidth(w float32) *ProgressW
SuccessSuccess shows the first pct percent in the success color (Ant success).Success(pct float64) *ProgressW
TextInsideTextInside draws the percentage inside the bar (Element text-inside).TextInside() *ProgressW
TrailColorTrailColor sets the rail color.TrailColor(col theme.Color) *ProgressW
TypeType selects "line", "circle" or "dashboard".Type(t string) *ProgressW
WidthWidth sets the line length (or circle diameter).Width(px float32) *ProgressW

相关链接 ​

以 MIT 许可发布