Fyne-Widget
软件包widget定义了许多小部件 ```go import "fyne.io/fyne/v2/widget" ``` ## Accordion 下拉框 ### 定义 ```go type Accordion struct { BaseWidget // 项目 Items []*AccordionItem // 是否同时打开多个项目 MultiOpen bool } ``` ### 示例 ```go 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("test") 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() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoWidget1.png) ## Button 按钮 ### 定义 ```go type Button struct { DisableableWidget // 按钮的文本或图标 Text string Icon fyne.Resource // 按钮突出程度 Importance ButtonImportance // 按钮对齐方式 Alignment ButtonAlign // 图标位置 IconPlacement ButtonIconPlacement // 按钮绑定的函数 OnTapped func() `json:"-"` } ``` ### 示例 ```go 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("test") 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() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoWidget2.png) ## Card 卡片 ### 定义 ```go type Card struct { BaseWidget // 标题 Title, Subtitle string // 图片 Image *canvas.Image // 内容 Content fyne.CanvasObject } ``` ### 示例 ```go 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("test") c := widget.NewCard("Hello", "World", canvas.NewCircle(color.White)) Window.SetContent(c) Window.ShowAndRun() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoWidget3.png) ## Check 选择框 ### 定义 ```go type Check struct { DisableableWidget // 文本 Text string // 当前选择状态 Checked bool // 触发时的函数 OnChanged func(bool) `json:"-"` } ``` ### 示例 ```go 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("test") 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() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoWidget4.png) ## CheckGroup 多选框 ### 定义 ```go type CheckGroup struct { DisableableWidget // 选项是否水平排列 Horizontal bool // 是否至少选择一个 Required bool // 触发时的函数,传入被选择字符串的列表 OnChanged func([]string) `json:"-"` // 选项列表 Options []string // 被选择字符串的列表 Selected []string } ``` ### 示例 ```go 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("test") 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() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoWidget5.png) ## Entry 输入框 ### 定义 ```go 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:"-"` } ``` ### 示例 ```go 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("test") 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() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoWidget6.png) ## Form 表单 ### 定义 ```go type Form struct { BaseWidget // 表单内容 Items []*FormItem // 提交函数 OnSubmit func() `json:"-"` // 取消函数 OnCancel func() `json:"-"` // 提交按钮文本 SubmitText string // 取消按钮文本 CancelText string } ``` ### 示例 ```go 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("test") box := container.NewVBox() l := widget.NewLabel("") f := widget.NewForm() hello := widget.NewEntry() world := widget.NewEntry() f.AppendItem(widget.NewFormItem("hello", hello)) f.Append("world", world) f.OnSubmit = func() { s := hello.Text + world.Text l.Text = s l.Refresh() } f.Refresh() box.Add(l) box.Add(f) Window.SetContent(box) Window.ShowAndRun() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoWidget7.png) ## Hyperlink 超链接 ### 定义 ```go type Hyperlink struct { BaseWidget // 文本 Text string // 连接 URL *url.URL // 对齐方式 Alignment fyne.TextAlign // 文本包装 Wrapping fyne.TextWrap // 文本样式 TextStyle fyne.TextStyle // 点击时触发 OnTapped func() `json:"-"` } ``` ### 示例 ```go package main import ( "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/widget" "net/url" ) func main() { App := app.New() Window := App.NewWindow("test") h := widget.NewHyperlink("Hello", &url.URL{Path: "http://leafbackaut.cn"}) Window.SetContent(h) Window.ShowAndRun() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoWidget8.png) ## Label 标签 ### 定义 ```go type Label struct { BaseWidget // 文本 Text string // 对齐方式 Alignment fyne.TextAlign // 文本包装 Wrapping fyne.TextWrap // 文本样式 TextStyle fyne.TextStyle } ``` ### 示例 ```go package main import ( "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/widget" ) func main() { App := app.New() Window := App.NewWindow("test") l := widget.NewLabel("Hello") Window.SetContent(l) Window.ShowAndRun() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoWidget9.png) ## List 列表 ### 定义 ```go type List struct { BaseWidget // 可以用来重写的函数 Length func() int `json:"-"` CreateItem func() fyne.CanvasObject `json:"-"` UpdateItem func(id ListItemID, item fyne.CanvasObject) `json:"-"` OnSelected func(id ListItemID) `json:"-"` OnUnselected func(id ListItemID) `json:"-"` } ``` ### 示例 使用NewList创建新的列表,参数分别代表创建列表长度,创建列表单个元素的内容,更新每个列表元素的函数 ```go func NewList(length func() int, createItem func() fyne.CanvasObject, updateItem func(ListItemID, fyne.CanvasObject)) *List ``` 一个列表 ```go package main import ( "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/widget" ) func main() { App := app.New() Window := App.NewWindow("test") var data = []string{"a", "b", "c"} l := widget.NewList(func() int { return len(data) }, func() fyne.CanvasObject { return widget.NewLabel("Hello") }, func(id widget.ListItemID, object fyne.CanvasObject) { object.(*widget.Label).SetText(data[id]) }) Window.SetContent(l) Window.ShowAndRun() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoWidget10.png) ## Menu 菜单 ### 定义 ```go type Menu struct { BaseWidget // 菜单内容 Items []fyne.CanvasObject OnDismiss func() } ``` ### 示例 ```go package main import ( "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/widget" ) func main() { App := app.New() Window := App.NewWindow("test") m := widget.NewMenu(fyne.NewMenu("Menu")) m.Items = append(m.Items, widget.NewLabel("hello")) m.Items = append(m.Items, widget.NewLabel("World")) Window.SetContent(m) Window.ShowAndRun() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoWidget11.png) ## ProgressBar 进度条 ### 定义 ```go type ProgressBar struct { BaseWidget // 最小值,最大值,当前值 Min, Max, Value float64 // 自定义文本样式 TextFormatter func() string `json:"-"` } ``` ### 示例 ```go package main import ( "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/widget" ) func main() { App := app.New() Window := App.NewWindow("test") p := widget.NewProgressBar() p.Max = 10 p.Min = 0 p.Value = 6 Window.SetContent(p) Window.ShowAndRun() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoWidget12.png) ## RadioGroup 单选列表 ### 定义 ```go type RadioGroup struct { DisableableWidget // 排列方式 Horizontal bool // 是否必选一个 Required bool // 选择时调用这个函数,传入一个字符串 OnChanged func(string) `json:"-"` // 选项列表 Options []string // 被选的字符串 Selected string } ``` ### 示例 ```go 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("test") box := container.NewVBox() l := widget.NewLabel("") c := []string{"Hello", "World"} r := widget.NewRadioGroup(c, func(s string) { l.Text = s l.Refresh() }) box.Add(l) box.Add(r) Window.SetContent(box) Window.ShowAndRun() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoWidget13.png) ## RichText 富文本 ### 定义 ```go type RichText struct { BaseWidget // 富文本标记 Segments []RichTextSegment Wrapping fyne.TextWrap Scroll widget.ScrollDirection } ``` ### 示例 ```go 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("test") box := container.NewVBox() m := widget.NewRichTextFromMarkdown("## Hello") t := widget.NewRichTextWithText("World") box.Add(m) box.Add(t) Window.SetContent(box) Window.ShowAndRun() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoWidget14.png) ## Select 选择框 ### 定义 ```go type Select struct { DisableableWidget // 对齐方式 Alignment fyne.TextAlign // 当前选择的字符串 Selected string // 选项列表 Options []string PlaceHolder string // 单击选项调用函数 OnChanged func(string) `json:"-"` } ``` ### 示例 ```go 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("test") box := container.NewVBox() l := widget.NewLabel("") var o = []string{"A", "B", "c"} s := widget.NewSelect(o, func(s string) { l.Text = s l.Refresh() }) s.PlaceHolder = "Hello World" box.Add(l) box.Add(s) Window.SetContent(box) Window.ShowAndRun() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoWidget15.png) ## SelectEntry 输入选择框 ### 定义 ```go type SelectEntry struct { // 输入框 Entry dropDown *fyne.Menu popUp *PopUpMenu // 选项列表 options []string } ``` ### 示例 ```go 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("test") box := container.NewVBox() var o = []string{"A", "B", "C"} s := widget.NewSelectEntry(o) box.Add(s) Window.SetContent(box) Window.ShowAndRun() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoWidget16.png) ## Slider 滑块 ### 定义 ```go type Slider struct { BaseWidget // 一些值 Value float64 Min float64 Max float64 Step float64 // 方向,垂直或水平 Orientation Orientation // 数据改变时触发函数 OnChanged func(float64) } ``` ### 示例 ```go package main import ( "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/container" "fyne.io/fyne/v2/widget" "strconv" ) func main() { App := app.New() Window := App.NewWindow("test") box := container.NewVBox() l := widget.NewLabel("") s := widget.NewSlider(1, 10) s.Step = 0.01 s.OnChanged = func(f float64) { l.Text = strconv.FormatFloat(f, 'f', 3, 64) l.Refresh() } box.Add(l) box.Add(s) Window.SetContent(box) Window.ShowAndRun() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoWidget17.png) ## Table 表格 ### 定义 ```go type Table struct { BaseWidget // 一些参数 Length func() (int, int) `json:"-"` CreateCell func() fyne.CanvasObject `json:"-"` UpdateCell func(id TableCellID, template fyne.CanvasObject) `json:"-"` OnSelected func(id TableCellID) `json:"-"` OnUnselected func(id TableCellID) `json:"-"` } ``` ### 示例 ```go package main import ( "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/widget" ) func main() { App := app.New() Window := App.NewWindow("test") var data = [][]string{{"A", "B", "C"}, {"D", "E", "F"}, {"G", "H", "I"}} t := widget.NewTable(func() (int, int) { return len(data), len(data[0]) }, func() fyne.CanvasObject { return widget.NewLabel("") }, func(id widget.TableCellID, object fyne.CanvasObject) { object.(*widget.Label).SetText(data[id.Col][id.Row]) }) Window.SetContent(t) Window.ShowAndRun() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoWidget18.png) ## TextGrid 文本网格 ### 定义 ```go type TextGrid struct { BaseWidget // 每一行的元素 Rows []TextGridRow // 是否显示行数 ShowLineNumbers bool // 是否在空格处打点 ShowWhitespace bool // Tab键的宽度 TabWidth } ``` ### 示例 ```go package main import ( "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/widget" ) func main() { App := app.New() Window := App.NewWindow("test") t := widget.NewTextGrid() var cell1 []widget.TextGridCell var hello = []rune("Hello") for _, s := range hello { cell1 = append(cell1, widget.TextGridCell{Rune: s}) } var cell2 []widget.TextGridCell var world = []rune(" World") for _, s := range world { cell2 = append(cell2, widget.TextGridCell{Rune: s}) } row1 := widget.TextGridRow{ Cells: cell1, Style: nil, } row2 := widget.TextGridRow{ Cells: cell2, Style: nil, } t.SetRow(0, row1) t.SetRow(1, row2) t.ShowLineNumbers = true t.ShowWhitespace = true Window.SetContent(t) Window.ShowAndRun() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoWidget19.png) ## Toolbar 工具栏 ### 定义 ```go type Toolbar struct { BaseWidget // 工具栏选项 Items []ToolbarItem } ``` ### 示例 ```go package main import ( "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/container" "fyne.io/fyne/v2/theme" "fyne.io/fyne/v2/widget" ) func main() { App := app.New() Window := App.NewWindow("test") box := container.NewBorder(t, nil, nil, nil, nil) t := widget.NewToolbar() t.Append(widget.NewToolbarAction(theme.HomeIcon(), func() {})) t.Append(widget.NewToolbarSeparator()) t.Append(widget.NewToolbarAction(theme.AccountIcon(), func() {})) t.Append(widget.NewToolbarAction(theme.HelpIcon(), func() {})) Window.SetContent(box) Window.ShowAndRun() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoWidget20.png)
创建时间:2023-08-31
|
最后修改:2024-04-14
|
©允许规范转载
酷酷番茄
首页
文章
友链