Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ group 'com.flutter.moum.screenshot_callback'
version '1.0'

buildscript {
ext.kotlin_version = '1.6.10'

ext.kotlin_version = '1.7.10'
repositories {
google()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:7.1.2'
classpath 'com.android.tools.build:gradle:7.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand Down
28 changes: 19 additions & 9 deletions lib/screenshot_callback.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ import 'dart:async';
import 'package:flutter/services.dart';

class ScreenshotCallback {
static const MethodChannel _channel =
const MethodChannel('flutter.moum/screenshot_callback');
static final ScreenshotCallback _singleton = ScreenshotCallback._internal();
static ScreenshotCallback get instance => _singleton;

/// Functions to execute when callback fired.
List<VoidCallback> onCallbacks = <VoidCallback>[];

ScreenshotCallback() {
ScreenshotCallback._internal() {
initialize();
}

static const MethodChannel _channel = const MethodChannel('flutter.moum/screenshot_callback');

/// Functions to execute when callback fired.
List<VoidCallback> onCallbacks = <VoidCallback>[];

/// Initializes screenshot callback plugin.
Future<void> initialize() async {
_channel.setMethodCallHandler(_handleMethod);
Expand All @@ -24,12 +26,20 @@ class ScreenshotCallback {
onCallbacks.add(callback);
}

/// Remove void callback.
void removeListener(VoidCallback callback) {
onCallbacks.remove(callback);
}

Future<dynamic> _handleMethod(MethodCall call) async {
switch (call.method) {
case 'onCallback':
for (final callback in onCallbacks) {
callback();
}
// for (final callback in onCallbacks) {
// callback();
// }
// exc last call
final callback = onCallbacks.lastOrNull;
callback?.call();
break;
default:
throw ('method not defined');
Expand Down