diff --git a/android/build.gradle b/android/build.gradle index 2cf230b..f0828bb 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -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" } } diff --git a/lib/screenshot_callback.dart b/lib/screenshot_callback.dart index 77e3ecb..f79f1f5 100644 --- a/lib/screenshot_callback.dart +++ b/lib/screenshot_callback.dart @@ -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 onCallbacks = []; - - ScreenshotCallback() { + ScreenshotCallback._internal() { initialize(); } + static const MethodChannel _channel = const MethodChannel('flutter.moum/screenshot_callback'); + + /// Functions to execute when callback fired. + List onCallbacks = []; + /// Initializes screenshot callback plugin. Future initialize() async { _channel.setMethodCallHandler(_handleMethod); @@ -24,12 +26,20 @@ class ScreenshotCallback { onCallbacks.add(callback); } + /// Remove void callback. + void removeListener(VoidCallback callback) { + onCallbacks.remove(callback); + } + Future _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');