đź’¤

Idle Logout

Minimum Dart Version Platform Very Good CLI

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).


Idle Logout Demo


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: ^1.0.0+1

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 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: isLoggedIn,
      isLockedOut: isLockedOut,
      lockedOutAction: lockedOutAction,
      child: MaterialApp(navigatorKey: navigatorKey, home: const HomeScreen()),
    );
  }
}

Future<void> lockedOutAction() async {
  debugPrint('User logged out due to inactivity');

  await navigatorKey.currentState?.pushReplacement(
    MaterialPageRoute<void>(builder: (_) => const LockScreen()),
  );
}

Future<bool> isLoggedIn() async {
  // write your in-app logic for checking if your app is logged in
  return true;
}

Future<bool> isLockedOut() async {
  // write your in-app logic for checking if your app is locked out
  return false;
}


API Documentation

Constructor

IdleLogout({
  required Widget child,
  required Future<bool> Function() isLoggedIn,
  required Future<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, lockedOutAction is executed immediately.
  • If not provided, this defaults to 30 seconds.

This helps protect sessions when the app is backgrounded or the device is locked. One of the use cases of this is when dialogs pops up, the app locks immediately if you do not include a delay.


isLoggedIn

Future<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.

isLockedOut

Future<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() returns true
  • isLockedOut() returns false

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.


Libraries

idle_logout
A Flutter package for handling idle user logout