Selection controls
Selection controls let the user choose instead of type. Each one reports the
choice through an OnChanged function and keeps it in an exported field you
can read at any time.
Which one to use
| You want the user to | Use |
|---|---|
| Agree to one thing, or turn one option on | Check |
| Turn a setting on or off that applies at once | Switch |
| Pick any number from a short list | CheckGroup |
| Pick exactly one from a short list, all options visible | RadioGroup |
| Pick one or several options shown as chips that wrap onto new rows | ChipGroup |
| Pick one from a longer list, shown on demand | Select (or SearchSelect for long lists, see Text input) |
| Pick one of 2 to 5 options shown as buttons | SegmentedButton (see Buttons) |
| Pick a value along a range | Slider |
| Pick a low and high bound | RangeSlider |
| Set an exact number by stepping up and down | SpinBox |
Check and CheckGroup
terms := widget.NewCheck("I accept the terms", func(on bool) {
status.SetText(fmt.Sprint("Accepted: ", on))
})
toppings := widget.NewCheckGroup([]string{"Cheese", "Olives", "Basil"}, func(sel []string) {
status.SetText("Toppings: " + strings.Join(sel, ", "))
})
toppings.Horizontal = true
toppings.SetSelected([]string{"Cheese"})Check is a
check box with a label. Checked holds its state and SetChecked changes it
from code (safe from any goroutine). Setting Partial shows a dash, the
“some but not all” state of a parent check box. The user cannot set it; a tap
on a partial check box checks it. NewCheckWithData connects the check box
to a binding.Bool.
CheckGroup
shows one check box per option and passes the list of checked options to
OnChanged. Horizontal lays them out in a row. With Required set, the
user cannot uncheck the last checked option. Append and Remove change the
options later.
RadioGroup
size := widget.NewRadioGroup([]string{"Small", "Medium", "Large"}, func(s string) {
status.SetText("Size: " + s)
})
size.Required = true
size.SetSelected("Medium")RadioGroup
keeps at most one option selected and passes its text to OnChanged.
Tapping the selected option again clears the selection, unless Required is
set. SetSelected picks an option from code and is safe to call from any
goroutine.
ChipGroup
sizes := widget.NewChipGroup([]string{"Small", "Medium", "Large"}, false, func(picked []string) {
status.SetText(fmt.Sprint("Size: ", picked))
})
sizes.Required = true
sizes.Selected = []string{"Medium"}
toppings := widget.NewChipGroup([]string{"Cheese", "Olives", "Peppers", "Onions"}, true, func(picked []string) {
status.SetText(fmt.Sprint("Toppings: ", picked))
})ChipGroup
shows one filter chip per option. Each chip is as wide
as its label, and the chips wrap onto the next row when a row is full.
- Without
Multiple, one chip can be selected, and tapping another moves the selection to it. WithMultiple, any number can be selected. - Tapping a selected chip deselects it, unless
Requiredis set and it is the last one selected. OnChangedreceives the selected options, in the order they were selected.Selectedholds them.SetSelectedselects options from code, firesOnChangedand refreshes.SetSelectedSilentlydoes the same without firing anything.Alignmentplaces each row of chips.bellui.TextAlignLeading(the default) puts it at the start,TextAlignCenterin the middle andTextAlignTrailingat the end. A shorter last row is placed the same way. Start and end follow the layout direction, so under right-to-left the chips run from the right edge.
Excluding options
genres := widget.NewChipGroup([]string{"Rock", "Jazz", "Pop", "Folk"}, true, nil)
genres.Excludable = true
genres.OnStateChanged = func(option string, state widget.ChipState) {
switch state {
case widget.ChipStateSelected:
status.SetText("Show " + option)
case widget.ChipStateExcluded:
status.SetText("Hide " + option)
case widget.ChipStateOff:
status.SetText(option + " no longer filtered")
}
}A filter often needs “not this one” as well as “only these”. Set Excludable
on a group with Multiple, and each tap moves a chip from off to selected,
to excluded, and back to off. Excludable does nothing without Multiple.
- An excluded chip shows a cross where the check mark goes, an error-coloured outline, and its label struck through, so the state does not depend on colour alone. Material 3 has no excluded filter chip, so this look is BellUI’s own. A screen reader reads it as “Rock, excluded”.
Excludedholds the excluded options, in the order they were excluded. An option is never in bothSelectedandExcluded.OnStateChangedreceives each option whose state changed, with its newChipState:ChipStateOff,ChipStateSelectedorChipStateExcluded. A tap changes one option. Both lists are already updated when it runs. The tap from selected to excluded also takes the option out ofSelected, soOnChangedfires first with the smaller selection.Statereturns one option’s state.SetExcludedexcludes options from code and firesOnStateChanged. If it takes an option out ofSelected, it firesOnChangedtoo.SetExcludedSilentlyfires nothing.- To refuse a change, put the lists back from
OnStateChangedwithSetSelectedSilentlyandSetExcludedSilently.
OnChanged keeps its func([]string) signature and still reports only the
selection. Code written for an earlier version works unchanged.
OnStateChanged runs in every group, not only excludable ones, after
OnChanged. In a single-choice group, a tap that moves the selection reports
two options: the one that lost the selection and the one that gained it.
Select
lang := widget.NewSelect([]string{"English", "Deutsch", "Français"}, func(s string) {
status.SetText("Language: " + s)
})
lang.PlaceHolder = "Language"Select
shows the current option and opens the full list when tapped. Until the user
picks something it shows PlaceHolder.
Selectedholds the chosen text.SelectedIndex()returns its position, or -1.SetSelectedandSetSelectedIndexchoose from code.ClearSelectedgoes back to the placeholder.SetOptionsreplaces the list.NewSelectWithDataconnects the selection to abinding.String.
Switch
wifi := widget.NewSwitch(func(on bool) {
status.SetText(fmt.Sprint("Wi-Fi: ", on))
})
wifi.On = trueSwitch is
the MD3 toggle with a sliding thumb. On holds the state and OnChanged
receives each change. It has no label of its own, so put a Label next to
it. Space toggles a focused switch.
Use a Switch for a setting that takes effect immediately. Use a Check when
the choice is applied later, for example when a form is submitted.
Slider
volume := widget.NewSlider(0, 100)
volume.Step = 1
volume.ShowValueIndicator = true
volume.OnChanged = func(v float64) {
status.SetText(fmt.Sprintf("Volume %.0f", v))
}
volume.OnChangeEnded = func(v float64) {
status.SetText(fmt.Sprintf("Volume set to %.0f", v))
}
volume.SetValue(40)
rating := widget.NewSlider(1, 5)
rating.Step = 1
rating.ShowTickMarks = trueSlider
moves between Min and Max. Step snaps the value; with Step above zero,
ShowTickMarks draws a mark at each step. ShowValueIndicator shows the
value above the thumb while dragging. Set Orientation to widget.Vertical
for an upright slider.
OnChangedruns continuously while the user drags.OnChangeEndedruns when the change is finished, for example when the drag ends. Save or send the value inOnChangeEnded.SetValueclamps the value into range and fires both callbacks, as if the user had moved it.SetValueSilentlymoves the thumb without firing them, for example to show playback position. Both are safe from any goroutine.Dragging()reports whether the user is dragging right now. A media player can use it to stop updating the position while the user scrubs.OnHoverandOnHoverEndreport the value under the pointer without moving the thumb, which suits a preview on a seek bar.
NewSliderWithData connects the value to a binding.Float.
RangeSlider
price := widget.NewRangeSlider(0, 500)
price.Step = 10
price.SetValues(100, 300)
price.OnChangeEnded = func(low, high float64) {
status.SetText(fmt.Sprintf("Price %.0f to %.0f", low, high))
}RangeSlider
has two thumbs. LowValue and HighValue hold the range, and both callbacks
receive both values. SetValues clamps, snaps to Step and swaps the values
if they arrive the wrong way round; SetValuesSilently does the same without
firing the callbacks. From the keyboard, the arrows along the slider move the
focused thumb, and the arrows across it switch between thumbs.
SpinBox
guests := widget.NewSpinBox(1, 12, 1)
guests.SetValue(2)
guests.OnChanged = func(v float64) {
status.SetText(fmt.Sprintf("%.0f guests", v))
}SpinBox
is a number field with increment and decrement buttons. Each press adds or
subtracts Step, and SetValue clamps to Min and Max. The up and down
arrow keys step the value when it has focus.
- The user can type a value. It is clamped to the range as they type, and the field shows the clamped value when they press Enter or leave the field.
- The field is as wide as the wider of
MinandMax. For a side with no limit, usemath.Inf(-1)ormath.Inf(1), which takes no width. - After setting
Value,Min,MaxorStepdirectly, callRefresh. It clamps the value, shows it formatted for the step and updates the buttons, without callingOnChanged.SetValueis the setter that calls it.