flutter如何与原生项目通信
方法一:MethodChannel
在android/app/src/main
下找到对应的开发语言新建文件 《包名》MethodChannelDemo.kt
MethodChannelDemo.kt
代码如下: 在这个文件中写插件的基本内容。
com.flutter.guide.MethodChannel
为唯一的插件名称,做定位用
这里使用MethodChannel
进行通信
package com.flutter.guide
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
/**
* des:
*/
class MethodChannelDemo(messenger: BinaryMessenger): MethodChannel.MethodCallHandler {
private var channel: MethodChannel
init {
channel = MethodChannel(messenger, "com.flutter.guide.MethodChannel")
channel.setMethodCallHandler(this)
}
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
if (call.method == "sendData") {
val name = call.argument("name") as String?
val age = call.argument("age") as Int?
var map = mapOf("name" to "hello,$name",
"age" to "$age"
)
result.success(map)
}
}
}
package com.example.**
+ import com.flutter.guide.MethodChannelDemo // 这个就是我们插件名字 - 需要是独一无二的
import io.flutter.embedding.android.FlutterActivity // 这个
import io.flutter.embedding.engine.FlutterEngine
class MainActivity: FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
+ MethodChannelDemo(flutterEngine.dartExecutor.binaryMessenger)
}
}
在flutter
中如何使用
var channel = MethodChannel('com.flutter.guide.MethodChannel');
test() async {
var result = await channel.invokeMethod('sendData',{'name': 'laomeng', 'age': 18});
print(result);
}
完整代码如下:我们就可看到在控制台输出了。
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:zioncom_erp_flutter/const/config.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: APP_TITLE,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: APP_TITLE),
debugShowCheckedModeBanner: false,
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var channel = MethodChannel('com.flutter.guide.MethodChannel');
@override
Widget build(BuildContext context) {
test() async {
var result = await channel.invokeMethod('sendData',{'name': 'laomeng', 'age': 18});
print(result);
}
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'9999',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: test,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
如何导入本地jar
包进行使用
在android/app
下新建文件夹libs
,放入你本地的jar
包
在app/build.gradle
中引入这个依赖
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
+ implementation files('libs/DeviceManagerNew_v231010.jar')
}
在项目中正常导入使用即可。
import android.device.ScanManager;
var mScanManager = ScanManager();
mScanManager.switchOutputMode(0);
mScanManager.startDecode();
如果此时打开了android studio
,会发现导入的包找不到,这是由于android studio
不自动更新依赖的原因。这时就需要在implementation
依赖后 进行Sync project with gradle files
.