软件包widget定义了许多小部件
import "fyne.io/fyne/v2/widget"
type Accordion struct {
BaseWidget
// 项目
Items []*AccordionItem
// 是否同时打开多个项目
MultiOpen bool
}
package main
import (
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/widget"
"image/color"
"strconv"
)
func main() {
App := app.New()
Window := App.NewWindow("TabContainer Widget")
a1 := widget.NewAccordion()
for i := 0; i < 3; i++ {
a3 := widget.NewAccordion()
for j := 0; j < 4; j++ {
s := strconv.Itoa(i) + strconv.Itoa(j)
a3.Append(widget.NewAccordionItem(s, canvas.NewText(s, color.White)))
}
a2 := widget.NewAccordionItem(strconv.Itoa(i), a3)
a1.Append(a2)
}
Window.SetContent(a1)
Window.ShowAndRun()
}
type Button struct {
DisableableWidget
// 按钮的文本或图标
Text string
Icon fyne.Resource
// 按钮突出程度
Importance ButtonImportance
// 按钮对齐方式
Alignment ButtonAlign
// 图标位置
IconPlacement ButtonIconPlacement
// 按钮绑定的函数
OnTapped func() `json:"-"`
}
package main
import (
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
func main() {
App := app.New()
Window := App.NewWindow("TabContainer Widget")
l := widget.NewLabel("hello")
b := widget.NewButton("world", func() {
l.Text = "world"
l.Refresh()
})
box := container.NewVBox()
box.Add(l)
box.Add(b)
Window.SetContent(box)
Window.ShowAndRun()
}
type Card struct {
BaseWidget
// 标题
Title, Subtitle string
// 图片
Image *canvas.Image
// 内容
Content fyne.CanvasObject
}
package main
import (
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/widget"
"image/color"
)
func main() {
App := app.New()
Window := App.NewWindow("TabContainer Widget")
c := widget.NewCard("Hello", "World", canvas.NewCircle(color.White))
Window.SetContent(c)
Window.ShowAndRun()
}
type Check struct {
DisableableWidget
// 文本
Text string
// 当前选择状态
Checked bool
// 触发时的函数
OnChanged func(bool) `json:"-"`
}
package main
import (
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
func main() {
App := app.New()
Window := App.NewWindow("TabContainer Widget")
l := widget.NewLabel("Hello")
c := widget.NewCheck("World", func(b bool) {
if b {
l.Text = "World"
l.Refresh()
} else {
l.Text = "Hello"
l.Refresh()
}
})
box := container.NewVBox()
box.Add(l)
box.Add(c)
Window.SetContent(box)
Window.ShowAndRun()
}
type CheckGroup struct {
DisableableWidget
// 选项是否水平排列
Horizontal bool
// 是否至少选择一个
Required bool
// 触发时的函数,传入被选择字符串的列表
OnChanged func([]string) `json:"-"`
// 选项列表
Options []string
// 被选择字符串的列表
Selected []string
}
package main
import (
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
func main() {
App := app.New()
Window := App.NewWindow("TabContainer Widget")
l := widget.NewLabel("")
ss := []string{"A", "B", "C"}
c := widget.NewCheckGroup(ss, func(s []string) {
l.Text = ""
for _, i := range s {
l.Text += i
}
l.Refresh()
})
box := container.NewVBox()
box.Add(l)
box.Add(c)
Window.SetContent(box)
Window.ShowAndRun()
}
type Entry struct {
DisableableWidget
// 文本
Text string
// 文本样式
TextStyle fyne.TextStyle
PlaceHolder string
// 触发函数
OnChanged func(string) `json:"-"`
OnSubmitted func(string) `json:"-"`
// 输入框类型
Password bool
MultiLine bool
// 其它参数
Wrapping fyne.TextWrap
Validator fyne.StringValidator `json:"-"`
CursorRow, CursorColumn int
OnCursorChanged func() `json:"-"`
ActionItem fyne.CanvasObject `json:"-"`
}
package main
import (
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
func main() {
App := app.New()
Window := App.NewWindow("TabContainer Widget")
l := widget.NewLabel("")
e := widget.NewEntry()
e.OnChanged = func(s string) {
l.Text = e.Text
l.Refresh()
}
box := container.NewVBox()
box.Add(l)
box.Add(e)
Window.SetContent(box)
Window.ShowAndRun()
}