flutter 代码仓库_go-flutter开发桌面应用(二) 创建go-flutter插件
需要先聲明下,本節教程不是go-flutter官網創建plugin和發布plugin的方式,是自己寫桌面應用時能實現Dart端調用Go端的代碼。
本教程github代碼
https://github.com/bettersun/hellogoflutter?github.comandroid和ios目錄下的文件理論上不需要。
編寫go-flutter插件
在hellogoflutter工程下新建go_plugin目錄,在go_plugin目錄下新建hello目錄,
在hello目錄中創建go語言代碼文件hello.go。
hellogoflutter/go_plugin/hello.go
package helloimport ("github.com/go-flutter-desktop/go-flutter""github.com/go-flutter-desktop/go-flutter/plugin" )// go-flutter插件需要聲明包名和函數名 // dart代碼中調用時需要指定相應的包名和函數名 const (channelName = "bettersun.go-flutter.plugin.hello"hello = "hello" )// 聲明插件結構體 type HelloPlugin struct{}// 指定為go-flutter插件 var _ flutter.Plugin = &HelloPlugin{}// 初始化插件 func (HelloPlugin) InitPlugin(messenger plugin.BinaryMessenger) error {channel := plugin.NewMethodChannel(messenger, channelName, plugin.StandardMethodCodec{})channel.HandleFunc(hello, helloFunc)return nil }// 插件中具體的執行函數 func helloFunc(arguments interface{}) (reply interface{}, err error) {return "hello go-flutter", nil }執行 go mod init 命令在hello目錄中創建 go.mod 文件。
go mod init hello執行命令后的go.mod文件
hellogoflutter/go_plugin/go.mod
module hellogo 1.13require github.com/go-flutter-desktop/go-flutter v0.42.0執行 go mod tidy命令處理依賴關系
go mod tidy在helloflutter/go中引入插件
在go.mod中添加引入。
helloflutter/go/go.mod
module hellogoflutter/gogo 1.13require (// 非真實存在的github倉庫,在下方被改寫github.com/bettersun/go-flutter-plugin/hello v0.0.0github.com/go-flutter-desktop/go-flutter v0.42.0github.com/pkg/errors v0.9.1 )// 使用本地目錄改寫上方的github倉庫 replace github.com/bettersun/go-flutter-plugin/hello => ../go_plugin/hello修改helloflutter/go/cmd目錄下的options.go,main.go無需修改。
helloflutter/go/cmd/options.go
package mainimport (// go.mod中引入的路徑"github.com/bettersun/go-flutter-plugin/hello""github.com/go-flutter-desktop/go-flutter" )var options = []flutter.Option{// 設置窗口寬高flutter.WindowInitialDimensions(800, 600),// 添加插件flutter.AddPlugin(hello.HelloPlugin{}), }編寫go-flutter插件對應的Dart接口
在lib下創建plugin目錄,在plugin目錄下創建go目錄,在go目錄中創建hello_plugin.dart.
hellogoflutter/lib/plugin/go/hello_plugin.dart
import 'dart:async';import 'package:flutter/services.dart';class HelloPlugin {// go-flutter插件中的包名,兩者必須一致static const _channel = const MethodChannel("bettersun.go-flutter.plugin.hello");// go-flutter插件中的函數名static Future<String> hello() async => _channel.invokeMethod("hello"); }創建一個確認用的畫面
在hellogoflutter/lib目錄下創建module,在module目錄下創建hello目錄,在hello目錄下創建hello_page.dart。該畫面調用go-flutter插件對應的Dart接口。
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> {@overrideWidget 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插件執行出錯');}return Text(snapshot.data);},)],)),);} }修改main.dart中FAB的點擊處理,點擊FAB跳轉到上面創建的畫面。
main.dart FAB 部分代碼:
import './module/hello/hello_page.dart'; floatingActionButton: FloatingActionButton(// onPressed: _incrementCounter,onPressed:() async {await Navigator.of(context).push(MaterialPageRoute(builder: (_) {return HelloPage();}),);},tooltip: 'Increment',child: Icon(Icons.add),),執行hover run啟動程序
程序啟動后,點擊FAB,會跳轉到確認畫面。
確認畫面會顯示 hello go-flutter,即插件的具體處理函數的返回結果。
總結
以上是生活随笔為你收集整理的flutter 代码仓库_go-flutter开发桌面应用(二) 创建go-flutter插件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: adb 连接某个wifi_一加7 Pro
- 下一篇: 列表对象转数组 微信小程序_微信小程序—