【发布时间】:2021-05-19 06:32:56
【问题描述】:
如果用户在登录后超过 5 分钟对应用没有反应或处于非活动状态,我需要从应用中注销用户。该应用程序将转到主登录页面。
我尝试实施给定的解决方案here,但没有成功。请帮助我如何实现这一目标。
我的代码
class AppRootState extends State<AppRoot> {
Timer _rootTimer;
@override
void initState() {
// TODO: implement initState
super.initState();
initializeTimer();
}
void initializeTimer() {
const time = const Duration(minutes: 5);
_rootTimer = Timer(time, () {
logOutUser();
});
}
void logOutUser() async {
// Log out the user if they're logged in, then cancel the timer.
// You'll have to make sure to cancel the timer if the user manually logs out
// and to call _initializeTimer once the user logs in
_rootTimer?.cancel();
}
// You'll probably want to wrap this function in a debounce
void _handleUserInteraction([_]) {
if (_rootTimer != null && !_rootTimer.isActive) {
// This means the user has been logged out
return;
}
_rootTimer?.cancel();
initializeTimer();
}
@override
Widget build(BuildContext context) {
return Listener(
behavior: HitTestBehavior.translucent,
onPointerDown: _handleUserInteraction,
onPointerMove: _handleUserInteraction,
onPointerUp: _handleUserInteraction,
child: MaterialApp(
)
);
}
}
我尝试使用 Listener 和 GestureDetector,但它不起作用。用户甚至在积极使用该应用程序时也已注销。
【问题讨论】: