From 58e67a539710e1648018c5844fc448d3cbf43723 Mon Sep 17 00:00:00 2001 From: zhangyuanyuan <13520592875@163.com> Date: Fri, 9 Aug 2024 11:43:21 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9kotlin=5Fversion=E5=92=8C?= =?UTF-8?q?gradle=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- android/build.gradle | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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" } } From 3c7346123c3d71a3fa0724dbea009a245ef93466 Mon Sep 17 00:00:00 2001 From: zhangyuanyuan Date: Mon, 10 Nov 2025 17:57:44 +0800 Subject: [PATCH 2/2] exc last call and set singleton --- lib/screenshot_callback.dart | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) 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');