Skip to content

Commit 0ae22f2

Browse files
committed
example app
1 parent 9b129df commit 0ae22f2

File tree

1 file changed

+60
-4
lines changed

1 file changed

+60
-4
lines changed

example/lib/main.dart

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,87 @@
1+
import 'dart:async';
2+
13
import 'package:flutter/material.dart';
4+
import 'package:reflex/reflex.dart';
25

36
void main() {
47
runApp(const MyApp());
58
}
69

710
class MyApp extends StatefulWidget {
8-
const MyApp({Key? key}) : super(key: key);
11+
const MyApp({
12+
Key? key,
13+
}) : super(key: key);
914

1015
@override
1116
State<MyApp> createState() => _MyAppState();
1217
}
1318

1419
class _MyAppState extends State<MyApp> {
20+
Reflex? _reflex;
21+
StreamSubscription<NotificationEvent>? _subscription;
22+
final List<NotificationEvent> _log = [];
23+
bool started = false;
24+
1525
@override
1626
void initState() {
1727
super.initState();
28+
initPlatformState();
29+
}
30+
31+
// Platform messages are asynchronous, so we initialize in an async method.
32+
Future<void> initPlatformState() async {
33+
startListening();
34+
}
35+
36+
void onData(NotificationEvent event) {
37+
setState(() {
38+
_log.add(event);
39+
});
40+
debugPrint(event.toString());
41+
}
42+
43+
void startListening() {
44+
_reflex = Reflex();
45+
try {
46+
_subscription = _reflex!.notificationStream!.listen(onData);
47+
setState(() {
48+
started = true;
49+
});
50+
} on ReflexException catch (exception) {
51+
debugPrint(exception.toString());
52+
}
53+
}
54+
55+
void stopListening() {
56+
_subscription?.cancel();
57+
setState(() => started = false);
1858
}
1959

2060
@override
2161
Widget build(BuildContext context) {
2262
return MaterialApp(
2363
home: Scaffold(
2464
appBar: AppBar(
25-
title: const Text('Plugin example app'),
65+
title: const Text('Reflex Example app'),
66+
),
67+
body: Center(
68+
child: ListView.builder(
69+
itemCount: _log.length,
70+
itemBuilder: (BuildContext context, int idx) {
71+
final entry = _log[idx];
72+
return ListTile(
73+
title: Text(entry.title ?? ""),
74+
subtitle: Text(entry.message ?? ""),
75+
trailing: Text(entry.packageName.toString().split('.').last),
76+
);
77+
},
78+
),
2679
),
27-
body: const Center(
28-
child: Text('App Running!!\n'),
80+
floatingActionButton: FloatingActionButton(
81+
onPressed: started ? stopListening : startListening,
82+
tooltip: 'Start/Stop listening',
83+
child:
84+
started ? const Icon(Icons.stop) : const Icon(Icons.play_arrow),
2985
),
3086
),
3187
);

0 commit comments

Comments
 (0)