当前位置: 首页 > news >正文

鸿蒙harmonyos next flutter混合开发之开发plugin(获取操作系统版本号)

  • 创建Plugin为my_plugin
flutter create --org com.example --template=plugin --platforms=android,ios,ohos my_plugin
  • 创建Application为my_application
flutter create --org com.example my_application
  • flutter_application引用flutter_plugin,在pubspec.yaml文件中dependencies中添加flutter_plugin引用
  my_plugin:path: /Users/administrator/Desktop/workspace/my_plugin
  • 代码编写调用

my_plugin ohos代码编写:

import {FlutterPlugin,FlutterPluginBinding,MethodCall,MethodCallHandler,MethodChannel,MethodResult,
} from '@ohos/flutter_ohos';
import deviceInfo from '@ohos.deviceInfo'/** MyPlugin **/
export default class MyPlugin implements FlutterPlugin, MethodCallHandler {private channel: MethodChannel | null = null;constructor() {}getUniqueClassName(): string {return "MyPlugin"}onAttachedToEngine(binding: FlutterPluginBinding): void {this.channel = new MethodChannel(binding.getBinaryMessenger(), "my_plugin");this.channel.setMethodCallHandler(this)}onDetachedFromEngine(binding: FlutterPluginBinding): void {if (this.channel != null) {this.channel.setMethodCallHandler(null)}}onMethodCall(call: MethodCall, result: MethodResult): void {if (call.method == "getPlatformVersion") {result.success(deviceInfo.osFullName);} else {result.notImplemented()}}
}

my_plugin dart代码编写:


import 'my_plugin_platform_interface.dart';class MyPlugin {Future<String?> getPlatformVersion() {return MyPluginPlatform.instance.getPlatformVersion();}
}
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';import 'my_plugin_platform_interface.dart';/// An implementation of [MyPluginPlatform] that uses method channels.
class MethodChannelMyPlugin extends MyPluginPlatform {/// The method channel used to interact with the native platform.@visibleForTestingfinal methodChannel = const MethodChannel('my_plugin');@overrideFuture<String?> getPlatformVersion() async {final version = await methodChannel.invokeMethod<String>('getPlatformVersion');return version;}
}
import 'package:plugin_platform_interface/plugin_platform_interface.dart';import 'my_plugin_method_channel.dart';abstract class MyPluginPlatform extends PlatformInterface {/// Constructs a MyPluginPlatform.MyPluginPlatform() : super(token: _token);static final Object _token = Object();static MyPluginPlatform _instance = MethodChannelMyPlugin();/// The default instance of [MyPluginPlatform] to use.////// Defaults to [MethodChannelMyPlugin].static MyPluginPlatform get instance => _instance;/// Platform-specific implementations should set this with their own/// platform-specific class that extends [MyPluginPlatform] when/// they register themselves.static set instance(MyPluginPlatform instance) {PlatformInterface.verifyToken(instance, _token);_instance = instance;}Future<String?> getPlatformVersion() {throw UnimplementedError('platformVersion() has not been implemented.');}
}

my_application dart代码调用:

import 'package:flutter/material.dart';
import 'package:my_plugin/my_plugin.dart';void main() {runApp(const MyApp());
}class MyApp extends StatelessWidget {const MyApp({super.key});@overrideWidget build(BuildContext context) {return MaterialApp(title: 'Flutter Demo',theme: ThemeData(primarySwatch: Colors.blue,),home: const MyHomePage(title: 'my_application调用my_plugin'),);}
}class MyHomePage extends StatefulWidget {const MyHomePage({super.key, required this.title});final String title;@overrideState<MyHomePage> createState() => _MyHomePageState();
}class _MyHomePageState extends State<MyHomePage> {MyPlugin myPlugin = MyPlugin();String PlatformVersion = "";@overridevoid initState() {// TODO: implement initStatesuper.initState();myPlugin.getPlatformVersion().then((value){PlatformVersion = value ?? "";setState(() {});});}@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text(widget.title),),body: Center(child: Column(mainAxisAlignment: MainAxisAlignment.center,crossAxisAlignment: CrossAxisAlignment.center,children: <Widget>[Text('当前平台版本号为:$PlatformVersion',style: Theme.of(context).textTheme.headlineMedium,textAlign: TextAlign.center,),],),),);}
}
  • 效果展示


http://www.mrgr.cn/news/43680.html

相关文章:

  • 资产管理系统建设方案,资产盘点,rfid,出入库,消耗品管理,系统方案,系统源码(word原件)
  • C++——用类实现输入和输出时间。要求:(1)私有数据成员包括时、分、秒。(2)输入输出功能改为由成员函数实现。(3)在类体内定义成员函数。
  • 父类的final方法能不能够被子类重写?
  • ChatGPT的150个角色提示场景实测(16)瑜伽师
  • AI学习指南深度学习篇-学习率衰减在深度学习中的应用
  • 华为OD机试 - 几何平均值最大子数(Python/JS/C/C++ 2024 E卷 200分)
  • vue实现token的无感刷新
  • 振动分析-30-振动信号的幅值概率密度函数CWRU西楚大学轴承数据(实战)
  • rk3568 LTE(EC20 Android14)
  • C语言 | Leetcode C语言题解之第459题重复的子字符串
  • PMP--冲刺题--解题--11-20
  • Anaconda的安装与环境设置
  • 【AI自然语言处理应用】通过API调用通义晓蜜CCAI-对话分析AIO应用
  • 【分布式微服务云原生】深入剖析Redis缓存问题及其解决方案
  • Python网络编程:开启你的网络之旅
  • 基于Springboot+Android的的电子书阅读器系统的设计与实现(含源码+数据库)
  • C++ | Leetcode C++题解之第459题重复的子字符串
  • 设计模式、系统设计 record part03
  • stm32 使用 rt-thread 引脚PIN中断设置边沿触发问题
  • 付费计量系统数据元素(Data elements)