Skip to content

Commit e59a847

Browse files
committed
refactoring
1 parent 4d6e24f commit e59a847

File tree

3 files changed

+63
-62
lines changed

3 files changed

+63
-62
lines changed

lib/io/src/ui/delete_project_dialog.dart

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,36 @@ class _DeleteProjectDialogState extends State<DeleteProjectDialog> {
2222
@override
2323
Widget build(BuildContext context) => AlertDialog(
2424
title: Text("Delete ${widget.name}"),
25-
actions: [_discardButton, _deleteButton],
25+
actions: const [
26+
DialogElevatedButton(text: 'Cancel'),
27+
DialogTextButton(text: 'Delete'),
28+
],
2629
content: const Text("Do you really want to delete your project?"),
2730
);
31+
}
32+
33+
class DialogTextButton extends StatelessWidget {
34+
final String text;
2835

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-
}
36+
const DialogTextButton({Key? key, required this.text}) : super(key: key);
3737

38-
ElevatedButton get _discardButton => ElevatedButton(
38+
@override
39+
Widget build(BuildContext context) => TextButton(
40+
style:
41+
ButtonStyle(foregroundColor: MaterialStateProperty.all(Colors.red)),
42+
onPressed: () => Navigator.of(context).pop(true),
43+
child: Text(text),
44+
);
45+
}
46+
47+
class DialogElevatedButton extends StatelessWidget {
48+
final String text;
49+
50+
const DialogElevatedButton({Key? key, required this.text}) : super(key: key);
51+
52+
@override
53+
Widget build(BuildContext context) => ElevatedButton(
3954
onPressed: () => Navigator.of(context).pop(false),
40-
child: const Text(
41-
"Cancel",
42-
style: TextStyle(color: Colors.white),
43-
),
55+
child: Text(text, style: const TextStyle(color: Colors.white)),
4456
);
4557
}
Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:flutter/material.dart';
2+
import 'package:paintroid/io/src/ui/delete_project_dialog.dart';
23

34
/// Returns [true] if user chose to discard changes or [null] if user
45
/// dismissed the dialog by tapping outside
@@ -19,27 +20,13 @@ class DiscardChangesDialog extends StatefulWidget {
1920
class _DiscardChangesDialogState extends State<DiscardChangesDialog> {
2021
@override
2122
Widget build(BuildContext context) {
22-
return AlertDialog(
23-
title: const Text("Discard changes"),
24-
actions: [_discardButton, _saveButton],
25-
content: const Text(
26-
"You have not saved your last changes. They will be lost!"),
27-
);
28-
}
29-
30-
TextButton get _discardButton {
31-
return TextButton(
32-
style:
33-
ButtonStyle(foregroundColor: MaterialStateProperty.all(Colors.red)),
34-
onPressed: () => Navigator.of(context).pop(true),
35-
child: const Text("Discard"),
36-
);
37-
}
38-
39-
ElevatedButton get _saveButton {
40-
return ElevatedButton(
41-
onPressed: () => Navigator.of(context).pop(false),
42-
child: const Text("Save", style: TextStyle(color: Colors.white)),
23+
return const AlertDialog(
24+
title: Text("Discard changes"),
25+
actions: [
26+
DialogTextButton(text: 'Discard'),
27+
DialogElevatedButton(text: 'Save'),
28+
],
29+
content: Text("You have not saved your last changes. They will be lost!"),
4330
);
4431
}
4532
}

lib/ui/landing_page.dart

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,6 @@ class _LandingPageState extends ConsumerState<LandingPage> {
5454
);
5555
}
5656

57-
Uint8List? _getProjectPreview(String? path) =>
58-
imageService.getProjectPreview(path).when(
59-
ok: (preview) => preview,
60-
err: (failure) {
61-
showToast(failure.message);
62-
return null;
63-
},
64-
);
65-
66-
Widget _getPreviewForLatestModifiedProject(Project? project) {
67-
Uint8List? img;
68-
if (project != null) {
69-
img = _getProjectPreview(project.imagePreviewPath);
70-
}
71-
return _ImagePreview(img: img, color: Colors.white54);
72-
}
73-
7457
void _clearCanvas() {
7558
ref.read(CanvasState.provider.notifier)
7659
..clearBackgroundImageAndResetDimensions()
@@ -124,8 +107,10 @@ class _LandingPageState extends ConsumerState<LandingPage> {
124107
_openProject(latestModifiedProject!, ioHandler);
125108
}
126109
},
127-
child: _getPreviewForLatestModifiedProject(
128-
latestModifiedProject,
110+
child: _ImagePreview(
111+
project: latestModifiedProject,
112+
imageService: imageService,
113+
color: Colors.white54,
129114
),
130115
),
131116
),
@@ -180,13 +165,11 @@ class _LandingPageState extends ConsumerState<LandingPage> {
180165
itemBuilder: (context, position) {
181166
if (position != 0) {
182167
Project project = snapshot.data![position];
183-
Uint8List? img =
184-
_getProjectPreview(project.imagePreviewPath);
185168
return Card(
186-
// margin: const EdgeInsets.all(5),
187169
child: ListTile(
188170
leading: _ImagePreview(
189-
img: img,
171+
project: project,
172+
imageService: imageService,
190173
width: 80,
191174
color: Colors.white,
192175
),
@@ -282,24 +265,43 @@ class _LandingPageFAB extends StatelessWidget {
282265
}
283266

284267
class _ImagePreview extends StatelessWidget {
285-
final Uint8List? img;
268+
final Project? project;
286269
final double? width;
287270
final Color color;
271+
final IImageService imageService;
288272

289-
const _ImagePreview({Key? key, this.img, this.width, required this.color})
290-
: super(key: key);
273+
const _ImagePreview({
274+
Key? key,
275+
this.width,
276+
required this.color,
277+
this.project,
278+
required this.imageService,
279+
}) : super(key: key);
291280

292281
ImageProvider _getProjectPreviewImageProvider(Uint8List img) =>
293282
Image.memory(img, fit: BoxFit.cover).image;
294283

284+
Uint8List? _getProjectPreview(String? path) =>
285+
imageService.getProjectPreview(path).when(
286+
ok: (preview) => preview,
287+
err: (failure) {
288+
showToast(failure.message);
289+
return null;
290+
},
291+
);
292+
295293
@override
296294
Widget build(BuildContext context) {
295+
Uint8List? img;
296+
if (project != null) {
297+
img = _getProjectPreview(project!.imagePreviewPath);
298+
}
297299
var imgPreview = BoxDecoration(color: color);
298300
if (img != null) {
299301
imgPreview = BoxDecoration(
300302
color: color,
301303
image: DecorationImage(
302-
image: _getProjectPreviewImageProvider(img!),
304+
image: _getProjectPreviewImageProvider(img),
303305
),
304306
);
305307
}

0 commit comments

Comments
 (0)