flutter_user_idle
A simple and lightweight Flutter package to detect user inactivity (idle state) and trigger callbacks when the user becomes idle or active again.
β¨ Features
- Detect user inactivity (idle)
- Trigger callback when user becomes idle
- Detect when user becomes active again
- Lightweight and efficient
- Easy integration with any Flutter app
π Why This Package?
Handling user inactivity is a common requirement in apps like:
- π Auto logout systems
- π€ Session timeout handling
- π User activity tracking
- π Auto refresh after inactivity
This package provides a simple way to detect inactivity without complex setup.
π¦ Installation
Add this to your pubspec.yaml:
dependencies:
flutter_user_idle: ^0.1.0
π§βπ» Usage
Wrap your widget tree with IdleDetector inside MaterialApp:
import 'package:flutter_user_idle/flutter_user_idle.dart';
class MyPage extends StatefulWidget {
const MyPage({super.key});
@override
State<MyPage> createState() => _MyPageState();
}
class _MyPageState extends State<MyPage> {
bool _isIdle = false;
@override
Widget build(BuildContext context) {
return IdleDetector(
timeout: const Duration(minutes: 5),
onIdle: () => setState(() => _isIdle = true),
onActive: () => setState(() => _isIdle = false),
child: Scaffold(
body: Center(
child: Text(_isIdle ? 'Idle π΄' : 'Active π’'),
),
),
);
}
}
β οΈ Place
IdleDetectorinsideMaterialApp, not above it.
π§ͺ Example
Check the /example folder for a complete working demo.
βοΈ Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| timeout | Duration | β Yes | Time before user is considered idle |
| onIdle | VoidCallback | β Yes | Called when user becomes idle |
| onActive | VoidCallback? | β No | Called when user becomes active again |
| child | Widget | β Yes | Your widget tree |
π± Behavior
| State | Supported |
|---|---|
| Foreground | β Yes |
| Background | β No |
| App Resume Detection | β οΈ Manual handling |
π§ How it works
The package listens to pointer events (taps, scrolls, gestures) anywhere in the widget subtree.
If no interaction is detected within the given timeout, the user is marked as idle and onIdle fires.
Once interaction resumes, onActive is triggered.
π‘ Best Practices
- Place
IdleDetectorinsideMaterialAppat the screen level, not above it - Use for session timeout or auto logout
- Combine with app lifecycle for better accuracy
- Avoid very short timeout durations in production
π€ Contributing
Contributions are welcome! Feel free to open issues or submit pull requests.
π€ Author
AZHARKC
GitHub: https://github.com/AZHARKC
π License
MIT License
β If you find this package useful, consider starring the repository!
Libraries
- flutter_user_idle
- A Flutter package for detecting user idle state based on pointer activity.