我需要创建一个应用程序,导航到一个用户后,一段时间的不活动.
我try 将我的应用程序包装在GestureDetector中,并在指定的时间段后运行计时器,如下所示,但它不起作用:
class _MyAppState extends State<MyApp> {
Timer _timer;
@override
void initState() {
super.initState();
_initializeTimer();
}
void _initializeTimer() {
_timer = Timer.periodic(const Duration(seconds: 20), (_) => _logOutUser);
}
void _logOutUser() {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => WelcomePage()));
_timer.cancel();
}
void _handleUserInteraction([_]) {
if (!_timer.isActive) {
return;
}
_timer.cancel();
_initializeTimer();
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: _handleUserInteraction,
onPanDown: _handleUserInteraction,
child: MaterialApp(
title: 'Hello',
home: WelcomePage(),
routes: <String, WidgetBuilder>{
'/welcome': (BuildContext context) => new WelcomePage(),
'/login': (BuildContext context) => new LoginPage(),
},
debugShowCheckedModeBanner: false,
),
);
}
}
我的问题是,在一段时间的不活动之后,我应该如何最好地实现在Ffltter应用程序中运行函数?