Skip to content

Commit 852e56f

Browse files
committed
update
1 parent fd89814 commit 852e56f

7 files changed

Lines changed: 72 additions & 15 deletions

example/ios/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ SPEC CHECKSUMS:
2525

2626
PODFILE CHECKSUM: 819463e6a0290f5a72f145ba7cde16e8b6ef0796
2727

28-
COCOAPODS: 1.13.0
28+
COCOAPODS: 1.15.2

example/lib/main.dart

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ class _Home extends StatefulWidget {
2828
}
2929

3030
class _HomeState extends State<_Home> {
31+
bool hasScreenDetected = false;
32+
@override
33+
void initState() {
34+
// TODO: implement initState
35+
super.initState();
36+
ScreenshotDetector.instance.startListening(() {});
37+
}
38+
39+
@override
40+
void dispose() {
41+
ScreenshotDetector.instance.dispose();
42+
super.dispose();
43+
}
44+
3145
@override
3246
Widget build(BuildContext context) {
3347
return Scaffold(
@@ -39,15 +53,37 @@ class _HomeState extends State<_Home> {
3953
crossAxisAlignment: CrossAxisAlignment.center,
4054
mainAxisAlignment: MainAxisAlignment.center,
4155
children: [
42-
ScreenShotDetectorWrapper(
43-
onScreenshot: const Text('Screenshot Detected'),
44-
child: TextButton(
45-
onPressed: () => {},
46-
child: const Text(
47-
'Share',
48-
),
49-
),
50-
)
56+
if (hasScreenDetected) const Text('Screenshot Detected'),
57+
TextButton(
58+
onPressed: () => Navigator.of(context).push(MaterialPageRoute(builder: (context) => const Page1())),
59+
child: const Text('push'))
60+
],
61+
),
62+
),
63+
);
64+
}
65+
}
66+
67+
class Page1 extends StatefulWidget {
68+
const Page1({super.key});
69+
70+
@override
71+
State<Page1> createState() => _Page1State();
72+
}
73+
74+
class _Page1State extends State<Page1> {
75+
@override
76+
Widget build(BuildContext context) {
77+
return Scaffold(
78+
appBar: AppBar(
79+
title: const Text('Plugin example app'),
80+
),
81+
body: const Center(
82+
child: Column(
83+
crossAxisAlignment: CrossAxisAlignment.center,
84+
mainAxisAlignment: MainAxisAlignment.center,
85+
children: [
86+
Text('Page 1'),
5187
],
5288
),
5389
),

example/pubspec.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ packages:
192192
path: ".."
193193
relative: true
194194
source: path
195-
version: "0.0.1"
195+
version: "1.0.0"
196196
sky_engine:
197197
dependency: transitive
198198
description: flutter
@@ -280,4 +280,4 @@ packages:
280280
version: "3.0.3"
281281
sdks:
282282
dart: ">=3.3.1 <4.0.0"
283-
flutter: ">=3.3.0"
283+
flutter: ">=3.19.3"

ios/Classes/ScreenshotDetectorPlugin.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ public class ScreenshotDetectorPlugin: NSObject, FlutterPlugin, FlutterStreamHan
88
let instance = ScreenshotDetectorPlugin()
99
let eventChannel = FlutterEventChannel(name: "screenshot_detector", binaryMessenger: registrar.messenger())
1010
eventChannel.setStreamHandler(instance)
11+
12+
let methodChannel = FlutterMethodChannel(name: "screenshot_detector_method", binaryMessenger: registrar.messenger())
13+
registrar.addMethodCallDelegate(instance, channel: methodChannel)
14+
}
15+
16+
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
17+
if call.method == "cancelStream" {
18+
onCancel(withArguments: nil)
19+
result(nil)
20+
} else {
21+
result(FlutterMethodNotImplemented)
22+
}
1123
}
1224

1325
public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {

lib/screenshot_detector.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ScreenshotDetector {
1616
onScreenshotController.add(true);
1717
});
1818

19-
void disposeStream() {
19+
void dispose() {
2020
onScreenshotController.close();
2121
ScreenshotDetectorPlatform.instance.dispose();
2222
}
@@ -41,6 +41,12 @@ class _ScreenShotDetectorWrapperState extends State<ScreenShotDetectorWrapper> {
4141
screenshotDetector._startListeningStream();
4242
}
4343

44+
@override
45+
void dispose() {
46+
screenshotDetector.dispose();
47+
super.dispose();
48+
}
49+
4450
@override
4551
Widget build(BuildContext context) {
4652
return StreamBuilder<bool>(

lib/screenshot_detector_method_channel.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class MethodChannelScreenshotDetector extends ScreenshotDetectorPlatform {
1010
/// The method channel used to interact with the native platform.
1111
@visibleForTesting
1212
static const EventChannel eventChannel = EventChannel('screenshot_detector');
13+
static const MethodChannel _methodChannel = MethodChannel('screenshot_detector_method');
14+
1315
StreamSubscription<dynamic>? _eventSubscription;
1416

1517
@override
@@ -22,7 +24,8 @@ class MethodChannelScreenshotDetector extends ScreenshotDetectorPlatform {
2224
}
2325

2426
@override
25-
void dispose() {
27+
void dispose() async {
28+
await _methodChannel.invokeMethod('dispose');
2629
_eventSubscription?.cancel();
2730
_eventSubscription = null;
2831
}

lib/screenshot_detector_platform_interface.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ abstract class ScreenshotDetectorPlatform extends PlatformInterface {
2727
return instance.startListening(onScreenshot);
2828
}
2929

30-
void dispose();
30+
void dispose() => instance.dispose();
3131
}

0 commit comments

Comments
 (0)