Skip to content

Commit 47ce157

Browse files
committed
* Update LifecycleState by App Lifecycle Listener
* Add LegacyLifecycleState
1 parent b2b65cd commit 47ce157

File tree

5 files changed

+153
-5
lines changed

5 files changed

+153
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.3.1
2+
3+
* Update LifecycleState by App Lifecycle Listener
4+
* Add LegacyLifecycleState
5+
16
## 1.3.0
27

38
* Add function protect data leakage with blur off for iOS only

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ packages:
121121
path: ".."
122122
relative: true
123123
source: path
124-
version: "1.3.0"
124+
version: "1.3.1"
125125
sky_engine:
126126
dependency: transitive
127127
description: flutter
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import 'package:flutter/foundation.dart';
2+
import 'package:flutter/material.dart';
3+
4+
abstract class LegacyLifecycleState<T extends StatefulWidget> extends State<T>
5+
with WidgetsBindingObserver {
6+
@override
7+
void initState() {
8+
super.initState();
9+
WidgetsBinding.instance.addObserver(this);
10+
}
11+
12+
@override
13+
void dispose() {
14+
WidgetsBinding.instance.removeObserver(this);
15+
super.dispose();
16+
}
17+
18+
@override
19+
void didChangeAppLifecycleState(AppLifecycleState state) {
20+
switch (state) {
21+
case AppLifecycleState.resumed:
22+
onResumed();
23+
break;
24+
case AppLifecycleState.inactive:
25+
onPaused();
26+
break;
27+
case AppLifecycleState.paused:
28+
onInactive();
29+
break;
30+
case AppLifecycleState.detached:
31+
onDetached();
32+
break;
33+
case AppLifecycleState.hidden:
34+
onHidden();
35+
break;
36+
}
37+
}
38+
39+
void onResumed() {
40+
if (kDebugMode) {
41+
print("on resumed");
42+
}
43+
}
44+
45+
void onPaused() {
46+
if (kDebugMode) {
47+
print("on paused");
48+
}
49+
}
50+
51+
void onInactive() {
52+
if (kDebugMode) {
53+
print("on inactive");
54+
}
55+
}
56+
57+
void onDetached() {
58+
if (kDebugMode) {
59+
print("on detached");
60+
}
61+
}
62+
63+
void onHidden() {
64+
if (kDebugMode) {
65+
print("on hidden");
66+
}
67+
}
68+
}

lib/lifecycle/lifecycle_state.dart

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
import 'dart:ui';
2+
13
import 'package:flutter/foundation.dart';
24
import 'package:flutter/material.dart';
35

46
abstract class LifecycleState<T extends StatefulWidget> extends State<T>
57
with WidgetsBindingObserver {
8+
AppLifecycleState? _lifecycleState;
9+
610
@override
711
void initState() {
812
super.initState();
13+
_lifecycleState = WidgetsBinding.instance.lifecycleState;
914
WidgetsBinding.instance.addObserver(this);
1015
}
1116

@@ -15,23 +20,87 @@ abstract class LifecycleState<T extends StatefulWidget> extends State<T>
1520
super.dispose();
1621
}
1722

23+
@override
24+
Future<AppExitResponse> didRequestAppExit() async {
25+
onExitRequested();
26+
return AppExitResponse.exit;
27+
}
28+
29+
/// [App Lifecycle Listener](https://miro.medium.com/v2/resize:fit:1400/0*bN0QtrIRWGDMC9LJ)
30+
///
31+
/// detached -> resumed -|
32+
/// ^ v
33+
/// | inactive
34+
/// paused <- hidden <--|
35+
///
1836
@override
1937
void didChangeAppLifecycleState(AppLifecycleState state) {
38+
final AppLifecycleState? previousState = _lifecycleState;
39+
if (state == previousState) {
40+
// Transitioning to the same state twice doesn't produce any notifications (but also won't actually occur).
41+
return;
42+
}
43+
_lifecycleState = state;
2044
switch (state) {
2145
case AppLifecycleState.resumed:
2246
onResumed();
2347
break;
2448
case AppLifecycleState.inactive:
25-
onPaused();
49+
if (previousState == AppLifecycleState.hidden) {
50+
onShow();
51+
} else if (previousState == null ||
52+
previousState == AppLifecycleState.resumed) {
53+
onInactive();
54+
}
55+
break;
56+
case AppLifecycleState.hidden:
57+
if (previousState == AppLifecycleState.paused) {
58+
onRestart();
59+
} else if (previousState == null ||
60+
previousState == AppLifecycleState.inactive) {
61+
onHidden();
62+
}
2663
break;
2764
case AppLifecycleState.paused:
28-
onInactive();
65+
if (previousState == null ||
66+
previousState == AppLifecycleState.hidden) {
67+
onPaused();
68+
}
2969
break;
3070
case AppLifecycleState.detached:
3171
onDetached();
3272
break;
3373
default:
3474
}
75+
76+
// At this point, it can't be null anymore.
77+
if (_lifecycleState != null) {
78+
onStateChange(_lifecycleState!);
79+
}
80+
}
81+
82+
void onExitRequested() {
83+
if (kDebugMode) {
84+
print("on exit requested");
85+
}
86+
}
87+
88+
void onStateChange(AppLifecycleState state) {
89+
if (kDebugMode) {
90+
print("on state change: $state");
91+
}
92+
}
93+
94+
void onShow() {
95+
if (kDebugMode) {
96+
print("on show");
97+
}
98+
}
99+
100+
void onHidden() {
101+
if (kDebugMode) {
102+
print("on hidden");
103+
}
35104
}
36105

37106
void onResumed() {
@@ -57,4 +126,10 @@ abstract class LifecycleState<T extends StatefulWidget> extends State<T>
57126
print("on detached");
58127
}
59128
}
129+
130+
void onRestart() {
131+
if (kDebugMode) {
132+
print("on restart");
133+
}
134+
}
60135
}

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: screen_protector
22
description: Safe Data Leakage via Application Background Screenshot and Prevent Screenshot for Android and iOS.
3-
version: 1.3.0
3+
version: 1.3.1
44
homepage: https://github.com/prongbang/screen_protector
55

66
environment:
@@ -14,7 +14,7 @@ dependencies:
1414
dev_dependencies:
1515
flutter_test:
1616
sdk: flutter
17-
flutter_lints: ^2.0.2
17+
flutter_lints: ^2.0.3
1818

1919
# For information on the generic Dart part of this file, see the
2020
# following page: https://dart.dev/tools/pub/pubspec

0 commit comments

Comments
 (0)