Avatar 头像
用头像、首字母或图标表达用户、团队和对象身份。
何时使用
在线体验
基本用法
import "github.com/tangoui/tango/ui"
func page(c *ui.Context) {
ui.Avatar(c, "张三").Size(40).A11yLabel("张三")
}头像会从 label 生成缩写。可用 Icon 显示图标,或用 Image 显示图片。两者都设置时优先显示图片。
常见用法
方形与自定义颜色
func avatarVariants(c *ui.Context) {
ui.Avatar(c, "AB").Square().Size(32)
ui.Avatar(c, "A").Size(40)
}图片头像
func imageAvatar(c *ui.Context, img image.Image) {
ui.Avatar(c, "").Image(img).Size(40).A11yLabel("项目头像")
}使用时导入标准库 image;图片加载和缓存由应用负责。
头像组
func members(c *ui.Context) {
ui.AvatarGroup(c).Max(3).Size(32).Body(func() {
ui.Avatar(c, "李"); ui.Avatar(c, "王"); ui.Avatar(c, "赵"); ui.Avatar(c, "孙")
})
}主题与样式
三套主题分别通过头像背景、文字和组重叠令牌适配 Ant Design、Element Plus、shadcn。ConfigProvider.Defaults("Avatar", ...) 支持 size、shape 和 gap,ConfigProvider.Defaults("AvatarGroup", ...) 支持 maxCount。
注意事项
状态放在页面变量或后端数据中;头像本身是展示控件。图片头像应提供 A11yLabel,不要只依赖首字母。RTL 下组的重叠方向自动镜像。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Avatar
func Avatar(c *Context, label string) *AvatarWAvatar shows text initials (or an icon when label is "").
ConfigProvider.Defaults("Avatar", ...) keys: "size" (float32 px or ControlSize), "shape" ("circle" | "square"), "gap" (px, default 4).
返回 *AvatarW,可链式调用的方法见 AvatarW 方法。
ui.Gap
func Gap(c *Context, size float32)Gap inserts a fixed-size blank.
ui.AvatarGroup
func AvatarGroup(c *Context) *AvatarGroupWAvatarGroup starts a group; call Body with the avatars.
ConfigProvider.Defaults("AvatarGroup", ...) keys: "maxCount" (int).
返回 *AvatarGroupW,可链式调用的方法见 AvatarGroupW 方法。
AvatarW 方法
ui.AvatarW:AvatarW is a round user avatar.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name (the person's name; the default is the avatar text, or "avatar" for an icon or image avatar). | A11yLabel(s string) *AvatarW |
Bg | Bg(col theme.Color) *AvatarW | |
Gap | Gap sets the space kept on each side of the text; longer text is scaled down to fit (antd gap, default 4). | Gap(px float32) *AvatarW |
Icon | Icon shows an icon instead of the text (icon avatar). | Icon(ic *icon.Icon) *AvatarW |
Image | Image(img image.Image) *AvatarW | |
Large | Large uses the large container size (antd 40px, Element 56px). | Large() *AvatarW |
Shape | Shape sets "circle" (default) or "square". | Shape(s string) *AvatarW |
Size | Size(px float32) *AvatarW | |
Small | Small uses the small container size (antd size="small": 24px). | Small() *AvatarW |
Square | Square() *AvatarW |
AvatarGroupW 方法
ui.AvatarGroupW:AvatarGroupW stacks avatars with overlap.
| 方法 | 说明 | 签名 |
|---|---|---|
Body | Body builds the avatars. | Body(fn func()) *Box |
Max | Max folds avatars beyond n into a "+k" avatar; hovering it shows the folded avatars in a popover (antd max.count / max.popover). | Max(n int) *AvatarGroupW |
MaxPopover | MaxPopover shows (default) or hides the popover listing the folded avatars; placement and trigger as in Popover (default "top", "hover"). | MaxPopover(on bool, placement, trigger string) *AvatarGroupW |
MoreBg | MoreBg sets the background of the "+k" avatar (antd max.style). | MoreBg(col theme.Color) *AvatarGroupW |
Size | Size sets the size of the "+k" avatar (match your avatars; default 32). | Size(px float32) *AvatarGroupW |