1
- import 'package:badgemagic/bademagic_module/utils/byte_array_utils.dart' ;
2
1
import 'package:badgemagic/bademagic_module/utils/converters.dart' ;
3
2
import 'package:badgemagic/bademagic_module/utils/file_helper.dart' ;
4
3
import 'package:badgemagic/bademagic_module/utils/toast_utils.dart' ;
@@ -14,7 +13,6 @@ class DrawBadge extends StatefulWidget {
14
13
final bool ? isSavedCard;
15
14
final bool ? isSavedClipart;
16
15
final List <List <int >>? badgeGrid;
17
-
18
16
const DrawBadge ({
19
17
super .key,
20
18
this .filename,
@@ -28,15 +26,12 @@ class DrawBadge extends StatefulWidget {
28
26
}
29
27
30
28
class _DrawBadgeState extends State <DrawBadge > {
31
- late DrawBadgeProvider drawToggle;
29
+ var drawToggle = DrawBadgeProvider () ;
32
30
33
31
@override
34
- void initState () {
35
- super .initState ();
36
- drawToggle = DrawBadgeProvider ();
37
- WidgetsBinding .instance.addPostFrameCallback ((_) {
38
- _setLandscapeOrientation ();
39
- });
32
+ void didChangeDependencies () {
33
+ super .didChangeDependencies ();
34
+ _setLandscapeOrientation ();
40
35
}
41
36
42
37
@override
@@ -45,67 +40,27 @@ class _DrawBadgeState extends State<DrawBadge> {
45
40
super .dispose ();
46
41
}
47
42
48
- Future <void > _resetPortraitOrientation () async {
49
- try {
50
- await SystemChrome .setPreferredOrientations ([
51
- DeviceOrientation .portraitUp,
52
- DeviceOrientation .portraitDown,
53
- ]);
54
- } catch (e) {
55
- logger.e ('Error setting portrait orientation' , error: e);
56
- }
57
- }
58
-
59
- Future <void > _setLandscapeOrientation () async {
60
- try {
61
- await SystemChrome .setPreferredOrientations ([
62
- DeviceOrientation .landscapeRight,
63
- DeviceOrientation .landscapeLeft,
64
- ]);
65
- } catch (e) {
66
- logger.e ('Error setting landscape orientation' , error: e);
67
- }
43
+ void _resetPortraitOrientation () {
44
+ SystemChrome .setPreferredOrientations ([
45
+ DeviceOrientation .portraitUp,
46
+ DeviceOrientation .portraitDown,
47
+ ]);
68
48
}
69
49
70
- Future <void > _saveImage () async {
71
- try {
72
- List <List <int >> badgeGrid = drawToggle
73
- .getDrawViewGrid ()
74
- .map ((e) => e.map ((e) => e ? 1 : 0 ).toList ())
75
- .toList ();
76
- List <String > hexString =
77
- Converters .convertBitmapToLEDHex (badgeGrid, false );
78
-
79
- if (widget.isSavedCard == true ) {
80
- await FileHelper ().updateBadgeText (
81
- widget.filename ?? '' ,
82
- hexString,
83
- );
84
- } else if (widget.isSavedClipart == true ) {
85
- await FileHelper ().updateClipart (
86
- widget.filename ?? '' ,
87
- badgeGrid,
88
- );
89
- } else {
90
- await FileHelper ().saveImage (drawToggle.getDrawViewGrid ());
91
- }
92
-
93
- await FileHelper ().generateClipartCache ();
94
- ToastUtils ().showToast ("Clipart Saved Successfully" );
95
- } catch (e) {
96
- logger.e ('Error saving image' , error: e);
97
- }
50
+ void _setLandscapeOrientation () {
51
+ SystemChrome .setPreferredOrientations ([
52
+ DeviceOrientation .landscapeRight,
53
+ DeviceOrientation .landscapeLeft,
54
+ ]);
98
55
}
99
56
100
57
@override
101
58
Widget build (BuildContext context) {
102
- return PopScope (
103
- canPop: true ,
104
- // ignore: deprecated_member_use
105
- onPopInvoked: (didPop) async {
106
- if (didPop) {
107
- await _resetPortraitOrientation ();
108
- }
59
+ FileHelper fileHelper = FileHelper ();
60
+ return WillPopScope (
61
+ onWillPop: () async {
62
+ _resetPortraitOrientation ();
63
+ return true ;
109
64
},
110
65
child: CommonScaffold (
111
66
index: 1 ,
@@ -154,7 +109,7 @@ class _DrawBadgeState extends State<DrawBadge> {
154
109
Text (
155
110
'Draw' ,
156
111
style: TextStyle (
157
- color: drawToggle.getIsDrawing ()
112
+ color: drawToggle.isDrawing
158
113
? colorPrimary
159
114
: Colors .black,
160
115
),
@@ -172,14 +127,14 @@ class _DrawBadgeState extends State<DrawBadge> {
172
127
children: [
173
128
Icon (
174
129
Icons .delete,
175
- color: drawToggle.getIsDrawing ()
130
+ color: drawToggle.isDrawing
176
131
? Colors .black
177
132
: colorPrimary,
178
133
),
179
134
Text (
180
135
'Erase' ,
181
136
style: TextStyle (
182
- color: drawToggle.getIsDrawing ()
137
+ color: drawToggle.isDrawing
183
138
? Colors .black
184
139
: colorPrimary,
185
140
),
@@ -195,19 +150,62 @@ class _DrawBadgeState extends State<DrawBadge> {
195
150
},
196
151
child: const Column (
197
152
children: [
198
- Icon (Icons .refresh, color: Colors .black),
199
- Text ('Reset' ,
200
- style: TextStyle (color: Colors .black))
153
+ Icon (
154
+ Icons .refresh,
155
+ color: Colors .black,
156
+ ),
157
+ Text (
158
+ 'Reset' ,
159
+ style: TextStyle (color: Colors .black),
160
+ )
201
161
],
202
162
),
203
163
),
204
164
TextButton (
205
165
onPressed: () async {
206
- await _saveImage ();
166
+ try {
167
+ List <List <int >> badgeGrid = drawToggle
168
+ .getDrawViewGrid ()
169
+ .map ((e) => e.map ((e) => e ? 1 : 0 ).toList ())
170
+ .toList ();
171
+ List <String > hexString =
172
+ Converters .convertBitmapToLEDHex (
173
+ badgeGrid, false );
174
+
175
+ if (widget.isSavedCard! ) {
176
+ await fileHelper.updateBadgeText (
177
+ widget.filename! , hexString);
178
+ } else if (widget.isSavedClipart! ) {
179
+ await fileHelper.updateClipart (
180
+ widget.filename! , badgeGrid);
181
+ } else {
182
+ await fileHelper
183
+ .saveImage (drawToggle.getDrawViewGrid ());
184
+ }
185
+
186
+ await fileHelper.generateClipartCache ();
187
+
188
+ ToastUtils ()
189
+ .showToast ("Clipart Saved Successfully" );
190
+
191
+ await Future .delayed (
192
+ const Duration (milliseconds: 800 ));
193
+
194
+ if (mounted) {
195
+ Navigator .of (context)
196
+ .popUntil ((route) => route.isFirst);
197
+ }
198
+ } catch (e) {
199
+ ToastUtils ().showToast (
200
+ "Failed to save badge: ${e .toString ()}" );
201
+ }
207
202
},
208
203
child: const Column (
209
204
children: [
210
- Icon (Icons .save, color: Colors .black),
205
+ Icon (
206
+ Icons .save,
207
+ color: Colors .black,
208
+ ),
211
209
Text ('Save' ,
212
210
style: TextStyle (color: Colors .black))
213
211
],
0 commit comments