session_wrapper
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
SessionControllerlogic.
๐ License
This package is licensed under the MIT License. See the LICENSE file for more information.