Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Preface: Today just see, can [carry own original article] participate in “programmer essential small knowledge” activity, very excited. Remember before there are a few technical articles on a platform, I feel the nuggets technology atmosphere is stronger, I want to move over.

Create a go-flutter plugin for your desktop application — Zhihu.com

The body of the

Note that this tutorial does not describe how to create a plugin or publish a plugin on the website of Go-Flutter. Instead, it describes the code that can be used to invoke the GO terminal from the Dart terminal when a desktop application is written.

Github code for this tutorial

Files in android and ios directories are theoretically unnecessary.

Write the Go-Flutter plugin

Create a new go_plugin directory under the helloGoflutter project. Create a new Hello directory under the go_plugin directory.

Create the go language code file hello.go in the Hello directory.

hellogoflutter/go_plugin/hello.go

package hello import ( "github.com/go-flutter-desktop/go-flutter" "github.com/go-flutter-desktop/go-flutter/plugin" ) // Const (channelName = "bettersun.go-flutter. Plugin. Hello "hello = "bettersun.go-flutter "Hello") // declare the Plugin structure type HelloPlugin struct{} // specify the go-flutter Plugin var _ flutter.Plugin = &helloPlugin {} // initialize the Plugin func (HelloPlugin) InitPlugin(messenger plugin.BinaryMessenger) error { channel := plugin.NewMethodChannel(messenger, channelName, plugin.StandardMethodCodec{}) channel.HandleFunc(hello, Func helloFunc(arguments interface{}) (reply interface{}, err error) { return "hello go-flutter", nil }Copy the code

Run the go mod init command to create the go.mod file in the Hello directory.

go mod init hello
Copy the code

The go.mod file after the command is executed

hellogoflutter/go_plugin/go.mod

Module hello go 1.13 require github.com/go-flutter-desktop/go-flutter v0.42.0Copy the code

Execute the go mod Tidy command to handle dependencies

go mod tidy
Copy the code

Introduce a plugin to HelloFlutter/Go

Add intro to go.mod.

helloflutter/go/go.mod

Module hellogoflutter/go go 1.13 require (// non-real github repository, Rewritten below github.com/bettersun/go-flutter-plugin/hello v0.0.0 github.com/go-flutter-desktop/go-flutter v0.42.0 github.com/pkg/errors v0.9.1) // Replace github.com/bettersun/go-flutter-plugin/hello =>.. /go_plugin/helloCopy the code

Modify options. Go in the helloflutter/go/ CMD directory without modifying main.go.

helloflutter/go/cmd/options.go

Package main import (// go. Mod imported path "github.com/bettersun/go-flutter-plugin/hello" "github.com/go-flutter-desktop/go-flutter") var options = []flutter.Option{// Set window width and height Flutter. WindowInitialDimensions (800, 600), / / adding plugin flutter AddPlugin (hello.html HelloPlugin {}),}Copy the code

Write the Dart interface corresponding to the Go-Flutter plugin

Create the plugin directory under lib, create the go directory under Plugin, and create hello_plugin.dart in the go directory.

hellogoflutter/lib/plugin/go/hello_plugin.dart

import 'dart:async'; import 'package:flutter/services.dart'; Class HelloPlugin {// The package name of the go-flutter plugin, Static const _channel = const MethodChannel("bettersun.go-flutter.plugin.hello"); Static Future<String> hello() async => _channel.invokemethod ("hello"); }Copy the code

Create a confirmation screen

Create module in hellogoflutter/lib, create hello in module, and create hello_page.dart in hello. This screen calls the Dart interface corresponding to the Go-Flutter plugin.

hellogoflutter/lib/module/hello/hello_page.dart

import 'package:flutter/material.dart'; import 'package:hellogoflutter/plugin/go/plugin.dart'; class HelloPage extends StatefulWidget { @override _HelloPageState createState() => _HelloPageState(); } class _HelloPageState extends State<HelloPage> { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Hello'), ), body: Center( child: Column( children: <Widget>[ FutureBuilder<String>( future: HelloPlugin.hello(), builder: (c, snapshot) {if (! Snapshot.hasdata) {return Text('Hello error ');} return Text(snapshot.data);},)],); }}Copy the code

Modify the FAB click processing in Main. dart to jump to the image created above.

Dart FAB code:

import './module/hello/hello_page.dart';
Copy the code
floatingActionButton: FloatingActionButton( // onPressed: _incrementCounter, onPressed:() async { await Navigator.of(context).push( MaterialPageRoute(builder: (_) { return HelloPage(); })); }, tooltip: 'Increment', child: Icon(Icons.add), ),Copy the code

Execute hover Run to start the program

After the program starts, click FAB to jump to the confirmation screen.

The confirmation screen displays the return of Hello Go-flutter, the plugin’s specific handler.