-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.dart
More file actions
120 lines (100 loc) · 3.19 KB
/
Copy pathmain.dart
File metadata and controls
120 lines (100 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
import 'package:logging/logging.dart';
import 'package:simple_frame_app/simple_frame_app.dart';
import 'package:frame_msg/tx/code.dart';
import 'package:frame_msg/tx/sprite.dart';
void main() => runApp(const MainApp());
final _log = Logger("MainApp");
class MainApp extends StatefulWidget {
const MainApp({super.key});
@override
State<MainApp> createState() => MainAppState();
}
/// SimpleFrameAppState mixin helps to manage the lifecycle of the Frame connection outside of this file
class MainAppState extends State<MainApp> with SimpleFrameAppState {
// show the loaded image in the Flutter UI also
Image? _image;
MainAppState() {
Logger.root.level = Level.INFO;
Logger.root.onRecord.listen((record) {
debugPrint('${record.level.name}: [${record.loggerName}] ${record.time}: ${record.message}');
});
}
@override
void initState() {
super.initState();
// start up if the Frame can be found
tryScanAndConnectAndStart(andRun: true);
}
@override
Future<void> run() async {
currentState = ApplicationState.running;
if (mounted) setState(() {});
try {
// Open the file picker
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['png'],
);
if (result != null) {
File file = File(result.files.single.path!);
// Read the file content
Uint8List pngBytes = await file.readAsBytes();
// Update the UI
setState(() {
_image = Image.memory(pngBytes);
});
// and send initial text to Frame
var msg = TxSprite.fromPngBytes(pngBytes: pngBytes);
await frame!.sendMessage(0x20, msg.pack());
}
else {
currentState = ApplicationState.ready;
if (mounted) setState(() {});
}
} catch (e) {
_log.fine('Error executing application logic: $e');
currentState = ApplicationState.ready;
if (mounted) setState(() {});
}
}
@override
Future<void> cancel() async {
// remove the displayed image
var msg = TxCode(value: 1);
await frame!.sendMessage(0x10, msg.pack());
_image = null;
currentState = ApplicationState.ready;
if (mounted) setState(() {});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Frame Sprite Viewer',
theme: ThemeData.dark(),
home: Scaffold(
appBar: AppBar(
title: const Text('Frame Sprite Viewer'),
actions: [getBatteryWidget()]
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Spacer(),
if (_image != null) _image!,
const Spacer(),
],
),
),
floatingActionButton: getFloatingActionButtonWidget(const Icon(Icons.file_open), const Icon(Icons.close)),
persistentFooterButtons: getFooterButtonsWidget(),
)
);
}
}