Reputation: 79
I need to logout user from app if user has no reaction to app or inactive more than 5 minutes after login. the app will go to main login page.
I tried to implement the given solution here but not got success. Please help me how to achieve this.
My Code
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(
)
);
}
}
I tried with Listener and GestureDetector but it's not working. User logged out even actively using the app.
Upvotes: 2
Views: 5121
Reputation: 1968
Late but might be help to someone other. Last time I faced same problem. I was surprised when I declared Timer _rootTimer as global variable and my code started working.
My code is:
Timer _rootTimer;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) => AppRoot();
}
class AppRoot extends StatefulWidget {
// This widget is the root of your application.
@override
AppRootState createState() => AppRootState();
}
class AppRootState extends State<AppRoot> {
@override
void initState() {
// TODO: implement initState
super.initState();
initializeTimer();
}
void initializeTimer() {
if (_rootTimer != null) _rootTimer.cancel();
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.
_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(
title: 'example',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: HomePage()
),
);
}
}
Upvotes: 5