android_View_2">一. 内嵌android 原生View
-
在android 工程的包名下,也可在MainActivity创建 android 原生view ,继承PlatformView
// 1.自定义textview
public static class MyTextView implements PlatformView{private TextView textView ;/**** @param context* @param messenger 用于消息传递* @param id 生成时会分配一个唯一 ID* @param maps Flutter 传递的初始化参数。*/public MyTextView(Context context ,BinaryMessenger messenger,int id,Map<String,Object> maps) {textView = new TextView(context);textView.setText("我是来自android原生的textView");if (maps != null){String text = maps.get("text").toString();textView.setText(text);}textView.setTextColor(Color.RED);}// 返回要嵌入 Flutter 层次结构的Android View@Nullable@Overridepublic View getView() {return textView;}//释放此View时调用,此方法调用后 View 不可用,此方法需要清除所有对象引用,否则会造成内存泄漏。@Overridepublic void dispose() {}
}
-
新建MyViewFactory.java注册PlatformView
public class MyViewFactory extends PlatformViewFactory {private final BinaryMessenger messenger;public MyViewFactory(BinaryMessenger messenger) {super(StandardMessageCodec.INSTANCE);this.messenger = messenger;}@SuppressWarnings("unchecked")@Overridepublic PlatformView create(Context context, int id, Object args) {Map<String, Object> params = (Map<String, Object>) args;return new MainActivity.MyTextView(context, messenger, id, params);}}
- 创建plugin
public class MyViewFlutterPlugin implements FlutterPlugin {@Overridepublic void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {BinaryMessenger binaryMessenger = binding.getBinaryMessenger();binding.getPlatformViewRegistry().registerViewFactory("plugins.nightfarmer.top/myview",new MyViewFactory(binaryMessenger));}@Overridepublic void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {}
}
- 在MainActivy 中注册
@Overridepublic void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {super.configureFlutterEngine(flutterEngine);flutterEngine.getPlugins().add(new MyViewFlutterPlugin());}
- 在flutter 中使用
Container(width: 100,height: 100,child: AndroidView(viewType: "plugins.nightfarmer.top/myview",creationParams: {'text':'Flutter传给AndroidTextView的参数'},creationParamsCodec: StandardMessageCodec(),),),
android__flutter__109">二、android 与 flutter 相互跳转
- flutter 挑战到 原生Activity
var platform = MethodChannel('com.example.flutter/native_channel');void goToNativeActivity() {platform.invokeMethod('goToNative');}
在MainActivty 中注册通道跳转到TestActivity
private static final String CHANNEL = "com.example.flutter/native_channel";@Overridepublic void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {super.configureFlutterEngine(flutterEngine);flutterEngine.getPlugins().add(new MyViewFlutterPlugin());//注册通道new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler((call, result) -> {// 接收来自Flutter的方法调用if (call.method.equals("goToNative")) {Intent intent = new Intent(MainActivity.this, TestActivity.class);startActivity(intent);result.success(true);} else {result.notImplemented();}});}
- Activity 中跳转Flutter页面
在Manifest.xml 中注册
<activityandroid:name="io.flutter.embedding.android.FlutterActivity"android:theme="@style/LaunchTheme"android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"android:hardwareAccelerated="true"android:windowSoftInputMode="adjustResize"/>
根据路由跳转到Flutter 页面
startActivity(FlutterActivity.withNewEngine().initialRoute("/my_route")
// .withCachedEngine("engine_id").build(currentActivity));
跳转会与 2s的黑屏情况,在MyApp中
public class MyApp extends Application {private FlutterEngine fe ;@Overridepublic void onCreate() {super.onCreate();// 解决原生挑战到 flutter 会有2s 黑屏 的问题fe = new FlutterEngine(this);fe.getDartExecutor().executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault());FlutterEngineCache.getInstance().put("engine_id",fe);}//当application 销毁的时候调用@Overridepublic void onTerminate() {//销毁flutter引擎fe.destroy();super.onTerminate();}
}
在清单文件中注册 MyApp
<applicationandroid:name="com.qxx.ai_score_manager_flutter.MyApp">