如何在Flutter Web项目中使用MethodChannel?

时间:2019-05-20 16:41:05

标签: javascript android flutter flutter-web

我有一个使用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
              },
            )
          ],
        ),
      ),
    );
  }
}

0 个答案:

没有答案