shortcut_method_channel.dart 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import 'dart:async';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:flutter/services.dart';
  4. import 'shortcut_platform_interface.dart';
  5. /// An implementation of [FlutterShortcutPlatform] that uses method channels.
  6. class MethodChannelFlutterShortcut extends FlutterShortcutPlatform {
  7. /// The method channel used to interact with the native platform.
  8. ///
  9. ///
  10. ///
  11. MethodChannelFlutterShortcut() {
  12. methodChannel.setMethodCallHandler(_handleMethod);
  13. }
  14. @visibleForTesting
  15. final methodChannel = const MethodChannel('flutter_shortcut');
  16. final StreamController<String> _actionStreamController =
  17. StreamController<String>.broadcast();
  18. @override
  19. Future<String?> createShortcut({
  20. required String id,
  21. required String label,
  22. required String action,
  23. String? iconAssetName,
  24. }) async {
  25. return await methodChannel.invokeMethod("createShortcut", {
  26. "id": id,
  27. "shortLabel": label,
  28. "action": action,
  29. "icon": iconAssetName,
  30. });
  31. }
  32. Future<dynamic> _handleMethod(MethodCall call) async {
  33. debugPrint('MethodChannelFlutterShortcut _handleMethod ${call.method}');
  34. switch (call.method) {
  35. case "shortcutAction":
  36. _shortcutAction(call.arguments);
  37. break;
  38. }
  39. }
  40. void _shortcutAction(dynamic action) {
  41. if (action is String) {
  42. _actionStreamController.add(action);
  43. }
  44. }
  45. @override
  46. Stream<String> actionStream() {
  47. return _actionStreamController.stream;
  48. }
  49. @override
  50. Future<void> getLaunchAction(
  51. void Function(String action) onActionReceived) async {
  52. debugPrint('getLaunchAction 触发了');
  53. String? response = await methodChannel.invokeMethod("getLaunchAction");
  54. if (response != null) {
  55. onActionReceived(response);
  56. }
  57. }
  58. }