How to make a grid / Fyne / Golang

1.8k Views Asked by At

I'm making an application with Fyne.
I need to create a grid where the left column will be fixed and the right column will stretch. In general, there will be a menu on the left, and the main block on the right (a screenshot of the expected one below).
I read the documentation https://developer.fyne.io/container/grid but still don't understand how to do it. Help me please.
Grid

package main

import (
    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/app"
)

func main() {
    application := app.New()
    window := application.NewWindow("title")
    window.Resize(fyne.NewSize(1920, 1080))

    window.ShowAndRun()
}
2

There are 2 best solutions below

4
On BEST ANSWER

you can check the demo application of Fyne.

$ go get fyne.io/fyne/v2/cmd/fyne_demo
$ fyne_demo

from your description here is an example with menu on the left, and the main block on the right:

package main

import (
    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/container"
    "fyne.io/fyne/v2/widget"
)

func main() {
    a := app.New()

    w := a.NewWindow("Fyne Demo")

    w.SetMaster()

    content := container.NewMax()
    title := widget.NewLabel("Component name")
    intro := widget.NewLabel("An introduction would probably go\nhere, as well as a")
    intro.Wrapping = fyne.TextWrapWord

    tutorial := container.NewBorder(
        container.NewVBox(title, widget.NewSeparator(), intro), nil, nil, nil, content)

    split := container.NewHSplit(makeNav(), tutorial)

    split.Offset = 0
    w.SetContent(split)

    w.Resize(fyne.NewSize(640, 460))
    w.ShowAndRun()
}

func makeNav() fyne.CanvasObject {

    tree := widget.NewTreeWithStrings(menuItems)

    return container.NewBorder(nil, nil, nil, nil, tree)
}

var menuItems = map[string][]string{
    "":            {"welcome", "collections", "advanced"},
    "collections": {"list", "table"},
}

output :

screen shot op the app

if you explore their demo on your go path source you can see the full function of (makeNav) that will make things clickable.

and to make (the left column will be fixed and the right column will stretch):

split.Offset = 0
0
On

It sounds like you are making a Border layout (something attached to one edge). If so then you can set the left to be minimum size and the content stretch to fill by doing:

container.NewBorder(nil, nil, left, nil, content)

(the parameters are top, bottom, left, right, middle). If you want the user to control the split then do as suggested elsewhere and user container.NewHSplit(left, right).