idle_logout 0.1.4
idle_logout: ^0.1.4 copied to clipboard
Automatic inactivity-based logout for secure Flutter applications.
💤
Idle Logout
A Flutter package for handling automatic user logout after a period of inactivity. Ideal for apps where session security and compliance are important (e.g., banking, healthcare, enterprise apps).
Features #
- Detects user inactivity.
- Logs out automatically after a configurable timeout.
- Resets the timer on user activity.
- Simple and flexible API.
Installation #
Add to your project:
flutter pub add idle_logout
Or manually add to your pubspec.yaml:
dependencies:
idle_logout: ^0.1.4
Usage #
Basic Example #
import 'package:flutter/material.dart';
import 'package:idle_logout/idle_logout.dart';
import '../../screens/home_screen.dart';
import 'screens/lock_screen.dart';
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return IdleLogout(
pauseThreshold: const Duration(seconds: 15),
timeout: const Duration(seconds: 10),
isLoggedIn: () => true,
isLockedOut: () => false,
lockedOutAction: () async {
debugPrint('User logged out due to inactivity');
navigatorKey.currentState?.pushReplacement(
MaterialPageRoute<void>(
builder: (BuildContext context) => const LockScreen(),
),
);
},
child: MaterialApp(
navigatorKey: navigatorKey,
home: const HomeScreen(),
),
);
}
}
API Documentation #
Constructor #
IdleLogout({
required Widget child,
required bool Function() isLoggedIn,
required bool Function() isLockedOut,
required Future<void> Function() lockedOutAction,
required Duration timeout,
Duration? pauseThreshold,
})
Parameters #
child
Widget child
The widget subtree to monitor for user activity.
This is typically your MaterialApp, CupertinoApp, or a top‑level page. All pointer and keyboard events within this subtree reset the idle timer.
timeout
Duration timeout
The duration of inactivity allowed before the user is considered idle.
- The timer resets on every user interaction (touch, mouse, keyboard).
- When this duration elapses with no interaction, the idle handler is triggered.
pauseThreshold
Duration? pauseThreshold
The maximum amount of time the app may remain in the background before the user is automatically logged out on resume.
- If the app resumes after being paused longer than this duration,
lockedOutActionis executed immediately. - If not provided, this defaults to 30 seconds.
This helps protect sessions when the app is backgrounded or the device is locked.
isLoggedIn
bool Function() isLoggedIn
Determines whether idle monitoring should be active.
- If this returns
false, idle detection is disabled. - Useful for login, onboarding, or public routes.
This callback should be synchronous and inexpensive.
isLockedOut
bool Function() isLockedOut
Indicates whether the user is already logged out or locked.
- Prevents multiple executions of
lockedOutAction. - Avoids duplicate navigation or logout calls.
lockedOutAction
Future<void> Function() lockedOutAction
The callback executed when the user must be logged out due to inactivity.
Typical responsibilities include:
- Clearing authentication state
- Revoking tokens
- Navigating to a login or lock screen
- Displaying a session‑expired message
This action is executed only if:
isLoggedIn()returnstrueisLockedOut()returnsfalse
Testing #
This package is set up with Very Good Analysis and Very Good Workflows.
Run tests with:
very_good test --coverage
Generate and view coverage:
genhtml coverage/lcov.info -o coverage/
open coverage/index.html
License #
Licensed under the MIT License.