session_wrapper 0.0.4

SDKflutter
Platformandroidioswindowslinuxmacosweb

A Flutter widget that wraps your app to track user inactivity and trigger session timeout logic using a customizable controller.

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/MethodDescription
timeoutInSecondsDuration 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.