Badge 徽章
在对象的右上角(RTL 下为左上角)显示数量、状态点或简短提醒。
何时使用
- 头像、图标或列表项上叠加未读数、数量和提醒。
- 用状态点表达在线、成功等状态时使用
Status。 - 需要完整提示内容时改用 Alert 或 Notification。
在线体验
基本用法
import "github.com/tangoui/tango/ui"
func page(c *ui.Context) {
ui.Badge(c).Count(5).Body(func() { ui.Avatar(c, "U") })
}Body 把徽章叠加到内容的顶部末端。省略 Body 时显示独立徽标。
常见用法
超出上限和零值
func counts(c *ui.Context, unread int) {
ui.Badge(c).Count(unread).Max(99).ShowZero().Body(func() { ui.Text(c, "消息") })
}状态点
func state(c *ui.Context) {
ui.Badge(c).Status("success", "在线")
ui.Badge(c).Dot().Color("warning").Body(func() { ui.Text(c, "待处理") })
}偏移与徽标尺寸
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 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Badge
func Badge(c *Context) *BadgeWBadge 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
func BadgeRibbon(c *Context, label, color, placement string, fn func()) *BadgeRibbonWBadgeRibbon 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.
| 方法 | 说明 | 签名 |
|---|---|---|
Body | Body wraps fn's content and draws the bubble at its top-end corner. | Body(fn func()) *Box |
Color | Color: status name ("success", "warning", "processing", ...), Ant preset ("pink", "green", ...) or hex. | Color(col string) *BadgeW |
Count | Count sets the number. | Count(n int) *BadgeW |
Dot | Dot shows a small dot. | Dot() *BadgeW |
Hidden | Hidden hides the bubble or dot (Element hidden). | Hidden(on bool) *BadgeW |
Layout | Layout applies layout options to the badge. | Layout(o ...layout.Option) *BadgeW |
Max | Max sets the overflow count (99 → "99+"). | Max(n int) *BadgeW |
Offset | Offset 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 |
ShowZero | ShowZero shows a zero count. | ShowZero() *BadgeW |
Small | Small uses the 14px bubble. | Small() *BadgeW |
Status | Status 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 |
Text | Text shows a word instead of a number (Element value="new"). | Text(s string) *BadgeW |
Title | Title 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 自己的方法。