Skip to content

Commit 86778fe

Browse files
fix: Draw clipart crashing on android 10 (#1300)
1 parent 06bd85b commit 86778fe

File tree

1 file changed

+81
-64
lines changed

1 file changed

+81
-64
lines changed

lib/view/draw_badge_screen.dart

Lines changed: 81 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:badgemagic/bademagic_module/utils/byte_array_utils.dart';
12
import 'package:badgemagic/bademagic_module/utils/converters.dart';
23
import 'package:badgemagic/bademagic_module/utils/file_helper.dart';
34
import 'package:badgemagic/bademagic_module/utils/toast_utils.dart';
@@ -13,24 +14,29 @@ class DrawBadge extends StatefulWidget {
1314
final bool? isSavedCard;
1415
final bool? isSavedClipart;
1516
final List<List<int>>? badgeGrid;
16-
const DrawBadge(
17-
{super.key,
18-
this.filename,
19-
this.isSavedCard = false,
20-
this.isSavedClipart = false,
21-
this.badgeGrid});
17+
18+
const DrawBadge({
19+
super.key,
20+
this.filename,
21+
this.isSavedCard = false,
22+
this.isSavedClipart = false,
23+
this.badgeGrid,
24+
});
2225

2326
@override
2427
State<DrawBadge> createState() => _DrawBadgeState();
2528
}
2629

2730
class _DrawBadgeState extends State<DrawBadge> {
28-
var drawToggle = DrawBadgeProvider();
31+
late DrawBadgeProvider drawToggle;
2932

3033
@override
31-
void didChangeDependencies() {
32-
super.didChangeDependencies();
33-
_setLandscapeOrientation();
34+
void initState() {
35+
super.initState();
36+
drawToggle = DrawBadgeProvider();
37+
WidgetsBinding.instance.addPostFrameCallback((_) {
38+
_setLandscapeOrientation();
39+
});
3440
}
3541

3642
@override
@@ -39,27 +45,67 @@ class _DrawBadgeState extends State<DrawBadge> {
3945
super.dispose();
4046
}
4147

42-
void _resetPortraitOrientation() {
43-
SystemChrome.setPreferredOrientations([
44-
DeviceOrientation.portraitUp,
45-
DeviceOrientation.portraitDown,
46-
]);
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+
}
4768
}
4869

49-
void _setLandscapeOrientation() {
50-
SystemChrome.setPreferredOrientations([
51-
DeviceOrientation.landscapeRight,
52-
DeviceOrientation.landscapeLeft,
53-
]);
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+
}
5498
}
5599

56100
@override
57101
Widget build(BuildContext context) {
58-
FileHelper fileHelper = FileHelper();
59-
return WillPopScope(
60-
onWillPop: () async {
61-
_resetPortraitOrientation();
62-
return true; // Allows back navigation
102+
return PopScope(
103+
canPop: true,
104+
// ignore: deprecated_member_use
105+
onPopInvoked: (didPop) async {
106+
if (didPop) {
107+
await _resetPortraitOrientation();
108+
}
63109
},
64110
child: CommonScaffold(
65111
index: 1,
@@ -79,9 +125,7 @@ class _DrawBadgeState extends State<DrawBadge> {
79125
children: [
80126
Column(
81127
children: [
82-
SizedBox(
83-
width: 100,
84-
),
128+
const SizedBox(width: 100),
85129
BMBadge(
86130
providerInit: (provider) => drawToggle = provider,
87131
badgeGrid: widget.badgeGrid
@@ -110,7 +154,7 @@ class _DrawBadgeState extends State<DrawBadge> {
110154
Text(
111155
'Draw',
112156
style: TextStyle(
113-
color: drawToggle.isDrawing
157+
color: drawToggle.getIsDrawing()
114158
? colorPrimary
115159
: Colors.black,
116160
),
@@ -128,14 +172,14 @@ class _DrawBadgeState extends State<DrawBadge> {
128172
children: [
129173
Icon(
130174
Icons.delete,
131-
color: drawToggle.isDrawing
175+
color: drawToggle.getIsDrawing()
132176
? Colors.black
133177
: colorPrimary,
134178
),
135179
Text(
136180
'Erase',
137181
style: TextStyle(
138-
color: drawToggle.isDrawing
182+
color: drawToggle.getIsDrawing()
139183
? Colors.black
140184
: colorPrimary,
141185
),
@@ -151,46 +195,19 @@ class _DrawBadgeState extends State<DrawBadge> {
151195
},
152196
child: const Column(
153197
children: [
154-
Icon(
155-
Icons.refresh,
156-
color: Colors.black,
157-
),
158-
Text(
159-
'Reset',
160-
style: TextStyle(color: Colors.black),
161-
)
198+
Icon(Icons.refresh, color: Colors.black),
199+
Text('Reset',
200+
style: TextStyle(color: Colors.black))
162201
],
163202
),
164203
),
165204
TextButton(
166-
onPressed: () {
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-
widget.isSavedCard!
175-
? fileHelper.updateBadgeText(
176-
widget.filename!,
177-
hexString,
178-
)
179-
: widget.isSavedClipart!
180-
? fileHelper.updateClipart(
181-
widget.filename!, badgeGrid)
182-
: fileHelper.saveImage(
183-
drawToggle.getDrawViewGrid());
184-
fileHelper.generateClipartCache();
185-
ToastUtils()
186-
.showToast("Clipart Saved Successfully");
205+
onPressed: () async {
206+
await _saveImage();
187207
},
188208
child: const Column(
189209
children: [
190-
Icon(
191-
Icons.save,
192-
color: Colors.black,
193-
),
210+
Icon(Icons.save, color: Colors.black),
194211
Text('Save',
195212
style: TextStyle(color: Colors.black))
196213
],

0 commit comments

Comments
 (0)