Skip to content

Commit 4d6e24f

Browse files
committed
refactoring
1 parent 1bfc4d4 commit 4d6e24f

File tree

4 files changed

+104
-73
lines changed

4 files changed

+104
-73
lines changed

lib/io/src/ui/delete_project_dialog.dart

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,20 @@ class _DeleteProjectDialogState extends State<DeleteProjectDialog> {
2626
content: const Text("Do you really want to delete your project?"),
2727
);
2828

29-
TextButton get _deleteButton => TextButton(
30-
style: TextButton.styleFrom(primary: Colors.red),
31-
onPressed: () => Navigator.of(context).pop(true),
32-
child: const Text("Delete"),
33-
);
29+
TextButton get _deleteButton {
30+
return TextButton(
31+
style:
32+
ButtonStyle(foregroundColor: MaterialStateProperty.all(Colors.red)),
33+
onPressed: () => Navigator.of(context).pop(true),
34+
child: const Text("Delete"),
35+
);
36+
}
3437

3538
ElevatedButton get _discardButton => ElevatedButton(
3639
onPressed: () => Navigator.of(context).pop(false),
37-
child: const Text("Cancel", style: TextStyle(color: Colors.white)),
40+
child: const Text(
41+
"Cancel",
42+
style: TextStyle(color: Colors.white),
43+
),
3844
);
3945
}

