Toggle Group切换按钮组
查看源码components/toggle_group.go文本对齐#
一个调用方持有的单选按钮组。
items := tree.Sub(page, tree.Name("item"))
options := []components.ToggleGroupOption{
{ID: items.At(0), Label: "Left", Active: selected == 0},
{ID: items.At(1), Label: "Center", Active: selected == 1},
{ID: items.At(2), Label: "Right", Active: selected == 2},
}
group := components.BeginToggleGroup(canvas, theme, bounds, options).Outline().Spacing(0)
if id, ok := group.Paint().Clicked(); ok {
selected, _ = id.In(items)
}文本样式#
每个选项独立切换的多选按钮组。
items := tree.Sub(page, tree.Name("item"))
options := []components.ToggleGroupOption{
{ID: items.At(0), Label: "Bold", Active: flags&1 != 0},
{ID: items.At(1), Label: "Italic", Active: flags&2 != 0},
{ID: items.At(2), Label: "Underline", Active: flags&4 != 0},
}
group := components.BeginToggleGroup(canvas, theme, bounds, options).Outline()
group = group.Spacing(0).SizeToFit()
if id, ok := group.Paint().Clicked(); ok {
index, _ := id.In(items)
flags ^= 1 << index
}纵向与禁用#
纵向描边组,包含一个禁用选项。
items := tree.Sub(page, tree.Name("item"))
options := []components.ToggleGroupOption{
{ID: items.At(0), Label: "Left", Active: selected == 0},
{ID: items.At(1), Label: "Center", Active: selected == 1},
{ID: items.At(2), Label: "Right", Disabled: true},
}
group := components.BeginToggleGroup(canvas, theme, bounds, options).Vertical()
group = group.Outline().Spacing(1)
if id, ok := group.Paint().Clicked(); ok {
selected, _ = id.In(items)
}安装
将组件源码复制到自己的项目,直接修改。
go run ./cmd/viva add --out ./components/local toggle-group源码组件用法#
此预览直接调用生产 Go 组件。稳定 ID、值与打开状态仍由应用持有;组件只负责绘制并声明交互语义。
- 标题旁的源码链接列出了全部选项与视觉决策。
- 请使用原生命令验证系统文本、焦点、无障碍与平台集成。
- 浏览器预览验证共享布局和场景行为,不代表原生性能。
验证边界#
示例覆盖当前 API 的代表性状态,但不表示每个桌面后端都已完成真机验证。
键盘操作
Tab 在控件间移动;Ctrl/⌘ K 搜索组件。进入预览后,键盘事件由 Go 运行时处理。