Skip to content

Badge 徽章 ​

在对象的右上角(RTL 下为左上角)显示数量、状态点或简短提醒。

何时使用 ​

  • 头像、图标或列表项上叠加未读数、数量和提醒。
  • 用状态点表达在线、成功等状态时使用 Status。
  • 需要完整提示内容时改用 Alert 或 Notification。

在线体验 ​

Badge 徽标 / Avatar 头像 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

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

func page(c *ui.Context) {
    ui.Badge(c).Count(5).Body(func() { ui.Avatar(c, "U") })
}

Body 把徽章叠加到内容的顶部末端。省略 Body 时显示独立徽标。

常见用法 ​

超出上限和零值 ​

go
func counts(c *ui.Context, unread int) {
    ui.Badge(c).Count(unread).Max(99).ShowZero().Body(func() { ui.Text(c, "消息") })
}

状态点 ​

go
func state(c *ui.Context) {
    ui.Badge(c).Status("success", "在线")
    ui.Badge(c).Dot().Color("warning").Body(func() { ui.Text(c, "待处理") })
}

偏移与徽标尺寸 ​

go
func compact(c *ui.Context) {
    ui.Badge(c).Count(2).Small().Offset(4, -4).Body(func() { ui.Text(c, "任务") })
}

主题与样式 ​

Badge 的令牌控制颜色、气泡高度和状态间距。全局默认键为 size、overflowCount、showZero、color、dot。计数默认上限为 99,超出显示为 99+。主题会处理 RTL 下的角落位置。

注意事项 ​

数量应来自页面状态并在即时模式每帧重建;Max 只影响显示,不改变实际数量。徽标文本应短小,动态状态建议同时提供可读文本而不是只显示颜色。

示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

Badge 徽标 / Avatar 头像

gallery 演示 Badge 的 Render 函数,源码 page_data_misc.go#L141。

加载中…

Badge 负数、隐藏与标题

gallery 演示 BadgeAlign 的 Render 函数,源码 page_b2g.go#L65。

加载中…

API ​

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

入口函数 ​

ui.Badge ​

go
func Badge(c *Context) *BadgeW

Badge adds a badge. Body(fn) wraps content and draws the bubble at its top-end corner (top-right; top-left in RTL); without Body the badge stands alone (an inline bubble, or a status dot with a label when Status is set).

ui.Badge(c).Count(5).Body(func() { ui.Avatar(c, "U") })
ui.Badge(c).Status("success", "成功")

ConfigProvider.Defaults("Badge", ...) keys: "size" ("small" / Small), "overflowCount" (int), "showZero" (bool), "color" (string), "dot" (bool).

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

ui.BadgeRibbon ​

go
func BadgeRibbon(c *Context, label, color, placement string, fn func()) *BadgeRibbonW

BadgeRibbon wraps fn's content (usually a card) with a corner ribbon. placement is "end" (default) or "start" (logical: mirrored in RTL); color as in Badge.Color. With placement "", the "placement" key of ConfigProvider.Defaults("BadgeRibbon", ...) applies.

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

BadgeW 方法 ​

ui.BadgeW:BadgeW is a badge: count / dot / text bubble, overflow, show-zero, preset or custom color, offset, small size, standalone use and the status form with the "processing" ripple.

方法说明签名
BodyBody wraps fn's content and draws the bubble at its top-end corner.Body(fn func()) *Box
ColorColor: status name ("success", "warning", "processing", ...), Ant preset ("pink", "green", ...) or hex.Color(col string) *BadgeW
CountCount sets the number.Count(n int) *BadgeW
DotDot shows a small dot.Dot() *BadgeW
HiddenHidden hides the bubble or dot (Element hidden).Hidden(on bool) *BadgeW
LayoutLayout applies layout options to the badge.Layout(o ...layout.Option) *BadgeW
MaxMax sets the overflow count (99 → "99+").Max(n int) *BadgeW
OffsetOffset moves the bubble by (x, y) from the top-end corner; x points away from the content (mirrored in RTL).Offset(x, y float32) *BadgeW
ShowZeroShowZero shows a zero count.ShowZero() *BadgeW
SmallSmall uses the 14px bubble.Small() *BadgeW
StatusStatus turns a standalone badge into a status dot with a label ("success", "processing" (ripples), "default", "error", "warning", or "custom" with Color).Status(status, label string) *BadgeW
TextText shows a word instead of a number (Element value="new").Text(s string) *BadgeW
TitleTitle sets the text announced for the bubble instead of the count (antd title).Title(s string) *BadgeW

BadgeRibbonW 类型 ​

ui.BadgeRibbonW:BadgeRibbonW is the ribbon wrapper BadgeRibbon returns; it embeds the wrapper's Box.

字段说明类型
Box*Box(嵌入)

BadgeRibbonW 嵌入通用容器 *ui.Box,可用的布局与样式方法见 Box 的 API;下表只列 BadgeRibbonW 自己的方法。

相关链接 ​

以 MIT 许可发布