import 'dart:async'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'shortcut_platform_interface.dart'; /// An implementation of [FlutterShortcutPlatform] that uses method channels. class MethodChannelFlutterShortcut extends FlutterShortcutPlatform { /// The method channel used to interact with the native platform. /// /// /// MethodChannelFlutterShortcut() { methodChannel.setMethodCallHandler(_handleMethod); } @visibleForTesting final methodChannel = const MethodChannel('flutter_shortcut'); final StreamController _actionStreamController = StreamController.broadcast(); @override Future createShortcut({ required String id, required String label, required String action, String? iconAssetName, }) async { return await methodChannel.invokeMethod("createShortcut", { "id": id, "shortLabel": label, "action": action, "icon": iconAssetName, }); } Future _handleMethod(MethodCall call) async { debugPrint('MethodChannelFlutterShortcut _handleMethod ${call.method}'); switch (call.method) { case "shortcutAction": _shortcutAction(call.arguments); break; } } void _shortcutAction(dynamic action) { if (action is String) { _actionStreamController.add(action); } } @override Stream actionStream() { return _actionStreamController.stream; } @override Future getLaunchAction( void Function(String action) onActionReceived) async { debugPrint('getLaunchAction 触发了'); String? response = await methodChannel.invokeMethod("getLaunchAction"); if (response != null) { onActionReceived(response); } } }