screen_state 3.0.1 screen_state: ^3.0.1 copied to clipboard
A plugin for reporting screen events while the flutter application is running in background. Works for Android only.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:screen_state/screen_state.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class ScreenStateEventEntry {
ScreenStateEvent event;
DateTime? time;
ScreenStateEventEntry(this.event) {
time = DateTime.now();
}
}
class _MyAppState extends State<MyApp> {
Screen _screen = Screen();
StreamSubscription<ScreenStateEvent>? _subscription;
bool started = false;
List<ScreenStateEventEntry> _log = [];
void initState() {
super.initState();
startListening();
}
/// Start listening to screen events
void startListening() {
try {
_subscription = _screen.screenStateStream!.listen(_onData);
setState(() => started = true);
} on ScreenStateException catch (exception) {
print(exception);
}
}
void _onData(ScreenStateEvent event) {
setState(() {
_log.add(ScreenStateEventEntry(event));
});
print(event);
}
/// Stop listening to screen events
void stopListening() {
_subscription?.cancel();
setState(() => started = false);
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('Screen State Example'),
),
body: new Center(
child: new ListView.builder(
itemCount: _log.length,
reverse: true,
itemBuilder: (BuildContext context, int idx) {
final entry = _log[idx];
return ListTile(
leading: Text(entry.time.toString().substring(0, 19)),
trailing: Text(entry.event.toString().split('.').last));
})),
floatingActionButton: new FloatingActionButton(
onPressed: started ? stopListening : startListening,
tooltip: 'Start/Stop Listening',
child: started ? Icon(Icons.stop) : Icon(Icons.play_arrow),
),
),
);
}
}