0

我正在使用名为 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()
}

我在上面代码的注释中写了我需要做的事情:

  1. 当 componentsList 更新时,我需要更新它。
  2. 将项目附加到 componentsList 数组
4

1 回答 1

0

更新列表呈现的数据后,您只需调用Refresh(). 因此,只需将您的OnSubmit功能更新为:

        OnSubmit: func() {
            componentsList = append(componentsList, nameEntry.Text+": "+typeEntry.Text)
            componentsTree.Refresh()
        },
于 2021-03-02T16:20:14.373 回答