Write your first Glutter app

Make a Go module, write the counter app with a stateless and a stateful widget, run it as a window, and test it without a window.

4 min to read

Glutter is a GUI toolkit for Go with Flutter’s framework API. In this guide you write the app that flutter create makes, in Go: a page with an app bar, a number, and a button that adds one.

You can run the finished app in your browser first.

What you need#

  • Go 1.27.4 or newer. Run go version to check.
  • A desktop: Linux with Wayland or X11, Windows or macOS.

You need nothing else to build. Glutter has no cgo, so there is no C compiler and no library to install.

To run, a Linux desktop needs a graphics driver with Vulkan or with OpenGL ES 3.0, which desktops have. Glutter uses Vulkan where it works, and falls back to OpenGL ES by itself.

Make the module#

mkdir counter
cd counter
go mod init example.com/counter
go get github.com/rm4n0s/glutter@latest

Start the app#

Make main.go. An app imports one package, material, the way a Flutter app imports material.dart. By convention it is named m.

package main

import (
	"log"
	"strconv"

	m "github.com/rm4n0s/glutter/material"
)

func main() {
	err := m.RunApp(NewMyApp(), m.RunAppOpts{
		AppID:  "com.example.counter",
		Window: m.WindowConfig{Title: "Counter", Size: m.Size{Width: 480, Height: 640}},
	})
	if err != nil {
		log.Fatal(err)
	}
}

RunApp takes the root widget and the options of the app. It returns when the app ends, with an error if it could not start.

A widget without state#

The root of the app is a widget that keeps no state. It embeds m.StatelessWidget, which is Flutter’s superclass, and has a Build method.

// MyApp is the root of the app: a widget without state.
type MyApp struct{ m.StatelessWidget }

func NewMyApp() *MyApp { return &MyApp{} }

func (a *MyApp) Build(bc m.BuildContext) m.Widget {
	return m.NewMaterialApp(m.MaterialAppOpts{
		Title: "Counter",
		Theme: m.NewThemeData(m.ThemeDataOpts{ColorSchemeSeed: m.Some(m.Colors.Blue.Primary)}),
		Home:  NewCounterPage("Counter"),
	})
}

Two rules of the API show here:

  • A widget is made by a function NewX. The parameters that Flutter marks as required are arguments, and the rest are fields of an options struct, XOpts, with Flutter’s names.
  • A parameter that may be absent, and whose absence is not Go’s zero value, is m.Some(value). The seed color is one: no color is not the same as black.

A widget with state#

The page counts the taps, so it has state. A stateful widget is two types: the widget, which is immutable, and its state.

// CounterPage is a widget with state: the number of taps.
type CounterPage struct {
	m.StatefulWidget
	title string
}

func NewCounterPage(title string, opts ...m.WidgetOpts) *CounterPage {
	return &CounterPage{StatefulWidget: m.NewStatefulWidgetBase(opts...), title: title}
}

func (p *CounterPage) Title() string        { return p.title }
func (p *CounterPage) CreateState() m.State { return &counterPageState{} }

type counterPageState struct {
	m.StateOf[*CounterPage]
	count int
}

func (s *counterPageState) increment() { s.SetState(func() { s.count++ }) }

m.StateOf[*CounterPage] is Flutter’s State<CounterPage>. It gives the state s.Widget(), s.Context() and s.SetState(...). As in Flutter, you change the state inside SetState, and the framework builds the widget again.

The fields of a widget are read through methods, such as Title().

Build the page#

func (s *counterPageState) Build(bc m.BuildContext) m.Widget {
	return m.NewScaffold(m.ScaffoldOpts{
		AppBar: m.NewAppBar(m.AppBarOpts{Title: m.NewText(s.Widget().Title())}),
		Body: m.NewCenter(
			m.NewColumn([]m.Widget{
				m.NewText("You have pushed the button this many times:"),
				m.NewText(strconv.Itoa(s.count), m.TextOpts{Style: m.ThemeOf(bc).TextTheme.HeadlineMedium}),
			}, m.ColumnOpts{MainAxisAlignment: m.MainAxisAlignmentCenter}),
		),
		FloatingActionButton: m.NewFloatingActionButton(s.increment, m.NewIcon(m.Icons.Add()),
			m.FloatingActionButtonOpts{Tooltip: "Increment"}),
	})
}

The code reads like the Dart one: Scaffold, AppBar, Center, Column, FloatingActionButton. Flutter’s Theme.of(context).textTheme.headlineMedium is m.ThemeOf(bc).TextTheme.HeadlineMedium: a function for the static method, and exported fields for the properties.

The button’s callback is required in Flutter, so here it is the first argument: s.increment.

Run it#

go run .

A window opens. Press the button, and the number grows.

go run, go build and go install make a release build. For a debug build, with Flutter’s asserts and diagnostics, add a tag:

go run -tags debug_mode .

To build the program without cgo, which is all Glutter needs:

CGO_ENABLED=0 go build -o counter .

Test it without a window#

The package gluttertest runs a widget on a fake platform, as Flutter’s WidgetTester does. Make main_test.go:

package main

import (
	"testing"
	"time"

	"github.com/rm4n0s/glutter/gluttertest"
	m "github.com/rm4n0s/glutter/material"
)

func TestTapIncrements(t *testing.T) {
	ts := gluttertest.New(t, gluttertest.Opts{Size: m.Size{Width: 480, Height: 640}})
	ts.PumpWidget(NewMyApp())

	if n := ts.Count(gluttertest.ByText("0")); n != 1 {
		t.Fatalf("the text 0 is shown %d times, want 1", n)
	}

	ts.Tap(gluttertest.ByType[*m.FloatingActionButton]())
	ts.Pump(50 * time.Millisecond)

	if n := ts.Count(gluttertest.ByText("1")); n != 1 {
		t.Errorf("after one tap the text 1 is shown %d times, want 1", n)
	}
}

PumpWidget builds the app, Tap taps the widget that a finder finds, and Pump lets time pass. Run the test in both build modes:

go test ./...
go test -tags debug_mode ./...

Where to go next#