flutter android 开发笔记(四.根据路由进入不同的flutter页面)

如果一个页面有多个flutterView或者不同activity加载不同flutterView,需要设置不同标识作为flutter的路由表,不同路由标识加载不同的flutterview

关键代码:

//java代码
mFlutter2Engine = new FlutterEngine(this);
mFlutter2Engine.getNavigationChannel().setInitialRoute("route2");//设置加载的flutterView的路由标识
mFlutter1Engine = new FlutterEngine(this);
mFlutter1Engine.getNavigationChannel().setInitialRoute("route1");//设置加载的flutterView的路由标识

  

  

//flutter入口代码,通过路由标识,加载不同的布局
void main() { var defaultRouteName = window.defaultRouteName; if("route1" == defaultRouteName){ runApp(new MaterialApp( title: "route1", home: new Scaffold( appBar: new AppBar( title: new Text("route1"), ), body: new HomeApp(), ), )); } else if("route2" == defaultRouteName){ runApp(MyApp()); }else{ runApp(new Container( child: new Text("default"), )); } }

  

Flutter1Activity.java代码

package xyz.djytest.flutter_test_native_project;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import io.flutter.embedding.android.FlutterView;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.embedding.engine.renderer.FlutterUiDisplayListener;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class Flutter1Activity extends AppCompatActivity {
    FlutterEngine mFlutter1Engine;
    FlutterView   mFlutter1View;
    MethodChannel mFlutter1MethodChannel1;
    String        flutter1Name = "default is null";
    Button        mButton1,mButton2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 通过FlutterView引入Flutter编写的页面
        setContentView(R.layout.activity_flutter1);
        initFlutterEngine();
        mFlutter1View = createFlutterView();
        mButton1 = findViewById(R.id.button1);
        mButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mFlutter1MethodChannel1!=null){
                    Log.d("TAG","mButton1 java 调用 flutter");
                    mFlutter1MethodChannel1.invokeMethod("getFlutterName", flutter1Name, new MethodChannel.Result() {
                        @Override
                        public void success(Object result) {
                            flutter1Name = (String) result;
                            mButton1.setText(flutter1Name);
                        }

                        @Override
                        public void error(String errorCode, String errorMessage, Object errorDetails) {
                            Log.d("TAG","errorCode = "+ errorCode + ",errorMessage = "+errorMessage);
                        }

                        @Override
                        public void notImplemented() {
                            Log.d("TAG","flutter 端没有实现  getFlutterName");
                        }
                    });
                }
            }
        });
        mButton2 = findViewById(R.id.checkFlutterMap);
        mButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mFlutter1MethodChannel1!=null){
                    Log.d("TAG","mButton2 java 调用 flutter");
                    mFlutter1MethodChannel1.invokeMethod("checkFlutterMap", null, new MethodChannel.Result() {
                        @Override
                        public void success(Object result) {
                           String flutterMapStr  = (String) result;
                            Log.d("TAG","flutterMapStr = "+ flutterMapStr);
                        }

                        @Override
                        public void error(String errorCode, String errorMessage, Object errorDetails) {
                            Log.d("TAG","errorCode = "+ errorCode + ",errorMessage = "+errorMessage);
                        }

                        @Override
                        public void notImplemented() {
                            Log.d("TAG","flutter 端没有实现  checkFlutterMap");
                        }
                    });
                }
            }
        });
        // 关键代码,将Flutter页面显示到FlutterView中
        mFlutter1View.attachToFlutterEngine(mFlutter1Engine);
    }

    private FlutterView createFlutterView() {
            FlutterView flutterView = new FlutterView(this);
            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            FrameLayout flContainer = findViewById(R.id.fl_flutter);
            flContainer.addView(flutterView, lp);
            flContainer.setVisibility(View.INVISIBLE);
            FlutterUiDisplayListener listener = new FlutterUiDisplayListener() {
                @Override
                public void onFlutterUiDisplayed() {
                    flContainer.setVisibility(View.VISIBLE);
                }

                @Override
                public void onFlutterUiNoLongerDisplayed() {

                }
            };
            flutterView.addOnFirstFrameRenderedListener(listener);
            return flutterView;
    }

    private void initFlutterEngine() {
        mFlutter1Engine = FlutterEngineCache.getInstance().get("flutter1");
        if (mFlutter1Engine == null){
            mFlutter1Engine = new FlutterEngine(this);
            mFlutter1Engine.getNavigationChannel().setInitialRoute("route1");
            initChannel(mFlutter1Engine);
            mFlutter1Engine.getDartExecutor().executeDartEntrypoint(
                    DartExecutor.DartEntrypoint.createDefault()
            );
            FlutterEngineCache.getInstance().put("flutter1", mFlutter1Engine);
        }
    }

    private void initChannel(FlutterEngine flutterEngine) {
        mFlutter1MethodChannel1 = new MethodChannel(flutterEngine.getDartExecutor(), "flutter1/flutter2Java");
        mFlutter1MethodChannel1.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            @Override
            public void onMethodCall(MethodCall call, MethodChannel.Result result) {
                if (call == null || result == null){
                    if (result!=null){
                        result.error("-1","MethodCall is null",new Exception("MethodCall is null"));
                    }
                    return;
                }
                if ("getInt".equals(call.method)){
                    result.success(Integer.MAX_VALUE);
                }if ("getLong".equals(call.method)){
                    result.success(Long.MAX_VALUE);
                }else {
                    result.notImplemented();
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mFlutter1Engine.getLifecycleChannel().appIsResumed();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mFlutter1Engine.getLifecycleChannel().appIsInactive();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mFlutter1Engine.getLifecycleChannel().appIsPaused();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        FlutterEngineCache.getInstance().remove("flutter1");
        mFlutter1Engine.destroy();
        mFlutter1Engine = null;
        mFlutter1View = null;
        mFlutter1MethodChannel1 = null;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}

  

Flutter2Activity.java代码

package xyz.djytest.flutter_test_native_project;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;

import androidx.appcompat.app.AppCompatActivity;
import io.flutter.embedding.android.FlutterView;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.embedding.engine.renderer.FlutterUiDisplayListener;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.view.FlutterMain;

public class Flutter2Activity extends AppCompatActivity {
    FlutterEngine mFlutter2Engine;
    FlutterView   mFlutter2View;
    MethodChannel mFlutter2MethodChannel;
    Button        mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        FlutterMain.startInitialization(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flutter2);
        mButton = findViewById(R.id.button1);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mFlutter2MethodChannel != null) {
                    mFlutter2MethodChannel.invokeMethod("getFlutterMethod", null, new MethodChannel.Result() {
                        @Override
                        public void success(Object result) {
                            if (result != null) {
                                String str = result.toString();
                                mButton.setText(str);
                            }
                        }

                        @Override
                        public void error(String errorCode, String errorMessage, Object errorDetails) {

                        }

                        @Override
                        public void notImplemented() {

                        }
                    });
                }
            }
        });
        initFlutterEngine();
        mFlutter2View = createFlutterView();
        mFlutter2View.attachToFlutterEngine(mFlutter2Engine);
    }

    private FlutterView createFlutterView() {
        FlutterView flutterView = new FlutterView(this);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        FrameLayout flContainer = findViewById(R.id.fl_flutter);
        flContainer.addView(flutterView, lp);
        flContainer.setVisibility(View.INVISIBLE);
        FlutterUiDisplayListener listener = new FlutterUiDisplayListener() {
            @Override
            public void onFlutterUiDisplayed() {
                flContainer.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFlutterUiNoLongerDisplayed() {

            }
        };
        flutterView.addOnFirstFrameRenderedListener(listener);
        return flutterView;
    }

    private void initFlutterEngine() {
        mFlutter2Engine = FlutterEngineCache.getInstance().get("flutter2");
        if (mFlutter2Engine == null) {
            mFlutter2Engine = new FlutterEngine(this);
            mFlutter2Engine.getNavigationChannel().setInitialRoute("route2");
            initChannel(mFlutter2Engine);
            mFlutter2Engine.getDartExecutor().executeDartEntrypoint(
                    DartExecutor.DartEntrypoint.createDefault()
            );
            FlutterEngineCache.getInstance().put("flutter2", mFlutter2Engine);
        }
    }

    private void initChannel(FlutterEngine flutter2Engine) {
        mFlutter2MethodChannel = new MethodChannel(flutter2Engine.getDartExecutor(), "flutter2/flutter2Java");
        mFlutter2MethodChannel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            @Override
            public void onMethodCall(MethodCall call, MethodChannel.Result result) {
                if (call == null || result == null) {
                    if (result != null) {
                        result.error("-1", "MethodCall is null", new Exception("MethodCall is null"));
                    }
                    return;
                }
                if ("getJavaMethod".equals(call.method)) {
                    result.success("success ");
                } else {
                    result.success(" unKnow method");
                }
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        mFlutter2Engine.getLifecycleChannel().appIsResumed();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mFlutter2Engine.getLifecycleChannel().appIsInactive();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mFlutter2Engine.getLifecycleChannel().appIsPaused();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        FlutterEngineCache.getInstance().remove("flutter2");
        mFlutter2Engine.destroy();
        mFlutter2Engine = null;
        mFlutter2View = null;
        mFlutter2MethodChannel = null;
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}

  

posted @ 2020-06-12 17:46  凤雏小呆  阅读(1407)  评论(0编辑  收藏  举报