import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:no_screenshot/constants.dart'; import 'package:no_screenshot/screenshot_snapshot.dart'; import 'no_screenshot_platform_interface.dart'; /// An implementation of [NoScreenshotPlatform] that uses method channels. class MethodChannelNoScreenshot extends NoScreenshotPlatform { /// The method channel used to interact with the native platform. @visibleForTesting final methodChannel = const MethodChannel(screenshotMethodChannel); @visibleForTesting final eventChannel = const EventChannel(screenshotEventChannel); Stream? _cachedStream; @override Stream get screenshotStream { _cachedStream ??= eventChannel.receiveBroadcastStream().map( (event) => ScreenshotSnapshot.fromMap(jsonDecode(event) as Map), ); return _cachedStream!; } @override Future toggleScreenshot() async { final result = await methodChannel.invokeMethod( toggleScreenShotConst, ); return result ?? false; } @override Future screenshotOff() async { final result = await methodChannel.invokeMethod(screenShotOffConst); return result ?? false; } @override Future screenshotOn() async { final result = await methodChannel.invokeMethod(screenShotOnConst); return result ?? false; } @override Future toggleScreenshotWithImage() async { final result = await methodChannel.invokeMethod(screenSetImage); return result ?? false; } @override Future toggleScreenshotWithBlur({double blurRadius = 30.0}) async { final result = await methodChannel.invokeMethod(screenSetBlur, { 'radius': blurRadius, }); return result ?? false; } @override Future toggleScreenshotWithColor({int color = 0xFF000000}) async { final result = await methodChannel.invokeMethod(screenSetColor, { 'color': color, }); return result ?? false; } @override Future screenshotWithImage() async { final result = await methodChannel.invokeMethod(screenEnableImage); return result ?? false; } @override Future screenshotWithBlur({double blurRadius = 30.0}) async { final result = await methodChannel.invokeMethod(screenEnableBlur, { 'radius': blurRadius, }); return result ?? false; } @override Future screenshotWithColor({int color = 0xFF000000}) async { final result = await methodChannel.invokeMethod(screenEnableColor, { 'color': color, }); return result ?? false; } @override Future startScreenshotListening() { return methodChannel.invokeMethod(startScreenshotListeningConst); } @override Future stopScreenshotListening() { return methodChannel.invokeMethod(stopScreenshotListeningConst); } @override Future startScreenRecordingListening() { return methodChannel.invokeMethod(startScreenRecordingListeningConst); } @override Future stopScreenRecordingListening() { return methodChannel.invokeMethod(stopScreenRecordingListeningConst); } }