Mobile PickerView 滚轮选择器
以内联滚轮展示单列、多列、级联或日期选择,并把当前选择写回调用方值。
何时使用
- 需要把选择器直接嵌入页面时使用
PickerView、CascadePickerView或DatePickerView。 - 需要底部弹出、确认后再提交值时使用
Picker。 - 选项很多且不适合滚轮时使用 List、Select 或搜索型输入,不要强行压缩成滚轮。
- 日期业务应使用
DatePickerView的DatePrecision和边界,而不是手写日期字符串滚轮。
在线体验
真实 gallery 展示级联、多列和日期滚轮,拖动各列观察绑定值变化。
基本用法
PickerView 接收每列的 ui.Option,value 是每列当前选项值的切片。
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/mobile"
)
var selected = []string{"周三", "下午"}
func page(c *ui.Context) {
mobile.PickerView(c, [][]ui.Option{
ui.Opts("周一", "周二", "周三", "周四", "周五"),
ui.Opts("上午", "下午", "晚上"),
}, &selected).Visible(5).Show()
}常见用法
级联选择
CascaderOption 用 Children 描述层级,绑定值按当前路径保存。
var area = []string{"zj", "hz", "bj"}
func cascade(c *ui.Context) {
options := []ui.CascaderOption{
{Label: "浙江", Value: "zj", Children: []ui.CascaderOption{
{Label: "杭州", Value: "hz", Children: []ui.CascaderOption{
{Label: "西湖区", Value: "xh"},
}},
}},
}
mobile.CascadePickerView(c, options, &area).Show()
}日期选择
DatePickerView 直接绑定 time.Time,精度从年到分钟由 DatePrecision 指定。
import "time"
var date = time.Date(2026, 11, 12, 9, 30, 0, 0, time.UTC)
func datePicker(c *ui.Context) {
mobile.DatePickerView(c, &date, mobile.PrecisionDay).Visible(5).Show()
}弹出确认式选择
Picker 是带工具栏的底部 Popup;绑定值只有在确认后才更新。
var pickerOpen bool
var week = []string{"周三", "下午"}
func confirmed(c *ui.Context) {
p := mobile.Picker(c, &pickerOpen, "选择时间").Columns([][]ui.Option{
ui.Opts("周一", "周二", "周三"), ui.Opts("上午", "下午"),
}, &week).Show()
if p.Confirmed() {
// week 已经是确认后的值
}
if p.Cancelled() {
// 保留原值
}
}处理变更和无障碍
Changed 只在当前帧有新选择时为真;可为整列或每列设置标签。
func accessible(c *ui.Context) {
w := mobile.PickerView(c, [][]ui.Option{ui.Opts("A", "B")}, &selected).
A11yLabel("工作日").A11yLabels("日期").Show()
if w.Changed() {
// 根据 selected 更新预览
}
}注意事项
选择值切片和日期指针要放在稳定状态中;PickerView 的值会在滚轮变化时更新,而 Picker 的值在确认前保持原值。级联路径中的 Value 必须唯一且与选项树一致。滚轮可见项用 Visible 控制,选项很多时应限制数量或改用搜索。为多列提供有意义的 A11yLabels,确保键盘和屏幕阅读器能区分列。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
Mobile · PickerView / CascadePickerView
gallery 演示 MobileCascade 的 Render 函数,源码 page_mobile_gallery.go#L134。
加载中…
Mobile · Picker 滚轮选择器(日期时间)
gallery 演示 MobilePicker 的 Render 函数,源码 page_mobile_gallery.go#L134。
加载中…
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
mobile.PickerView
func PickerView(c *ui.Context, cols [][]ui.Option, value *[]string) *PickerViewWPickerView shows one wheel per column; value holds the selected option Value of each column. The shadcn style marks the selected row with a muted rounded-md band instead of hairlines.
ConfigProvider defaults ("mobile.PickerView", also read by the views of Picker, CascadePickerView and DatePickerView): "visibleItemCount" (int, at least 3).
返回 *PickerViewW,可链式调用的方法见 PickerViewW 方法。
mobile.CascadePickerView
func CascadePickerView(c *ui.Context, opts []ui.CascaderOption, value *[]string) *PickerViewWCascadePickerView shows linked wheels for a tree of options (province / city / district); value holds the Value path.
返回 *PickerViewW,可链式调用的方法见 PickerViewW 方法。
mobile.DatePickerView
func DatePickerView(c *ui.Context, value *time.Time, p DatePrecision, bounds ...time.Time) *PickerViewWDatePickerView shows year / month / day (… hour / minute) wheels bound to *value. Optional bounds are the earliest and latest selectable time (default: ten years either side of the value).
返回 *PickerViewW,可链式调用的方法见 PickerViewW 方法。
mobile.Picker
func Picker(c *ui.Context, open *bool, title string) *PickerWPicker creates a popup picker bound to *open; choose the wheels with Columns, Cascade or Date, then call Show. The shadcn style shows it as a Drawer (grab handle, semibold title).
ConfigProvider defaults ("mobile.Picker"): "closeOnMaskClick" (bool); the wheels read "mobile.PickerView".
返回 *PickerW,可链式调用的方法见 PickerW 方法。
PickerViewW 方法
mobile.PickerViewW:PickerViewW is an inline set of scroll wheels.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel names the wheels for screen readers (each wheel is an adjustable slider whose value is the selected option); A11yLabels names them one by one. | A11yLabel(s string) *PickerViewW |
A11yLabels | A11yLabels sets the accessible name of each wheel. | A11yLabels(names ...string) *PickerViewW |
Changed | Changed reports a wheel settling on a new value this frame. | Changed() bool |
ID | ID returns the wheel row's ID. | ID() ui.ID |
Show | Show renders the wheels. | Show() *PickerViewW |
Visible | Visible sets how many rows each wheel shows (odd numbers centre best). | Visible(n int) *PickerViewW |
PickerW 方法
mobile.PickerW:PickerW is a bottom popup with a toolbar (cancel / title / confirm) and wheels; the bound value only changes on confirm.
| 方法 | 说明 | 签名 |
|---|---|---|
Cancelled | Cancelled reports a cancel this frame. | Cancelled() bool |
Cascade | Cascade shows linked wheels for a tree of options. | Cascade(opts []ui.CascaderOption, value *[]string) *PickerW |
Columns | Columns shows independent wheels; value receives the Values on confirm. | Columns(cols [][]ui.Option, value *[]string) *PickerW |
Confirmed | Confirmed reports a confirm this frame (the bound value was updated). | Confirmed() bool |
Date | Date shows date / time wheels down to precision p (optional bounds as in DatePickerView). | Date(value *time.Time, p DatePrecision, bounds ...time.Time) *PickerW |
Show | Show renders the popup. | Show() *PickerW |
Option 参数类型
ui.Option:Option is an item of a Select / RadioGroup / CheckboxGroup. Group puts the option under a titled group in Select / MultiSelect / VirtualSelect dropdowns (Ant OptGroup, Element el-option-group); options of one group are listed together in order of the group's first appearance.
| 字段 | 说明 | 类型 |
|---|---|---|
Label | string | |
Value | string | |
Disabled | bool | |
Group | string |
CascaderOption 参数类型
ui.CascaderOption:CascaderOption is a node of a Cascader tree. With LoadData, a node without Children that is not IsLeaf loads its children when expanded.
| 字段 | 说明 | 类型 |
|---|---|---|
Label | string | |
Value | string | |
Disabled | bool | |
Children | []CascaderOption | |
IsLeaf | never has children (relevant with LoadData) | bool |