Rectangle
Demonstrates how to create and customize a Rectangle shape in Fyne using different colors and sizes.
Simple Rectangle
Section titled “Simple Rectangle”Show Code
package main
import ( "image/color"
"fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/canvas" "fyne.io/fyne/v2/container" "fyne.io/fyne/v2/widget")
func main() { myApp := app.New() myWindow := myApp.NewWindow("Rectangle")
// Create a rectangle with a red color rect := canvas.NewRectangle(color.RGBA{R: 255, G: 0, B: 0, A: 255}) rect.SetMinSize(fyne.NewSize(200, 100))
// Create centered text label := widget.NewLabel("Hello World") label.Alignment = fyne.TextAlignCenter
// Stack text on rectangle content := container.NewStack( rect, container.NewCenter(label), )
myWindow.SetContent(content) myWindow.Resize(fyne.NewSize(300, 200)) myWindow.ShowAndRun()}Multiple Rectangles
Section titled “Multiple Rectangles”Show Code
package main
import ( "image/color"
"fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/canvas" "fyne.io/fyne/v2/container" "fyne.io/fyne/v2/layout")
// a function to create the rectanglefunc makeRect(c color.Color) *canvas.Rectangle { r := canvas.NewRectangle(c) r.SetMinSize(fyne.NewSize(50, 100)) return r}
func main() { myApp := app.New() myWindow := myApp.NewWindow("Dashboard Layout")
// Top row: 3 rectangles topRow := container.NewGridWithColumns(3, makeRect(color.RGBA{R: 200, G: 0, B: 0, A: 255}), makeRect(color.RGBA{R: 0, G: 200, B: 0, A: 255}), makeRect(color.RGBA{R: 0, G: 0, B: 200, A: 255}), )
// Bottom row: 2 rectangles that fill remaining space bottomRow := container.NewGridWithColumns(2, makeRect(color.RGBA{R: 200, G: 200, B: 0, A: 255}), makeRect(color.RGBA{R: 200, G: 0, B: 200, A: 255}), )
// Layout: Top fixed height, bottom expands content := container.NewBorder( topRow, // top nil, // bottom nil, // left nil, // right container.New(layout.NewStackLayout(), bottomRow), // center (fills all space) )
myWindow.SetContent(content) myWindow.Resize(fyne.NewSize(800, 600)) myWindow.ShowAndRun()}