Reputation: 815
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):
In this case I want to execute the pull for the one repo which the "pull" button belongs to.
Upvotes: 2
Views: 1448
Reputation: 1
I am crazy enough to try to learn Go and Fyne both at the same time, so there may be a better solution. I found this code worked to click on a playing card in a displayed set, and return which one was tapped. The button background is transparent:
gridHuman := container.NewGridWithColumns(11)
var cardHuman [10]fyne.Widget
var cardBoxHuman [10]*fyne.Container
for idx, item := range human.hand {
pic := canvas.NewImageFromResource(cards.cardArt[item])
pic.SetMinSize(fyne.NewSize(60, 90))
cardHuman[idx] = widget.NewButton("", func() { testme(idx) })
cardBoxHuman[idx] = container.NewStack(pic, cardHuman[idx])
gridHuman.Add(cardBoxHuman[idx])
}
func testme(idx int) {
fmt.Println(idx)
}
Upvotes: 0
Reputation: 815
Credit goes to @andy.xyz as he pointed me in the right direction.
I just want to provide some example code if people are searching for a solution.
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[0].(*widget.Label).SetText(data[id])
// new part
item.(*fyne.Container).Objects[1].(*widget.Button).OnTapped = func() {
fmt.Println("I am button " + data[id])
}
},
)
}
Upvotes: 2