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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
| type Todo struct {
Title string
Done bool
}
var todos []Todo
func main() {
a := app.New()
w := a.NewWindow("Todo")
w.Resize(fyne.NewSize(400, 600))
input := widget.NewEntry()
input.SetPlaceHolder("输入新任务")
list := widget.NewList(
func() int { return len(todos) },
func() fyne.CanvasObject {
return container.NewHBox(
widget.NewCheck("", nil),
widget.NewLabel(""),
)
},
func(i int, o fyne.CanvasObject) {
c := o.(*fyne.Container)
c.Objects[0].(*widget.Check).SetChecked(todos[i].Done)
c.Objects[1].(*widget.Label).SetText(todos[i].Title)
},
)
addBtn := widget.NewButton("添加", func() {
if input.Text != "" {
todos = append(todos, Todo{Title: input.Text})
input.SetText("")
list.Refresh()
}
})
w.SetContent(container.NewVBox(
container.NewBorder(nil, nil, nil, addBtn, input),
list,
))
w.ShowAndRun()
}
|