lib/io/src/ui/discard_changes_dialog.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class _DiscardChangesDialogState extends State<DiscardChangesDialog> {
2929

3030
TextButton get _discardButton {
3131
return TextButton(
32-
style: TextButton.styleFrom(primary: Colors.red),
32+
style:
33+
ButtonStyle(foregroundColor: MaterialStateProperty.all(Colors.red)),
3334
onPressed: () => Navigator.of(context).pop(true),
3435
child: const Text("Discard"),
3536
);

lib/ui/landing_page.dart

Lines changed: 83 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,12 @@ class _LandingPageState extends ConsumerState<LandingPage> {
6363
},
6464
);
6565

66-
ImageProvider _getProjectPreviewImageProvider(Uint8List img) => Image.memory(
67-
img,
68-
fit: BoxFit.cover,
69-
).image;
70-
71-
BoxDecoration _getPreviewForLatestModifiedProject(Project project) {
72-
Uint8List? img = _getProjectPreview(project.imagePreviewPath);
73-
if (img != null) {
74-
return BoxDecoration(
75-
color: Colors.white54,
76-
image: DecorationImage(
77-
image: _getProjectPreviewImageProvider(img),
78-
),
79-
);
66+
Widget _getPreviewForLatestModifiedProject(Project? project) {
67+
Uint8List? img;
68+
if (project != null) {
69+
img = _getProjectPreview(project.imagePreviewPath);
8070
}
81-
return const BoxDecoration(color: Colors.white54);
71+
return _ImagePreview(img: img, color: Colors.white54);
8272
}
8373

8474
void _clearCanvas() {
@@ -88,6 +78,11 @@ class _LandingPageState extends ConsumerState<LandingPage> {
8878
ref.read(WorkspaceState.provider.notifier).updateLastSavedCommandCount();
8979
}
9080

81+
void _openProject(Project project, IOHandler ioHandler) async {
82+
bool loaded = await _loadProject(ioHandler, project);
83+
if (loaded) _navigateToPocketPaint();
84+
}
85+
9186
@override
9287
Widget build(BuildContext context) {
9388
final db = ref.watch(ProjectDatabase.provider);
@@ -113,13 +108,8 @@ class _LandingPageState extends ConsumerState<LandingPage> {
113108
builder: (BuildContext context, AsyncSnapshot<List<Project>> snapshot) {
114109
if (snapshot.connectionState == ConnectionState.done &&
115110
snapshot.hasData) {
116-
BoxDecoration bigImg;
117111
if (snapshot.data!.isNotEmpty) {
118112
latestModifiedProject = snapshot.data![0];
119-
bigImg =
120-
_getPreviewForLatestModifiedProject(latestModifiedProject!);
121-
} else {
122-
bigImg = const BoxDecoration(color: Colors.white54);
123113
}
124114
return Column(
125115
children: [
@@ -131,15 +121,11 @@ class _LandingPageState extends ConsumerState<LandingPage> {
131121
child: InkWell(
132122
onTap: () async {
133123
if (latestModifiedProject != null) {
134-
bool loaded = await _loadProject(
135-
ioHandler,
136-
latestModifiedProject!,
137-
);
138-
if (loaded) _navigateToPocketPaint();
124+
_openProject(latestModifiedProject!, ioHandler);
139125
}
140126
},
141-
child: Container(
142-
decoration: bigImg,
127+
child: _getPreviewForLatestModifiedProject(
128+
latestModifiedProject,
143129
),
144130
),
145131
),
@@ -149,11 +135,7 @@ class _LandingPageState extends ConsumerState<LandingPage> {
149135
iconSize: 264,
150136
onPressed: () async {
151137
if (latestModifiedProject != null) {
152-
bool loaded = await _loadProject(
153-
ioHandler,
154-
latestModifiedProject!,
155-
);
156-
if (loaded) _navigateToPocketPaint();
138+
_openProject(latestModifiedProject!, ioHandler);
157139
}
158140
},
159141
icon: SvgPicture.asset(
@@ -198,27 +180,15 @@ class _LandingPageState extends ConsumerState<LandingPage> {
198180
itemBuilder: (context, position) {
199181
if (position != 0) {
200182
Project project = snapshot.data![position];
201-
BoxDecoration imagePreview;
202183
Uint8List? img =
203184
_getProjectPreview(project.imagePreviewPath);
204-
if (img != null) {
205-
imagePreview = BoxDecoration(
206-
color: Colors.white,
207-
image: DecorationImage(
208-
image: _getProjectPreviewImageProvider(img),
209-
),
210-
);
211-
} else {
212-
imagePreview =
213-
const BoxDecoration(color: Colors.white);
214-
}
215-
216185
return Card(
217186
// margin: const EdgeInsets.all(5),
218187
child: ListTile(
219-
leading: Container(
188+
leading: _ImagePreview(
189+
img: img,
220190
width: 80,
221-
decoration: imagePreview,
191+
color: Colors.white,
222192
),
223193
dense: false,
224194
title: Text(
@@ -234,11 +204,7 @@ class _LandingPageState extends ConsumerState<LandingPage> {
234204
project: project,
235205
),
236206
enabled: true,
237-
onTap: () async {
238-
bool loaded =
239-
await _loadProject(ioHandler, project);
240-
if (loaded) _navigateToPocketPaint();
241-
},
207+
onTap: () async => _openProject(project, ioHandler),
242208
),
243209
);
244210
}
@@ -263,11 +229,9 @@ class _LandingPageState extends ConsumerState<LandingPage> {
263229
floatingActionButton: Column(
264230
mainAxisAlignment: MainAxisAlignment.end,
265231
children: [
266-
FloatingActionButton(
267-
heroTag: "btn1",
268-
backgroundColor: const Color(0xFFFFAB08),
269-
foregroundColor: const Color(0xFFFFFFFF),
270-
child: const Icon(Icons.file_download),
232+
_LandingPageFAB(
233+
heroTag: "import_image",
234+
icon: Icons.file_download,
271235
onPressed: () async {
272236
final bool imageLoaded =
273237
await ioHandler.loadImage(context, this, false);
@@ -279,11 +243,9 @@ class _LandingPageState extends ConsumerState<LandingPage> {
279243
const SizedBox(
280244
height: 10,
281245
),
282-
FloatingActionButton(
283-
heroTag: "btn2",
284-
backgroundColor: const Color(0xFFFFAB08),
285-
foregroundColor: const Color(0xFFFFFFFF),
286-
child: const Icon(Icons.add),
246+
_LandingPageFAB(
247+
heroTag: "new_image",
248+
icon: Icons.add,
287249
onPressed: () async {
288250
_clearCanvas();
289251
_navigateToPocketPaint();
@@ -294,3 +256,62 @@ class _LandingPageState extends ConsumerState<LandingPage> {
294256
);
295257
}
296258
}
259+
260+
class _LandingPageFAB extends StatelessWidget {
261+
final String heroTag;
262+
final IconData icon;
263+
final VoidCallback onPressed;
264+
265+
const _LandingPageFAB({
266+
Key? key,
267+
required this.heroTag,
268+
required this.icon,
269+
required this.onPressed,
270+
}) : super(key: key);
271+
272+
@override
273+
Widget build(BuildContext context) {
274+
return FloatingActionButton(
275+
heroTag: heroTag,
276+
backgroundColor: const Color(0xFFFFAB08),
277+
foregroundColor: const Color(0xFFFFFFFF),
278+
child: Icon(icon),
279+
onPressed: () async => onPressed(),
280+
);
281+
}
282+
}
283+
284+
class _ImagePreview extends StatelessWidget {
285+
final Uint8List? img;
286+
final double? width;
287+
final Color color;
288+
289+
const _ImagePreview({Key? key, this.img, this.width, required this.color})
290+
: super(key: key);
291+
292+
ImageProvider _getProjectPreviewImageProvider(Uint8List img) =>
293+
Image.memory(img, fit: BoxFit.cover).image;
294+
295+
@override
296+
Widget build(BuildContext context) {
297+
var imgPreview = BoxDecoration(color: color);
298+
if (img != null) {
299+
imgPreview = BoxDecoration(
300+
color: color,
301+
image: DecorationImage(
302+
image: _getProjectPreviewImageProvider(img!),
303+
),
304+
);
305+
}
306+
if (width != null) {
307+
return Container(
308+
width: width!,
309+
decoration: imgPreview,
310+
);
311+
} else {
312+
return Container(
313+
decoration: imgPreview,
314+
);
315+
}
316+
}
317+
}

lib/ui/project_overflow_menu.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ class _ProjectOverFlowMenuState extends ConsumerState<ProjectOverflowMenu> {
3535
@override
3636
Widget build(BuildContext context) {
3737
final db = ref.watch(ProjectDatabase.provider);
38-
// db.when(data: (database) {this.database = database;}, error: error, loading: loading)
39-
db.whenData((value) => database = value);
38+
db.when(
39+
data: (value) => database = value,
40+
error: (err, stacktrace) => showToast("Error: $err"),
41+
loading: () {},
42+
);
4043

4144
return PopupMenuButton(
4245
color: Theme.of(context).colorScheme.background,
@@ -74,8 +77,8 @@ class _ProjectOverFlowMenuState extends ConsumerState<ProjectOverflowMenu> {
7477
final previewFile = File(widget.project.imagePreviewPath!);
7578
await previewFile.delete();
7679
}
77-
} catch (err, stacktrace) {
78-
showToast(stacktrace.toString());
80+
} catch (err) {
81+
showToast(err.toString());
7982
}
8083
if (widget.project.id != null) {
8184
await database.projectDAO.deleteProject(widget.project.id!);

0 commit comments

Comments
 (0)