I am making a simple application with fyne, but I have a problem with text placement.
As you can see from the image , I managed to position the form components with an edge from the window, but I can't do the same with the underlying components (the Move method doesn't work).
Here the main file:
package main
import (
"fmt"
"weather-app/ui"
"weather-app/utils"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
)
func main() {
prova, err := utils.MakeRequest("palermo", "it")
if err != nil {
panic(err)
} else {
fmt.Println(prova.Weather[0])
fmt.Println(prova.Main)
fmt.Println(prova.Sys)
fmt.Println(prova.Dt)
}
app := app.New()
window := app.NewWindow("Current Weather")
customForm := ui.GenerateCustomForm()
apiResults := ui.ShowApiResults("Cinisi", "20 °C")
wrapper := container.NewGridWithRows(
2,
customForm,
apiResults,
)
window.Resize(fyne.NewSize(445, 400))
window.SetContent(wrapper)
window.ShowAndRun()
}
The ui:
package ui
import (
"fmt"
"image/color"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
)
func onSubmitFunc(city, country string) {
fmt.Println("submit")
}
func GenerateCustomForm() *fyne.Container {
//creating widgets
cityEntry := widget.NewEntry()
countrEntry := widget.NewEntry()
button := widget.NewButton("Search", func() { onSubmitFunc(cityEntry.Text, countrEntry.Text) })
//set placeholder in text input
cityEntry.SetPlaceHolder("City")
countrEntry.SetPlaceHolder("Country (optional)")
//setting size
cityEntry.Resize(fyne.NewSize(250, 40))
countrEntry.Resize(fyne.NewSize(150, 40))
button.Resize(fyne.NewSize(160, 40))
//positioning
cityEntry.Move(fyne.NewPos(10, 10))
countrEntry.Move(fyne.NewPos(cityEntry.Size().Width+25, 10))
button.Move(fyne.NewPos(10, 25))
customForm := container.New(
layout.NewVBoxLayout(),
container.NewWithoutLayout(
cityEntry,
countrEntry,
),
container.NewWithoutLayout(
button,
),
)
return customForm
}
func ShowApiResults(city, temp string) *fyne.Container {
text := canvas.NewText("Current weather in "+city, color.White)
text.TextSize = 18
temperature := canvas.NewText(temp, color.White)
temperature.TextSize = 50
//sample image
r, _ := fyne.LoadResourceFromURLString("http://openweathermap.org/img/wn/09d@2x.png")
image := canvas.NewImageFromResource(r)
image.FillMode = canvas.ImageFillContain
image.Resize(fyne.NewSize(100, 100))
//wrap the components to have them on the same line
tempAndImg := container.New(
layout.NewGridLayout(2),
temperature,
image,
)
tempAndImg.Move(fyne.NewPos(20, 0)) //DOESN'T WORK
apiResults := container.New(
layout.NewVBoxLayout(),
text,
tempAndImg,
)
//i tried also this:
//apiResults.Move(fyne.NewPos(20, 0)) --- but nothing
return apiResults
}
I am still in the basics of fyne, I thank in advance anyone who can help me out.
Move method should work with container.NewWithoutLayout
tempAndImg:= container.New(layout.NewGridLayout(2), container.NewWithoutLayout(temperature),container.NewWithoutLayout(image) ) temperature.Move(40, 0) image.Move(40,0) apiResults:= container.New(layout.NewVBoxLayout(),container.NewWithoutLayout(text),tempAndImg) text.Move(40, 0)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.