Installation
Add magic_device to a Flutter app, check that it answers, and know what it does not need.
On this page
Install#
flutter pub add magic_device
import 'package:magic_device/magic_device.dart';
The barrel is the only import; everything under src/ is private. BatteryState is re-exported from it, so a custom batteryState reader names the type without adding battery_plus to the app's own dependencies.
Requirements#
| Requirement | Value |
|---|---|
| Dart | >=3.6.0 |
| Flutter | >=3.27.0 |
| Dependencies | battery_plus and flutter_inactive_timer, pulled in automatically; WindowFullscreen uses the Flutter SDK only |
| Flutter binding | Outside macOS and Windows, DevicePower.start() attaches a lifecycle observer, so call it from the main isolate once Flutter's binding exists |
What You Do Not Need#
- No service provider, config file, facade or CLI command, and no dependency on
magic. Construct the classes where you need them: a controller, a provider'sboot()or a background job. - No
plugin:installstep. - No Runner edit. The package's macOS plugin registers the
magic_device/windowchannel itself, so the host'sMainFlutterWindow.swiftstays Flutter's stock one.
Verify#
import 'package:flutter/widgets.dart';
import 'package:magic_device/magic_device.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
final DevicePower power = DevicePower()..start();
debugPrint('on mains: ${await power.charging}, away: ${power.idle}');
power.stop();
debugPrint('fullscreen available: ${WindowFullscreen.instance.available}');
}
On a laptop on battery this prints on mains: false. fullscreen available is true on macOS and false everywhere else.
Platforms#
| Signal | Source |
|---|---|
DevicePower.charging |
battery_plus. A platform with no implementation answers false, and a web browser without navigator.getBattery reports unknown, which also reads as false. |
DevicePower.idle on native macOS and Windows |
The system-wide last-input clock. |
DevicePower.idle everywhere else, web included |
The app lifecycle: idle whenever the app is not in front. |
WindowFullscreen |
macOS only. available is false elsewhere and toggle() sends nothing. |
Related Documentation#
- DevicePower: mains power and viewer idle.
- WindowFullscreen: the macOS window state and its toggle.
- Testing: injecting every platform read.