我正在使用名为 Fyne for Go 的 GUI 框架。如何将项目动态添加到 List 小部件使用的数组中?编码:
var componentsList = []string{"test1: test1"}
func main() {
app := app.New()
window := app.NewWindow("Delta Interface")
componentsTree := widget.NewList(
func() int {
return len(componentsList)
},
func() fyne.CanvasObject {
return widget.NewLabel("template")
},
func(i widget.ListItemID, o fyne.CanvasObject) {
o.(*widget.Label).SetText(componentsList[i]) // i need to update this when componentsList was updated
})
nameEntry := widget.NewEntry()
typeEntry := widget.NewEntry()
form := &widget.Form{
Items: []*widget.FormItem{
{Text: "Name", Widget: nameEntry},
{Text: "Type", Widget: typeEntry}},
OnSubmit: func() {
componentsList = append(componentsList, nameEntry.Text+": "+typeEntry.Text) // append an item to componentsList array
},
}
layout := container.New(layout.NewGridWrapLayout(fyne.NewSize(350, 500)), componentsTree, form)
window.SetContent(layout)
window.Resize(fyne.NewSize(800, 600))
window.ShowAndRun()
}
我在上面代码的注释中写了我需要做的事情:
- 当 componentsList 更新时,我需要更新它。
- 将项目附加到 componentsList 数组