有什么方法可以从Android活动中调用Flutter方法吗?

时间:2020-06-12 07:07:48

标签: android flutter flutter-method-channel

我正在尝试在Flutter中构建一个SDK,本地应用可以使用该SDK调用带参数的flutter方法。

我在data returned之后构建了Flutter模块。

我在this之后添加了Flutter屏幕

Android本机代码

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import io.flutter.embedding.android.FlutterActivity
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    /****************** Launching Flutter activity *************/
    button.setOnClickListener {
        val flutterEngine = FlutterActivity.withNewEngine()
        startActivity(
                flutterEngine
                .initialRoute("/")
                .build(this)
        )
    }

    login.setOnClickListener {
        // Want to do this
        //  channel?.invokeFlutterMethod("login", "username", "password");
    }
}

}

颤振代码

import 'package:flutter/material.dart';
import 'package:flutter/services.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 StatefulWidget {
     MyHomePage({Key key, this.title}) : super(key: key);

     final String title;

     @override
     _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
    static const channel = const 
    MethodChannel('sample.flutter.sdk/apis');

    @override
    void initState() {
         channel.setMethodCallHandler((call) {
            switch(call.method) {
              case "login": {
                   // Call Login API and return the result to 
                   //   android activity  
              }
            }
            return;
         });
        super.initState();
    }

    @override
    Widget build(BuildContext context) {
           return Scaffold(
                body: Center(
                  child: Column(
                     mainAxisAlignment: MainAxisAlignment.center,
                     children: <Widget>[
                          Text(
                              'Login',
                              style: 
                              Theme.of(context).textTheme.headline4,
                           ),
                     ],
                  ),
                ),
           );
     }
}

我无法在此处建立MethodChannel,FlutterActivity.withNewEngine没有dartExecutor

    MethodChannel(flutterEngine.dartExecutor.binaryMessenger, 
CHANNEL).setMethodCallHandler {
  call, result ->
  // Note: this method is invoked on the main thread.
  // TODO
}

还有其他方法可以创建MethodChannels吗?

0 个答案:

没有答案