我有一个使用Flutter web webdev
构建的简单Web应用程序,并且想在本机应用程序的WebView上运行它,那么如何像jsbridge这样使用MethodChannel
来调用本机方法?
谢谢。
import 'package:flutter_web/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatelessWidget {
MyHomePage({Key key, this.title}) : super(key: key);
static const channel = const MethodChannel("some.channels"); <= this line
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Hello, World!',
),
FlatButton(
child: Text("Calc By NativeCode"),
onPressed: () {
channel.invokeMethod("calcAdd", [1, 2]); <= this line
},
)
],
),
),
);
}
}