Mobile Button 移动端按钮
提供适合手指触控的移动端按钮,支持主要、危险、加载、尺寸、块级和圆角样式。
何时使用
- 需要提交、确认、打开页面等明确操作时使用
mobile.Button。 - 表单底部的主提交按钮通常使用
Primary().Block()。 - 行内切换、导航或列表操作应使用对应的 Cell、Tabs 或 NavBar,不要全部改成按钮。
- 正在等待异步结果时使用
Loading(true),避免重复提交。
在线体验
真实 gallery 展示默认、主要、成功、危险、加载、尺寸、圆角和禁用按钮。
基本用法
按钮是否被点击由当前帧的 Clicked 返回;页面状态需要由调用方保存。
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/mobile"
)
var saved bool
func page(c *ui.Context) {
if mobile.Button(c, "保存").Primary().Clicked() {
saved = true
}
if saved {
ui.Text(c, "已保存")
}
}常见用法
主要和块级按钮
Primary 表示主要操作,Block 让按钮占据可用宽度。
func submitButton(c *ui.Context) {
mobile.Button(c, "提交").Primary().Block().Size(mobile.Large)
}颜色和填充
Color 使用移动组件约定的颜色名,Fill("outline") 生成描边按钮。
func variants(c *ui.Context) {
mobile.Button(c, "成功").Color("success")
mobile.Button(c, "危险").Danger().Fill("outline")
}尺寸和圆角
尺寸使用 Mini、Small、Middle、Large;Round 适合短标签或胶囊式操作。
func sizes(c *ui.Context) {
mobile.Button(c, "迷你").Size(mobile.Mini)
mobile.Button(c, "小号").Size(mobile.Small)
mobile.Button(c, "圆角").Round().Primary()
}加载和禁用
加载态会阻止重复点击;禁用态应由业务状态决定。
var submitting bool
func states(c *ui.Context) {
if mobile.Button(c, "发送").Primary().Loading(submitting).Clicked() {
submitting = true
}
mobile.Button(c, "不可用").Disabled(true)
}图标和无障碍标签
图标按钮或文案不完整时用 Icon 和 A11yLabel 补充语义。
import "github.com/tangoui/tango/ui/icon"
func iconButton(c *ui.Context) {
mobile.Button(c, "").Icon(icon.Plus).A11yLabel("新建消息")
}主题与样式
Button 的主色、危险色、圆角、尺寸和加载态读取 Provider 的移动主题。Ant Mobile、Vant、shadcn 的填充和边框配方不同;ConfigProvider.Defaults("mobile.Button", ui.Props{"size": mobile.Large}) 可设置默认属性,显式方法优先。
注意事项
Clicked 是当前帧事件,提交状态应放在稳定变量中。异步提交期间设置 Loading,完成或失败后恢复。按钮文案要描述动作,图标按钮必须设置 A11yLabel;确保触控区域足够大并支持 Enter、Space 和屏幕阅读器 Press。危险按钮应配合确认,而不是只依赖红色。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
Mobile · 表单:Form / Field / Stepper / Button
gallery 演示 MobileForm 的 Render 函数,源码 page_mobile_gallery.go#L134。
加载中…
Mobile · 展示:Swiper / Skeleton / Empty / Result / Toast
gallery 演示 MobileDisplay 的 Render 函数,源码 page_mobile_gallery.go#L134。
加载中…
Mobile · 导航:NavBar / SearchBar / Tabs / NoticeBar / Grid / TabBar
gallery 演示 MobileNav 的 Render 函数,源码 page_mobile_gallery.go#L134。
加载中…
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
mobile.Button
func Button(c *ui.Context, label string) *ButtonWButton adds a button labelled label. In the shadcn style the default color is the outline variant, "primary" the near-black default variant and "danger" the destructive one, with medium-weight labels and h-11 (Middle) touch heights.
ConfigProvider defaults ("mobile.Button"): "size" ("mini", "small", "middle", "large", a mobile.Size or a ui.ControlSize; otherwise the ConfigProvider size), "shape" ("round" or "default"), "fill" ("solid", "outline", "none"), "color" (string), "block" (bool).
返回 *ButtonW,可链式调用的方法见 ButtonW 方法。
mobile.Badge
func Badge(c *ui.Context, content string, fn func()) *ui.BoxBadge wraps content with a badge at its top-end corner (top right; top left in RTL): content is the text ("99+"), "dot" for a dot, or "" for none.
返回通用容器 *ui.Box,可用的布局与样式方法见 Box 的 API。
mobile.Tag
func Tag(c *ui.Context, label string) *TagWTag adds a tag (a shadcn Badge in the shadcn style: rounded-md, text-xs medium; "default" is the secondary variant).
ConfigProvider defaults ("mobile.Tag"): "color" (string), "fill" ("solid", "outline", "light"), "round" (bool).
返回 *TagW,可链式调用的方法见 TagW 方法。
mobile.Grid
func Grid(c *ui.Context, cols int, items []GridItem) *GridWGrid adds a grid of items in cols columns. In the shadcn style the icons sit on muted rounded-xl tiles and cells have no separators.
ConfigProvider defaults ("mobile.Grid"): "bordered" (bool).
返回 *GridW,可链式调用的方法见 GridW 方法。
mobile.NoticeBar
func NoticeBar(c *ui.Context, msg string) *NoticeBarWNoticeBar adds an announcement bar showing msg (a neutral shadcn Alert strip in the shadcn style).
ConfigProvider defaults ("mobile.NoticeBar"): "color" (string), "closeable", "scroll" (bool).
返回 *NoticeBarW,可链式调用的方法见 NoticeBarW 方法。
mobile.Skeleton
func Skeleton(c *ui.Context) *SkeletonWSkeleton adds a placeholder; choose parts with Title, Paragraph, Avatar. The shadcn style pulses rounded-md muted bars instead of a shimmer.
ConfigProvider defaults ("mobile.Skeleton"): "title", "avatar", "animated" (bool), "rows" (int).
返回 *SkeletonW,可链式调用的方法见 SkeletonW 方法。
mobile.Empty
func Empty(c *ui.Context) *EmptyWEmpty adds an empty state with the localized "暂无数据" (shadcn: an icon on a muted tile above the muted caption).
ConfigProvider defaults ("mobile.Empty"): "description" (string).
返回 *EmptyW,可链式调用的方法见 EmptyW 方法。
mobile.Result
func Result(c *ui.Context, status, title string) *ResultWResult adds an outcome block: status "success", "error", "info", "waiting" or "warning".
返回 *ResultW,可链式调用的方法见 ResultW 方法。
ButtonW 方法
mobile.ButtonW:ButtonW is a touch button sized for fingers.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name (icon-only buttons). | A11yLabel(s string) *ButtonW |
Block | Block stretches the button to the full width. | Block() *ButtonW |
Clicked | Clicked reports a tap this frame. | Clicked() bool |
Color | Color sets the color: "default", "primary", "success", "warning", "danger". | Color(s string) *ButtonW |
Danger | Danger is Color("danger"). | Danger() *ButtonW |
Disabled | Disabled greys the button out. | Disabled(on bool) *ButtonW |
Fill | Fill sets "solid" (default), "outline" or "none". | Fill(s string) *ButtonW |
ID | ID returns the button's ID. | ID() ui.ID |
Icon | Icon adds a leading icon. | Icon(ic *icon.Icon) *ButtonW |
Layout | Layout applies layout options. | Layout(opts ...layout.Option) *ButtonW |
Loading | Loading shows a spinner and ignores taps. | Loading(on bool) *ButtonW |
Primary | Primary is Color("primary"). | Primary() *ButtonW |
Round | Round makes the ends fully rounded. | Round() *ButtonW |
Size | Size sets the size. | Size(s Size) *ButtonW |
TagW 方法
mobile.TagW:TagW is a small label.
| 方法 | 说明 | 签名 |
|---|---|---|
Color | Color sets "default", "primary", "success", "warning", "danger". | Color(s string) *TagW |
Light | Light uses a tinted background with colored text. | Light() *TagW |
Outline | Outline draws a border instead of a solid fill. | Outline() *TagW |
Round | Round makes the tag pill-shaped. | Round() *TagW |
GridW 方法
mobile.GridW:GridW is a grid of icon + text entries.
| 方法 | 说明 | 签名 |
|---|---|---|
Border | Border toggles cell separators. | Border(on bool) *GridW |
Clicked | Clicked returns the index tapped this frame. | Clicked() (int, bool) |
NoticeBarW 方法
mobile.NoticeBarW:NoticeBarW is a one-line announcement bar, optionally scrolling.
| 方法 | 说明 | 签名 |
|---|---|---|
Closeable | Closeable shows a close button. | Closeable() *NoticeBarW |
Closed | Closed reports a tap on the close button this frame. | Closed() bool |
Color | Color sets "default" (orange), "alert", "error" or "info". | Color(s string) *NoticeBarW |
Scroll | Scroll makes long text scroll (marquee; towards the start, so to the right in RTL). | Scroll() *NoticeBarW |
SkeletonW 方法
mobile.SkeletonW:SkeletonW is a loading placeholder of grey bars.
| 方法 | 说明 | 签名 |
|---|---|---|
Animated | Animated toggles the shimmer (on by default when animations run). | Animated(on bool) *SkeletonW |
Avatar | Avatar shows a round avatar placeholder. | Avatar() *SkeletonW |
Paragraph | Paragraph sets the number of text rows. | Paragraph(n int) *SkeletonW |
Title | Title shows a title bar. | Title() *SkeletonW |
EmptyW 方法
mobile.EmptyW:EmptyW is an empty-state placeholder.
| 方法 | 说明 | 签名 |
|---|---|---|
Description | Description replaces the caption. | Description(s string) *EmptyW |
ResultW 方法
mobile.ResultW:ResultW is a full-width outcome page block.
| 方法 | 说明 | 签名 |
|---|---|---|
Description | Description adds an explanation under the title. | Description(s string) *ResultW |
GridItem 参数类型
mobile.GridItem:GridItem is one cell of a Grid.
| 字段 | 说明 | 类型 |
|---|---|---|
Text | string | |
Icon | *icon.Icon | |
Badge | "", "dot" or count text | string |