Choices
There are various widgets available to present the user with a choice, these include a checkbox, radio group and select popup.
The widget.Check provides a simple yes/no choice and is created
using a string label. Each of these widgets also takes a
“changed” func(...) where the parameter is of the appropriate
type. widget.NewCheck(..) therefore takes a string parameter for
the label and a func(bool) param for the change handler.
You can also use the Checked field to get the boolean value.
The radio widget is similar, but the first parameter is a
slice of strings that represents each of the options.
The change function expects a string parameter this time
to return the currently selected value. Call widget.NewRadio(...)
to construct the radio widget, you can use this reference
later to read the Selected field instead of using the change
callback.
The select widget is identical in the constructor signature
as the radio widget. Calling widget.NewSelect(...) will
instead show a button that displays a popup when tapped from
which the user can make a selection. This is better for long
lists of options.
Example Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main
import (
"log"
"fyne.io/fyne/app"
"fyne.io/fyne/widget"
)
func main() {
myApp := app.New()
myWindow := myApp.NewWindow("Choice Widgets")
check := widget.NewCheck("Optional", func(value bool) {
log.Println("Check set to", value)
})
radio := widget.NewRadio([]string{"Option 1", "Option 2"}, func(value string) {
log.Println("Radio set to", value)
})
combo := widget.NewSelect([]string{"Option 1", "Option 2"}, func(value string) {
log.Println("Select set to", value)
})
myWindow.SetContent(widget.NewVBox(check, radio, combo))
myWindow.ShowAndRun()
}