flutter_user_idle

pub package likes popularity pub points License: MIT

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 IdleDetector inside MaterialApp, 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 IdleDetector inside MaterialApp at 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.