I need to create an app that navigates a user to after a period of inactivity.
I tried wrapping my app in GestureDetector and run a timer after a specified period as shown below but it is not working:
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,
),
);
}
}
My question is how best should I implement running a function after a period of inactivity within my flutter app?