power_state_monitor 0.1.0 copy "power_state_monitor: ^0.1.0" to clipboard
power_state_monitor: ^0.1.0 copied to clipboard

Flutter plugin that exposes Windows sleep/resume power state events via a Dart stream.

power_state_monitor #

Flutter plugin that delivers Windows power management events (suspend and resume) straight into your app. It wraps a lightweight native listener around WM_POWERBROADCAST, then exposes events as a Dart broadcast stream so your UI and services can react in real time.

  • Observe when the machine enters sleep or hibernate.
  • Distinguish between user-triggered and automatic resume events.
  • Keep your app state in sync by refreshing data or re-establishing connections after wake.

Platform support #

Platform Status
Windows ✅ Implemented
macOS ⛔ Not yet
Linux ⛔ Not yet
Android ⛔ Not yet
iOS ⛔ Not yet
Web ⛔ Not yet

The plugin silently refuses to initialize on unsupported targets, so you can keep a single code path. Use PowerStateMonitor.instance.isPlatformSupported to guard features when needed.

Installation #

  1. Add the dependency in your pubspec.yaml:
dependencies:
  power_state_monitor: ^0.0.1
  1. Run flutter pub get.

  2. Target Windows (64-bit). No additional configuration is required.

Quick start #

Call initialize() early (for example in main() or your root widget's initState) and listen to the broadcast stream:

import 'package:flutter/material.dart';
import 'package:power_state_monitor/power_state_monitor.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  final initialized = await PowerStateMonitor.instance.initialize();
  if (!initialized) {
    debugPrint('PowerStateMonitor: unsupported platform');
  }

  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    PowerStateMonitor.instance.onPowerStateChanged.listen((event) {
      debugPrint('Power state changed: ${event.name}');
      if (event == PowerStateEvent.resumeAutomatic ||
          event == PowerStateEvent.resumeSuspend) {
        _refreshData();
      }
    });
  }

  void _refreshData() {
    // Your resume handling logic.
  }

  @override
  void dispose() {
    PowerStateMonitor.instance.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => const Placeholder();
}

Event reference #

Event Native source Typical use
PowerStateEvent.suspend PBT_APMSUSPEND Save app state, pause background work
PowerStateEvent.resumeSuspend PBT_APMRESUMESUSPEND User woke the PC: refresh data, reconnect sockets
PowerStateEvent.resumeAutomatic PBT_APMRESUMEAUTOMATIC System woke automatically: defer UI updates until user interaction

Under the hood the plugin registers a hidden window and forwards notifications through a MethodChannel. The Dart side keeps a broadcast StreamController, so you can attach multiple listeners safely.

Testing & debugging #

  • Use PowerStateMonitor.instance.debugPlatformOverride in widget tests to mock a supported OS.
  • On Windows, trigger sleep/resume manually or use the psshutdown utility (psshutdown -d -t 0) to automate testing.
  • Remember to call dispose() when your app shuts down or when you hot-reload during development to avoid duplicate listeners.

Example app #

A minimal example is available under example/. Launch it on Windows and put your device to sleep to see events show up in real time.

Roadmap ideas #

  • macOS and Linux platform implementations.
  • Optional callbacks for battery and AC change notifications.
  • Integrations with background services.

Contributing #

Contributions are welcome! Please file an issue or a pull request describing the change. Keep platform-specific code well isolated and cover new Dart logic with tests where possible.

License #

This project is distributed under the MIT License. See the LICENSE file for details.

0
likes
0
points
0
downloads

Publisher

unverified uploader

Weekly Downloads

Flutter plugin that exposes Windows sleep/resume power state events via a Dart stream.

Repository (GitHub)
View/report issues

Topics

#flutter-plugin #windows #power-management #system-events

License

unknown (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on power_state_monitor

Packages that implement power_state_monitor