日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

flutter 代码仓库_go-flutter开发桌面应用(二) 创建go-flutter插件

發布時間:2025/3/20 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 flutter 代码仓库_go-flutter开发桌面应用(二) 创建go-flutter插件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

需要先聲明下,本節教程不是go-flutter官網創建plugin和發布plugin的方式,是自己寫桌面應用時能實現Dart端調用Go端的代碼。


本教程github代碼

https://github.com/bettersun/hellogoflutter?github.com

android和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插件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。