Home > Mobile >  how to loop over fyne.CanvasObject in fyne in golang
how to loop over fyne.CanvasObject in fyne in golang

Time:05-19

the question is. i have NewVScroll in the NewVScroll i have NewVBoxLayout in NewVBoxLayout i have NewButton . the problem is when i press the NewButton it should loop over NewVScroll and then loop over NewVBoxLayout to find NewButton . but i don't know how to do it in fyne because i can't loop over var v1 fyne.CanvasObject

the error is : cannot range over v1 (variable of type fyne.CanvasObject)

this is the code

package main

import (
    "fmt"

    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/container"
    "fyne.io/fyne/v2/layout"
    "fyne.io/fyne/v2/widget"
)

var cEntries *fyne.Container

func FindObject(object fyne.CanvasObject, objects []fyne.CanvasObject) (int, bool) {
    for k1, v1 := range objects {
        for _, v2 := range v1 {// the problem is here. i can't loop over fyne.CanvasObject
            if v2 == object {
                return k1, true
            }
        }
    }
    return 0, false
}

func w1() *fyne.Container {
    wb := widget.NewButton("", nil)

    wb.OnTapped = func() {
        index, isin := FindObject(wb, cEntries.Objects)
        fmt.Println(index, isin)
    }

    return container.New(layout.NewVBoxLayout(), wb)
}

func main() {
    a := app.New()
    w := a.NewWindow("")

    wbAdd := widget.NewButton(" ", nil)
    cEntries = container.New(layout.NewVBoxLayout(), wbAdd, w1())

    wbAdd.OnTapped = func() {
        cEntries.Add(w1())
    }

    wsEntries := container.NewVScroll(cEntries)

    w.SetContent(wsEntries)
    w.ShowAndRun()
}

CodePudding user response:

The object is a container, so in general you could iterate v1.(*fyne.Container).Objects.

However it looks like you could avoid looping in your code because the button always refers to an index that is set when the button is added - so you could pass the current index (length) to w1 and then increment it.

CodePudding user response:

you don't need to loop over NewVBoxLayout because it is fyne.CanvasObject then if you want to know the index of the layout that contain this widget NewButton you can do it by just search for NewVBoxLayout in NewVScroll this is the code

package main

import (
    "fmt"

    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/container"
    "fyne.io/fyne/v2/layout"
    "fyne.io/fyne/v2/widget"
)

var cEntries *fyne.Container

func FindObject(object fyne.CanvasObject, objects []fyne.CanvasObject) (int, bool) {
    for k1, v1 := range objects {
        if v1 == object {
            return k1, true
        }
    }
    return 0, false
}

func w1() *fyne.Container {
    wb := widget.NewButton("", nil)
    c := container.New(layout.NewVBoxLayout(), wb)
    wb.OnTapped = func() {
        index, isin := FindObject(c, cEntries.Objects)
        fmt.Println(index, isin)
    }

    return c
}

func main() {
    a := app.New()
    w := a.NewWindow("")

    wbAdd := widget.NewButton(" ", nil)
    cEntries = container.New(layout.NewVBoxLayout(), wbAdd, w1())

    wbAdd.OnTapped = func() {
        cEntries.Add(w1())
    }

    wsEntries := container.NewVScroll(cEntries)

    w.SetContent(wsEntries)
    w.ShowAndRun()
}

see in this code you make the container that handle the button and then you search for the container that contain this button

  • Related