Button
触发操作,支持多种样式和状态。
实时预览
正在加载预览…
参数配置
全部字段 19
ButtonOptions
Go 代码
Go
// Inside a view; c is *ui.Context, t is nui.Theme.
// Keep s (example state) outside the view. See the complete example.
nui.Button(c.Key("button"), t, "Save changes", nui.ButtonOptions{AccessibleLabel: cmp.Or("Save changes", "Example action"), Color: nui.ColorDefault, Size: nui.SizeDefault, Variant: nui.VariantDefault, Disabled: false, Loading: false, Leading: nil}).AlignSelf(ui.Start).OnClick(func() { s.Clicks++ })
ui.Textf(c, "Clicked %d times", s.Clicks)使用说明
点击按钮观察计数,再试试图标、Outline、LG、加载和禁用状态。操作计数保存在视图外,修改外观不会重复触发点击回调;重置会清除计数。
API 参考
Button
源码func Button(c *ui.Context, t Theme, label string, opts ButtonOptions) ui.ElementButtonOptions
源码type ButtonOptions struct {
Variant Variant
Color Color
Size Size
// Neutral selects the neutral palette; Color: Neutral is preferred.
Neutral, Disabled, Loading, Block, Square bool
AccessibleLabel string
Leading, Trailing *ui.SVG
LoadingIcon *ui.SVG
LoadingTrailing bool // Place loading at the trailing edge unless Leading is set.
Active bool
ActiveColor Color
ActiveVariant Variant
Animate Optional[bool]
UI Styles
Style func(root, label, leading, trailing ui.Element)
}| 字段 | 类型 | 说明 |
|---|---|---|
Variant | VariantGo 零值: | 组件外观样式,支持范围取决于组件。 |
Color | ColorGo 零值: | 语义颜色;默认值继承主题或组件配色。 |
Size | SizeGo 零值: | 组件尺寸;默认值继承主题或组件尺寸。 |
Neutral | boolGo 零值: | 旧版中性色开关;优先使用 Color: nui.Neutral。 |
Disabled | boolGo 零值: | 禁用组件及其交互。 |
Loading | boolGo 零值: | 显示加载状态;是否阻止操作由组件决定。 |
Block | boolGo 零值: | 让按钮占据可用宽度。 |
Square | boolGo 零值: | 使用适合方形内容的间距。 |
AccessibleLabel | stringGo 零值: | 覆盖控件的可访问名称,适用于纯图标操作。 |
Leading | *ui.SVGGo 零值: | 内容前方的 SVG 图标。 |
Trailing | *ui.SVGGo 零值: | 内容后方的 SVG 图标。 |
LoadingIcon | *ui.SVGGo 零值: | 覆盖默认加载图标。 |
LoadingTrailing | boolGo 零值: | 在末尾显示加载图标;显式前置图标优先。 |
Active | boolGo 零值: | 控制激活状态或动态效果是否运行。 |
ActiveColor | ColorGo 零值: | 激活状态的颜色覆盖。 |
ActiveVariant | VariantGo 零值: | 激活状态的外观覆盖。 |
Animate | Optional[bool]Go 零值: | 控制动画,并遵守系统的减少动态效果偏好。 |
UI | StylesGo 零值: | 以部件名称为键覆盖外观和间距。 |
Style | func(root, label, leading, trailing ui.Element)Go 零值: | 同步配置当前构建的部件;不要保留 Element。 |