Lock screen after 1 minut off inactivity

android_attempt
Felix Bruns 10 months ago
parent c1624d7e05
commit 1a5daf3d66

@ -36,7 +36,7 @@ class _ProxmoxListerState extends State<ProxmoxListerView> {
nodes = Future<ProxmoxNodeMap>.delayed(Duration.zero, () => getVms()); nodes = Future<ProxmoxNodeMap>.delayed(Duration.zero, () => getVms());
Timer.periodic(const Duration(seconds: 3), (_) { Timer.periodic(const Duration(seconds: 3), (_) {
syncVMs(); syncVMs((_) {});
}); });
} }
@ -60,16 +60,25 @@ class _ProxmoxListerState extends State<ProxmoxListerView> {
return map; return map;
} }
void syncVMs() async { void syncVMs(Function(Exception) onExcept) async {
nodes = getVms(); nodes = getVms();
try {
await nodes; await nodes;
} on Exception catch (e) {
onExcept(e);
}
setState(() {}); setState(() {});
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// infinite touch container to turn screen back on // infinite touch container to turn screen back on
return Stack( return IdleTurnOff(
timeout: const Duration(seconds: 60),
onExpire: () async {
await turnOffScreen();
},
child: Stack(
children: [ children: [
Scaffold( Scaffold(
appBar: appbar(context), appBar: appbar(context),
@ -77,6 +86,7 @@ class _ProxmoxListerState extends State<ProxmoxListerView> {
), ),
screenActivator(), screenActivator(),
], ],
),
); );
} }
@ -93,7 +103,16 @@ class _ProxmoxListerState extends State<ProxmoxListerView> {
IconButton( IconButton(
icon: const Icon(Icons.sync), icon: const Icon(Icons.sync),
onPressed: () { onPressed: () {
syncVMs(); syncVMs((e) {
showDialog(
context: context,
builder: (BuildContext ctx) {
return AlertDialog(
title: const Text("Error"),
content: Text(e.toString()),
);
});
});
}, },
), ),
IconButton( IconButton(

@ -1,5 +1,6 @@
import 'dart:io'; import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:async/async.dart';
const _off = "1"; const _off = "1";
const _on = "0"; const _on = "0";
@ -48,3 +49,45 @@ Widget screenActivator() {
), ),
); );
} }
class IdleTurnOff extends StatefulWidget {
const IdleTurnOff({
super.key,
required this.child,
required this.timeout,
required this.onExpire,
});
final Widget child;
final Duration timeout;
final Function() onExpire;
@override
State<IdleTurnOff> createState() => _IdleTurnOffState();
}
class _IdleTurnOffState extends State<IdleTurnOff> {
late RestartableTimer _timer;
@override
void initState() {
super.initState();
_timer = RestartableTimer(widget.timeout, widget.onExpire);
}
@override
Widget build(BuildContext context) {
return KeyboardListener(
focusNode: FocusNode(
onKeyEvent: (_1, _2) {
_timer.reset();
return KeyEventResult.ignored;
},
),
child: Listener(
child: widget.child,
onPointerDown: (_) => _timer.reset(),
),
);
}
}

Loading…
Cancel
Save