Search code examples
gofyne

Button action for a specific list item in Fyne


I have a list in the GUI which has a simple string slice as its data source. And for every list item I create a button that should do something for that specific list item.

Here is some example code:

var data = []string{"folder1", "folder2"}

...

func someListCreationMethod(data []string) *widget.List {
    return widget.NewList(
        func() int {
            return len(data)
        },
        func() fyne.CanvasObject {
            return container.NewPadded(
                widget.NewLabel("Will be replaced"),
                widget.NewButton("Do Something", nil),
            )
        },
        func(id widget.ListItemID, item fyne.CanvasObject) {
            item.(*fyne.Container).Objects[1].(*widget.Label).SetText(data[id])
        },

    )
}

How can I wire up the button to the list item? I need a way to know which exact button was pressed or a way that the button knows on which list item it sits (or which list item is his parent).

Is there a way to do this?

Maybe widget.NewListWithData() is something that would address this issue but I'm unsure if this will help in this case.

Edit: Here is a more graphical example to show this (the code for this is slightly different but the principles are the same as with the code above): enter image description here

In this case I want to execute the pull for the one repo which the "pull" button belongs to.


Solution

  • You can set the button function by assigning Button.OnTapped