session_wrapper

Pub Version License: MIT

A lightweight Flutter widget that helps manage user session timeout based on inactivity. Wrap your app or screens with SessionWrapper to automatically reset a countdown timer on user interaction and trigger a callback when the session expires.


๐Ÿ“ธ Preview


โœจ Features

  • Detects user inactivity through touch input.
  • Triggers custom callback on session timeout.
  • Easy integration with existing Flutter apps.
  • Configurable timeout duration.

๐Ÿš€ Getting Started

1. Install

Add this to your pubspec.yaml:

dependencies:
  session_wrapper: ^0.0.4

Then run:

flutter pub get

2. Usage

Step 1: Define a SessionController

final SessionController sessionController = SessionController(
  timeoutInSeconds: 180, // e.g., 3 minutes
  onSessionExpired: () {
    // Handle session expiration, e.g., show alert or logout
  },
);

Step 2: Wrap your widget tree

MaterialApp(
  navigatorKey: appNavigatorKey,
  home: SessionWrapper(
    controller: sessionController,
    child: YourAppHome(),
  ),
);

Step 3: Start the session

sessionController.startSession();

๐Ÿงช Example

void main() {
  sessionController.startSession();

  runApp(
    MaterialApp(
      navigatorKey: appNavigatorKey,
      home: SessionWrapper(
        controller: sessionController,
        child: SessionWrapperExample(),
      ),
    ),
  );
}

Inside SessionWrapperExample, user taps reset the session timer. If inactive for the set timeout duration, the session expires.


๐Ÿ“ฆ API Reference

SessionController

Property/Method Description
timeoutInSeconds Duration in seconds before timeout.
onSessionExpired() Callback invoked on session timeout.
startSession() Begins session tracking.
resetSession() Resets the inactivity timer.
stopSession() Stops session tracking.
tick() Advances timer, used internally.

๐Ÿ’ก Notes

  • Inactivity is detected using pointer events (onPointerDown).
  • You can customize session behavior by modifying the SessionController logic.

๐Ÿ“„ License

This package is licensed under the MIT License. See the LICENSE file for more information.

Libraries

session_wrapper