日更挑战-Flutter尝试插件开发


越不懂的越爱装
大家都同等:IT世界没有难不难,只有是否了解过

挑战目录

创建Flutter插件项目

同创建Flutter项目类似,但是选择项目类型的时候要选择Flutter plugin项目


创建flutter插件项目
  • 创建出来的项目主要目录有


    image.png
    1. android:实现插件安卓部分
    2. example:示例项目,将其他3个的实现用于该处
    3. ios:实现插件IOS部分
    4. lib:实现参加Flutter部分

android:实现插件安卓部分

  • //创建FlutterpluginPlugin类实现FlutterPlugin, MethodCallHandler接口
    class FlutterpluginPlugin : FlutterPlugin, MethodCallHandler {
        private lateinit var channel: MethodChannel
    
        override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
          //匹配上和Flutter端相同的MethodChannel
            channel = MethodChannel(flutterPluginBinding.binaryMessenger, "flutterplugin")
            channel.setMethodCallHandler(this)
        }
        //当Flutter端调用方法时
        override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
            if (call.method == "getPlatformVersion") {
                result.success("Android ${android.os.Build.VERSION.RELEASE}")
            } else {
                result.notImplemented()
            }
        }
        override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
            channel.setMethodCallHandler(null)
        }
    }
    

example:示例项目,将其他3个的实现用于该处

  • 导入
  • import 'package:flutterplugin/flutterplugin.dart';
    
  • 调用
  • Flutterplugin.platformVersion
    

ios:实现插件IOS部分

由于这边的电脑时Window电脑,这个暂时写不了


lib:实现参加Flutter部分

  • 构造MethodChannel对象

    static const MethodChannel _channel =const MethodChannel('channel_name');
    
  • 通过上面的MethodChannel对象调用插件提供的方法

     static Future get platformVersion async {
        final String? version = await _channel.invokeMethod('插件实现的方法名字符串');
        return version;
      }
    

事件监听- android:实现插件安卓部分

  • class FlutterpluginPlugin : FlutterPlugin, MethodCallHandler {
        private lateinit var eventChannel: EventChannel
        
        override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
          //匹配和Flutter端相同的EventChannel
            eventChannel = EventChannel(flutterPluginBinding.binaryMessenger, "flutter_event_channel")
            eventChannel.setStreamHandler(streamHandler)
        }
        override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
            if (call.method == "getPlatformVersion") {
                if (eventSink != null) {
                  //发送事件
                    eventSink!!.success("这是事件参数,一般使用Map。这里用字符串作示例")
                }
            }
            result.notImplemented()
        }
    
        override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
            channel.setMethodCallHandler(null)
        }
    
        /************ 事件派发到flutter层 *************/
        //eventSink用于发送事件:eventSink!!.success("这是事件参数,一般使用Map。这里用字符串作示例")
        private var eventSink: EventChannel.EventSink? = null
        private val streamHandler = object : EventChannel.StreamHandler {
            override fun onListen(p0: Any?, p1: EventChannel.EventSink?) {
                eventSink = p1
            }
            override fun onCancel(p0: Any?) {
                eventSink = null
            }
        }
    }
    
    

事件监听- example:示例项目,将其他3个的实现用于该处

  • 导入

  • import 'package:flutterplugin/flutterplugin.dart';
    
  • 触发lib部分构造方法的初始化

    Flutterplugin()
    
  • 调用

  • Flutterplugin.platformVersion
    

事件监听- ios:实现插件IOS部分

由于这边的电脑时Window电脑,这个暂时写不了


事件监听- lib:实现参加Flutter部分

class Flutterplugin {
  //监听注册 对象
  late StreamSubscription subscription;
  Flutterplugin() {
    //注册 监听事件另一端的success和error方法
    subscription = EventChannel("flutter_event_channel")
        .receiveBroadcastStream()
        .listen(handleEvent, onError: errorEvent);
  }
  //success的处理方法
  void handleEvent(dynamic params) {
    final String paramStr = params;
    print(paramStr);
  }
  //error的处理方法
  void errorEvent(dynamic params){}

  static const MethodChannel _channel = const MethodChannel('flutterplugin');

  static Future get platformVersion async {
    final String? version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }
}

你可能感兴趣的:(日更挑战-Flutter尝试插件开发)