Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: allow changing backgroundLockLatency #27

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
57 changes: 57 additions & 0 deletions example/lib/screens/my_home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class _MyHomePageState extends State<MyHomePage> {
}
},
),
const LatencyEditor(
key: Key('LatencyEditor'),
),
],
),
),
Expand All @@ -82,3 +85,57 @@ class _MyHomePageState extends State<MyHomePage> {
);
}
}

class LatencyEditor extends StatefulWidget {
const LatencyEditor({Key? key}) : super(key: key);

@override
State<LatencyEditor> createState() => _LatencyEditorState();
}

class _LatencyEditorState extends State<LatencyEditor> {
late TextEditingController backgroundLockLatencyEditingController;

@override
void initState() {
backgroundLockLatencyEditingController = TextEditingController(text: "0");
super.initState();
}

@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: _setBackgroundLockLatency,
child: const Text('Set backgroundLockLatency to'),
),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 20),
child: TextField(
textAlign: TextAlign.right,
controller: backgroundLockLatencyEditingController,
),
),
),
const Text('s'),
],
);
}

void _setBackgroundLockLatency() {
try {
int latencyInSeconds =
int.parse(backgroundLockLatencyEditingController.text);
AppLock.of(context)?.backgroundLockLatency =
Duration(seconds: latencyInSeconds);
} catch (e) {
if (kDebugMode) {
print(e);
}
}
}
}
6 changes: 5 additions & 1 deletion lib/src/widgets/app_lock.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class _AppLockState extends State<AppLock> with WidgetsBindingObserver {
late bool _isLocked;
late bool _enabled;

late Duration backgroundLockLatency;

Timer? _backgroundLockLatencyTimer;

@override
Expand All @@ -63,6 +65,8 @@ class _AppLockState extends State<AppLock> with WidgetsBindingObserver {
_isLocked = false;
_enabled = widget.enabled;

backgroundLockLatency = widget.backgroundLockLatency;

super.initState();
}

Expand All @@ -75,7 +79,7 @@ class _AppLockState extends State<AppLock> with WidgetsBindingObserver {
if (state == AppLifecycleState.paused &&
(!_isLocked && _didUnlockForAppLaunch)) {
_backgroundLockLatencyTimer =
Timer(widget.backgroundLockLatency, () => showLockScreen());
Timer(backgroundLockLatency, () => showLockScreen());
}

if (state == AppLifecycleState.resumed) {
Expand Down