Skip to content

Tips and tricks

Pierre Champion | Drakirus edited this page Apr 1, 2020 · 18 revisions

Collection of tips and tricks found by go-flutter users

As more and more peoples use flutter on the desktop, a multitude of small features are requested. It's not very clear whether, or not some of those features should be incorporated into the go-flutter source code as a reminder of a list was created to keep track of those requests and potential solutions.
This list may be converted to go-flutter Option or plugins.

1. Windows console

On the Windows OS, the console keep showing and disappearing when using Dart Process?

Place the following hide-cmd_windows.go file into the go/cmd/ directory:

package main

// the file hide-cmd_windows.go is used to hide console windows that keep
// showing up on each Dart Process.run calls

import "github.com/gonutz/w32"

func init() {
	console := w32.GetConsoleWindow()
	if console != 0 {
		_, consoleProcID := w32.GetWindowThreadProcessId(console)
		if w32.GetCurrentProcessId() == consoleProcID {
			w32.ShowWindowAsync(console, w32.SW_HIDE)
		}
	}
}

Found by @Tokenyet.

2. Set the initial window dimension based on screen resolution

Place the following window-size-based-on-resolution.go file into the go/cmd/ directory:

package main

// the file window-size-based-on-resolution.go is used to set the initial
// dimension of the window based on screen resolution.
//
// The following example sets the window to take all of the monitor screen
// minus a border.

import (
	flutter "github.com/go-flutter-desktop/go-flutter"
	"github.com/go-gl/glfw/v3.2/glfw"
)

const windowBorder = 100

func init() {
	// Notice: Code in init() delays first frame!

	// Not best practice, you should let go-flutter make this call.
	err := glfw.Init()
	if err != nil {
		panic(err)
	}

	vidMoce := glfw.GetPrimaryMonitor().GetVideoMode()

	options = append(options,
		flutter.WindowInitialDimensions(
			vidMoce.Width-windowBorder*2,
			vidMoce.Height-windowBorder*2,
		))
	options = append(options,
		flutter.WindowInitialLocation(windowBorder, windowBorder),
	)
}

By @pchampio, #267.

3. Open the Virtual keyboard on text edit:

Place the following virtual_keyboard_linux.go file into the go/cmd/ directory:

package main

// the file virtual_keyboard_linux.go is used to show the 'onboard'
// vitrual keyboard upon text input.
// LINUX only, variants (i.e.: *_windows.go) needs to be created.

import (
	"os/exec"
	flutter "github.com/go-flutter-desktop/go-flutter"
)

var onBoard = exec.Command("onboard")

func init() {
	options = append(options,
		flutter.VirtualKeyboardShow(func(){
			err := onBoard.Start()
			if err != nil {
				return
			}
		}),
	)
}

By @pchampio, #274.

4. Save the windows mode, fullscreen or default

Source: https://github.com/ekasetiawans/go-flutter-json-config

Place the following window_mode_plugin.go file into the go/cmd/ directory:

package main

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"os"

	"github.com/go-flutter-desktop/go-flutter"
	"github.com/go-flutter-desktop/go-flutter/plugin"
	"github.com/go-gl/glfw/v3.3/glfw"
)

const channelName = "com.github.ekasetiawans/window_plugin"

type windowPlugin struct{}

var _ flutter.Plugin = &windowPlugin{} 
var _ flutter.PluginGLFW = &windowPlugin{} 

func (p *windowPlugin) InitPlugin(messenger plugin.BinaryMessenger) error {
	channel := plugin.NewMethodChannel(messenger, channelName, plugin.StandardMethodCodec{})

	channel.HandleFunc("setFullScreen", handleSetFullScreen)
	channel.HandleFunc("getFullScreen", handleGetFullScreen)

	return nil
}

func handleGetFullScreen(arguments interface{}) (reply interface{}, err error) {
	config := getConfiguration()
	return config.FullScreenMode, nil
}

func handleSetFullScreen(arguments interface{}) (reply interface{}, err error) {
	value := arguments.(bool)
	config := getConfiguration()
	config.FullScreenMode = value
	config.saveConfig()

	return true, nil
}

// InitPluginGLFW .
func (p *windowPlugin) InitPluginGLFW(window *glfw.Window) error {
	return nil
}

type jsonConfiguration struct {
	FullScreenMode bool    `json:"fullscreen"`
}

func getConfiguration() *jsonConfiguration {
	filename := "config.json"
	if _, err := os.Stat(filename); err == nil {
		b, err := ioutil.ReadFile(filename)
		if err == nil {
			var config jsonConfiguration
			json.Unmarshal(b, &config)
			return &config
		}
	}

	return &jsonConfiguration{
		FullScreenMode: false,
	}
}

func (config *jsonConfiguration) saveConfig() {
	b, err := json.Marshal(config)
	if err != nil {
		log.Println(err)
		return
	}

	filename := "config.json"
	ioutil.WriteFile(filename, b, 0644)
}


func getWindowModeFromJSON() flutter.Option {
	config := getConfiguration()
	if config.FullScreenMode {
		return flutter.WindowMode(flutter.WindowModeBorderlessFullscreen)
	}

	return flutter.WindowMode(flutter.WindowModeDefault)
}

// then add the following in `go/cmd/option.go` 
var options = []flutter.Option{
	flutter.WindowInitialDimensions(800, 1280),
	
	flutter.AddPlugin(&windowPlugin{}),
	getWindowModeFromJSON(),
}

Matching dart code:

import 'package:flutter/services.dart';

class WindowPlugin {
  static MethodChannel channel =
      MethodChannel("com.github.ekasetiawans/window_plugin");

  static setFullScreen(bool value) async {
    await channel.invokeMethod("setFullScreen", value);
  }

  static Future<bool> isFullScreen() async {
    return await channel.invokeMethod("getFullScreen");
  }
}

By @ekasetiawans #273