Mobile Development
Flutter
Subjective
Oct 03, 2025
How to implement custom Flutter plugins?
Detailed Explanation
Custom Flutter plugin implementation:\n\n**Plugin Structure:**\n\nmy_plugin/\n lib/\n my_plugin.dart # Dart API\n android/\n src/main/kotlin/ # Android code\n ios/\n Classes/ # iOS code\n example/ # Example app\n\n\n**Dart Interface:**\n\nclass MyPlugin {\n static const MethodChannel _channel = MethodChannel('my_plugin');\n \n static Future getPlatformVersion() async {\n return await _channel.invokeMethod('getPlatformVersion');\n }\n \n static Future showToast(String message) async {\n await _channel.invokeMethod('showToast', {'message': message});\n }\n}\n\n\n**Android Implementation:**\nkotlin\nclass MyPlugin: FlutterPlugin, MethodCallHandler {\n override fun onMethodCall(call: MethodCall, result: Result) {\n when (call.method) {\n \"getPlatformVersion\" -> {\n result.success(\"Android ${Build.VERSION.RELEASE}\")\n }\n \"showToast\" -> {\n val message = call.argument(\"message\")\n Toast.makeText(context, message, Toast.LENGTH_SHORT).show()\n result.success(null)\n }\n }\n }\n}\n\n\n**iOS Implementation:**\nswift\npublic class SwiftMyPlugin: NSObject, FlutterPlugin {\n public static func register(with registrar: FlutterPluginRegistrar) {\n let channel = FlutterMethodChannel(name: \"my_plugin\", binaryMessenger: registrar.messenger())\n let instance = SwiftMyPlugin()\n registrar.addMethodCallDelegate(instance, channel: channel)\n }\n \n public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {\n switch call.method {\n case \"getPlatformVersion\":\n result(\"iOS \" + UIDevice.current.systemVersion)\n default:\n result(FlutterMethodNotImplemented)\n }\n }\n}\n
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts