flutter_system_events 0.6.0
flutter_system_events: ^0.6.0 copied to clipboard
A Flutter plugin for listening to keyboard, lifecycle, network, memory, battery, orientation, and time events on Android and iOS.
flutter_system_events #
中文文档
A lightweight, all-around system monitoring plugin for Flutter.
Version 0.6.0 adds Android and iOS time change events.
In real apps, I often need to listen to small pieces of system state: network changes, app lifecycle, keyboard height, memory pressure, battery state, orientation changes, time changes, and so on. Each one can be solved with a separate package, but pulling in several plugins just to watch a few events can feel heavier than the problem needs.
flutter_system_events exists for that case. It only listens for system events
and exposes them through one small, typed API. You can also enable only the event
types you need, so unused listeners do not waste resources.
- Show an offline banner from
NetworkEvent - Refresh data when the app resumes from
LifecycleEvent - Move input UI with keyboard height from
KeyboardEvent - Clear app-owned caches from
MemoryEvent - Reduce background work from
BatteryEvent - Adapt layouts from
OrientationEvent - Refresh date-sensitive UI from
TimeEvent
Use this when you want one small API instead of wiring several platform-specific listeners or packages.
Platform support #
| Event | Android | iOS | macOS | Windows | Linux | Web |
|---|---|---|---|---|---|---|
| Keyboard | Yes | Yes | In progress | In progress | In progress | Yes |
| Lifecycle | Yes | Yes | In progress | In progress | In progress | Yes |
| Network | Yes | Yes | In progress | In progress | In progress | Yes |
| Memory | Yes | Yes | In progress | In progress | In progress | In progress |
| Battery | Yes | Yes | In progress | In progress | In progress | In progress |
| Orientation | Yes | Yes | In progress | In progress | In progress | In progress |
| Time | Yes | Yes | In progress | In progress | In progress | In progress |
Installation #
dependencies:
flutter_system_events: ^0.6.0
Usage #
Initialize once, then listen to SystemEvents.events.
import 'dart:async';
import 'package:flutter/painting.dart';
import 'package:flutter_system_events/flutter_system_events.dart';
StreamSubscription<SystemEvent>? subscription;
Future<void> startSystemEvents() async {
subscription = SystemEvents.events.listen((event) {
switch (event) {
case KeyboardEvent(:final visible, :final height):
print('keyboard visible=$visible height=$height');
case LifecycleEvent(:final state):
if (state == LifecycleState.resumed) print('refresh data');
case NetworkEvent(:final online, :final networkType):
print('network online=$online type=${networkType.name}');
case MemoryEvent():
PaintingBinding.instance.imageCache.clear();
PaintingBinding.instance.imageCache.clearLiveImages();
case BatteryEvent(:final level, :final charging, :final state):
print('battery level=$level charging=$charging state=${state.name}');
case OrientationEvent(:final orientation):
print('orientation=${orientation.name}');
case TimeEvent(:final reason):
print('time reason=${reason.name}');
case UnknownSystemEvent(:final rawType, :final reason):
print('unknown event type=$rawType reason=$reason');
}
});
await SystemEvents.initialize();
}
Future<void> stopSystemEvents() async {
await subscription?.cancel();
await SystemEvents.dispose();
}
By default, initialize() starts keyboard, lifecycle, network, memory,
orientation, and time events. Battery is opt-in:
await SystemEvents.initialize(config: const SystemEventsConfig.all());
Pass a custom config to enable only the events you need:
await SystemEvents.initialize(
config: const SystemEventsConfig(
network: NetworkConfig(),
battery: BatteryConfig(),
),
);
Memory events are hints. The plugin reports pressure; your app decides what can be released safely.
Example #
Run the example app and open each event page:
cd example
flutter run
The example includes separate pages for:
- Keyboard
- Lifecycle
- Network
- Memory
- Battery
- Orientation
- Time
Each page shows the latest event value at the top and provides a simple way to trigger or manually verify the event.