Anroid端总共四步:
第一步:建一个View页面,实现PlatformView,里面有一个getView方法,一定要有的哦
package com.yinuoapp.login; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import com.yinuoapp.smartsist.R; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.platform.PlatformView; public class PhoneLoginView implements PlatformView { private MethodChannel channel; private View nativeView; PhoneLoginView(Context context, MethodChannel channel){ this.channel=channel; nativeView= LayoutInflater.from(context).inflate(R.layout.activity_phone_login,null,false); } @Override public View getView() { return nativeView; } @Override public void dispose() { } }
第二步:建一个Factory
package com.yinuoapp.login; import android.content.Context; import io.flutter.plugin.common.MessageCodec; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.platform.PlatformView; import io.flutter.plugin.platform.PlatformViewFactory; public class PhoneLoginFactory extends PlatformViewFactory { private MethodChannel channel; private PhoneLoginView view; public PhoneLoginFactory(MessageCodec<Object> createArgsCodec,MethodChannel channel){ super(createArgsCodec); this.channel=channel; } @Override public PlatformView create(Context context, int viewId, Object args) { view=new PhoneLoginView(context,channel); return view; } }
第三步:建一个Plugin
package com.yinuoapp.login; import androidx.annotation.NonNull; import io.flutter.embedding.engine.plugins.FlutterPlugin; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.StandardMessageCodec; public class PhoneLoginPlugin implements FlutterPlugin, MethodChannel.MethodCallHandler { static String CHANNEL="com.yinuo/phoneLogin"; static String VIEW_CHANNEL="com.yinuo/view/phoneLogin"; private MethodChannel channel; private PhoneLoginFactory factory; @Override public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) { //创建通道 channel=new MethodChannel(binding.getBinaryMessenger(),CHANNEL); //注册通道的监听 channel.setMethodCallHandler(this); //创建platform的工厂对象 factory=new PhoneLoginFactory(StandardMessageCodec.INSTANCE,channel); //在flutter的引擎上注册loginphone的工厂对象 binding.getPlatformViewRegistry().registerViewFactory(VIEW_CHANNEL,factory); } @Override public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { channel.setMethodCallHandler(null); } @Override public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) { String name=call.method; if(name.equals("prelogin")){ } } }
第四步:MainActiviy注册
@Override public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) { super.configureFlutterEngine(flutterEngine); // Log.i(TAG,"打开设备时就进入 到了这里"); // if (Build.VERSION.SDK_INT >= 6.0) { // RequestPermissionUtil.request(this, new String[] { Manifest.permission.READ_PHONE_STATE, // Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE }); // } //蓝牙读身份证 blueCardChannel = new BlueCardChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), this, this); //软件安装//通用渠道 new CommonChannel((flutterEngine.getDartExecutor().getBinaryMessenger()),this,this); //通知栏调取 // new NotifierChannel(flutterEngine.getDartExecutor().getBinaryMessenger()); //视频sdk flutterEngine.getPlatformViewsController().getRegistry().registerViewFactory("com.yinuo.getvideo",new VideoViewFactory(flutterEngine.getDartExecutor().getBinaryMessenger())); //一键登录 // new PhoneLoginChannel((flutterEngine.getDartExecutor().getBinaryMessenger()),this,this); //一键登录 flutterEngine.getPlugins().add(new PhoneLoginPlugin()); // VideoViews videochannel=new VideoViews(flutterEngine.getDartExecutor().getBinaryMessenger(),this.getContext()); }
Flutter端两步:
第一步:AndoridView
第二步:MethodChannel