Button Group按钮组
查看源码components/button_group.go文本对齐#
三个相关动作组成一个水平控件。
options := []components.ButtonGroupOption{
{ID: tree.ID(page, "left"), Label: "Left", Variant: components.ButtonOutline},
{ID: tree.ID(page, "center"), Label: "Center", Variant: components.ButtonOutline},
{ID: tree.ID(page, "right"), Label: "Right", Variant: components.ButtonOutline},
}
group := components.BeginButtonGroup(canvas, theme, bounds, options)
if id, ok := group.Paint().Clicked(); ok {
align = id
}垂直操作#
窄区域中使用相同选项垂直排列。
options := []components.ButtonGroupOption{
{ID: tree.ID(page, "left"), Label: "Left", Variant: components.ButtonOutline},
{ID: tree.ID(page, "center"), Label: "Center", Variant: components.ButtonOutline},
{ID: tree.ID(page, "right"), Label: "Right", Variant: components.ButtonOutline},
}
group := components.BeginButtonGroup(canvas, theme, bounds, options).Vertical()
if id, ok := group.Paint().Clicked(); ok {
align = id
}禁用组合#
整体禁用仍保留每个动作的语义。
options := []components.ButtonGroupOption{
{ID: tree.ID(page, "left"), Label: "Left", Variant: components.ButtonOutline},
{ID: tree.ID(page, "center"), Label: "Center", Variant: components.ButtonOutline},
{ID: tree.ID(page, "right"), Label: "Right", Variant: components.ButtonOutline},
}
components.BeginButtonGroup(canvas, theme, bounds, options).Disabled().Paint()混合变体#
描边、危险与链接动作沿用各自 Button 变体的配色。
options := []components.ButtonGroupOption{
{ID: tree.ID(page, "save"), Label: "Save", Variant: components.ButtonOutline},
{ID: tree.ID(page, "delete"), Label: "Delete", Variant: components.ButtonDestructiveOutline},
{ID: tree.ID(page, "help"), Label: "Help", Variant: components.ButtonLink},
}
group := components.BeginButtonGroup(canvas, theme, bounds, options)
if id, ok := group.Paint().Clicked(); ok {
action = id
}安装
将组件源码复制到自己的项目,直接修改。
go run ./cmd/viva add --out ./components/local button-group源码组件用法#
此预览直接调用生产 Go 组件。稳定 ID、值与打开状态仍由应用持有;组件只负责绘制并声明交互语义。
- 标题旁的源码链接列出了全部选项与视觉决策。
- 请使用原生命令验证系统文本、焦点、无障碍与平台集成。
- 浏览器预览验证共享布局和场景行为,不代表原生性能。
验证边界#
示例覆盖当前 API 的代表性状态,但不表示每个桌面后端都已完成真机验证。
键盘操作
Tab 在控件间移动;Ctrl/⌘ K 搜索组件。进入预览后,键盘事件由 Go 运行时处理。