如何在Flutter中检测用户不活动



我使用Java开发了Android应用程序,现在想迁移到Flutter。

为了检测用户不活动,我在我的Activity:onUserInteraction中重写这个方法并重置Timer,如果没有用户交互,我显示一个屏幕保护程序,如动画。

是否有任何干净的方式在扑动做到这一点?我不想使用平台渠道。我想要纯粹的飞镖和飞镖。我知道我可以在用户触摸时设置计时器并重置它,但就像在Android中一样,我希望系统通知我有关用户交互。

您可以将MaterialApp包裹在Listener中。并在交互时重置计时器。类似于你在android中做的。

它只监听点击、拖动、释放或取消等手势。但是,它不监听鼠标事件,比如在不按任何按钮的情况下悬停一个区域。对于这样的事件,使用mousereregion。

示例代码:

import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Listener(
onPointerDown: (_) => print('down'), // best place to reset timer imo
onPointerMove: (_) => print('move'),
onPointerUp: (_) => print('up'),
child: MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
),
);
}
}
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return TextFormField(
maxLength: 10,
maxLengthEnforced: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Details',
),
);
}
}

我在我的应用程序后面有一个类似的逻辑,这是我想到的。

class _MyHomePageState extends State<MyHomePage> {
InActivityTimer timer = InActivityTimer();
int _counter = 0;
void _incrementCounter() {
timer.handleUserInteraction(context);
setState(() {
_counter++;
});
}
@override
void initState() {
// TODO: implement initState
super.initState();
timer.startTimer(context);
}
@override
Widget build(BuildContext context) {
return Listener(
onPointerHover: (_) {
timer.handleUserInteraction(context);
},
onPointerMove: (_) {
timer.handleUserInteraction(context);
},
child: RawKeyboardListener(
focusNode: FocusNode(),
onKey: (RawKeyEvent event) {
if (event is RawKeyDownEvent) {
timer.handleUserInteraction(context);
}
child: Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), 
),
);
}
}
class InActivityTimer {
Timer? _timer;
DateTime? lastInteractionTime;
void startTimer(BuildContext context) {
lastInteractionTime = DateTime.now();
_startInactivityTimer(context);
}
void _startInactivityTimer(BuildContext context) {
print("start Inactivity timer is triggered");
if (_timer != null && _timer!.isActive) {
_timer!.cancel();
}
_timer = Timer(Duration(seconds: 10), () {
_restartApp(context);
});
}
void _restartApp(BuildContext context) {
print("restarting the app");
// You can use platform-specific code to restart the app here.
// This example uses Flutter's `runApp` to restart the app.
RestartWidget.restartApp(context);
}
void handleUserInteraction(BuildContext context) {
print("Handling user interaction");
lastInteractionTime = DateTime.now();
_startInactivityTimer(context);
}
}
}

你所需要做的就是访问应用的生命周期。要做到这一点,一个好方法是在应用的LandingPage上使用mixin WidgetsBindingObserver。

mixin为你提供了一个枚举AppLifecycleState,它可以有4个值,分别是detached, inactive, paused和resume,用来描述应用的当前状态。

你可以用这个创建一个函数,例如didAppLifecycleChange,它接受应用程序的状态,即didAppLifecycleChange(AppLifecycleState state)。然后,你可以使用这些状态在应用程序上执行操作。

最新更新