import 'dart:async'; import 'dart:convert'; import 'package:flutter/services.dart'; const kActivityResultOk = -1; const kActivityResultCanceled = 0; class Intent { final bool isNull; final String? fromPackageName; final List? fromSignatures; final String? action; final String? data; final List? categories; final Map? extra; bool get isNotNull => !isNull; const Intent({ this.isNull = true, this.fromPackageName, this.fromSignatures, this.action, this.data, this.categories, this.extra, }); factory Intent.fromMap(Map? map) => Intent( isNull: map == null, fromPackageName: map?["fromPackageName"], fromSignatures: map?["fromSignatures"] != null ? List.unmodifiable( (map!["fromSignatures"] as List).map((e) => e.toString())) : null, action: map?["action"], data: map?["data"], categories: map?["categories"] != null ? List.unmodifiable( (map!["categories"] as List).map((e) => e.toString())) : null, extra: map?["extra"] != null ? (json.decode(map!["extra"]) as Map) .map((key, value) => MapEntry(key.toString(), value)) : null, ); Map toMap() => { "fromPackageName": fromPackageName, "fromSignatures": fromSignatures, "action": action, "data": data, "categories": categories, "extra": extra, }; @override String toString() { if (isNull) return 'Intent(null)'; var str = 'Intent${toMap()}'; return str.replaceFirst('{', '(').replaceFirst('}', ')', str.length - 1); } } class ReceiveIntent { static const MethodChannel _methodChannel = MethodChannel('receive_intent'); static const EventChannel _eventChannel = EventChannel("receive_intent/event"); static Future getInitialIntent() async { final renameMap = await _methodChannel.invokeMapMethod('getInitialIntent'); //print("result: $renameMap"); return Intent.fromMap(renameMap); } static Stream receivedIntentStream = _eventChannel .receiveBroadcastStream() .map((event) => Intent.fromMap(event as Map?)); static Future setResult(int resultCode, {Map? data, bool shouldFinish = false}) async { await _methodChannel.invokeMethod('setResult', { "resultCode": resultCode, if (data != null) "data": json.encode(data), "shouldFinish": shouldFinish, }); } }