Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeChamp-SS committed Oct 31, 2022
1 parent 4d6e24f commit e59a847
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 62 deletions.
40 changes: 26 additions & 14 deletions lib/io/src/ui/delete_project_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,36 @@ class _DeleteProjectDialogState extends State<DeleteProjectDialog> {
@override
Widget build(BuildContext context) => AlertDialog(
title: Text("Delete ${widget.name}"),
actions: [_discardButton, _deleteButton],
actions: const [
DialogElevatedButton(text: 'Cancel'),
DialogTextButton(text: 'Delete'),
],
content: const Text("Do you really want to delete your project?"),
);
}

class DialogTextButton extends StatelessWidget {
final String text;

TextButton get _deleteButton {
return TextButton(
style:
ButtonStyle(foregroundColor: MaterialStateProperty.all(Colors.red)),
onPressed: () => Navigator.of(context).pop(true),
child: const Text("Delete"),
);
}
const DialogTextButton({Key? key, required this.text}) : super(key: key);

ElevatedButton get _discardButton => ElevatedButton(
@override
Widget build(BuildContext context) => TextButton(
style:
ButtonStyle(foregroundColor: MaterialStateProperty.all(Colors.red)),
onPressed: () => Navigator.of(context).pop(true),
child: Text(text),
);
}

class DialogElevatedButton extends StatelessWidget {
final String text;

const DialogElevatedButton({Key? key, required this.text}) : super(key: key);

@override
Widget build(BuildContext context) => ElevatedButton(
onPressed: () => Navigator.of(context).pop(false),
child: const Text(
"Cancel",
style: TextStyle(color: Colors.white),
),
child: Text(text, style: const TextStyle(color: Colors.white)),
);
}
29 changes: 8 additions & 21 deletions lib/io/src/ui/discard_changes_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:paintroid/io/src/ui/delete_project_dialog.dart';

/// Returns [true] if user chose to discard changes or [null] if user
/// dismissed the dialog by tapping outside
Expand All @@ -19,27 +20,13 @@ class DiscardChangesDialog extends StatefulWidget {
class _DiscardChangesDialogState extends State<DiscardChangesDialog> {
@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text("Discard changes"),
actions: [_discardButton, _saveButton],
content: const Text(
"You have not saved your last changes. They will be lost!"),
);
}

TextButton get _discardButton {
return TextButton(
style:
ButtonStyle(foregroundColor: MaterialStateProperty.all(Colors.red)),
onPressed: () => Navigator.of(context).pop(true),
child: const Text("Discard"),
);
}

ElevatedButton get _saveButton {
return ElevatedButton(
onPressed: () => Navigator.of(context).pop(false),
child: const Text("Save", style: TextStyle(color: Colors.white)),
return const AlertDialog(
title: Text("Discard changes"),
actions: [
DialogTextButton(text: 'Discard'),
DialogElevatedButton(text: 'Save'),
],
content: Text("You have not saved your last changes. They will be lost!"),
);
}
}
56 changes: 29 additions & 27 deletions lib/ui/landing_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,6 @@ class _LandingPageState extends ConsumerState<LandingPage> {
);
}

Uint8List? _getProjectPreview(String? path) =>
imageService.getProjectPreview(path).when(
ok: (preview) => preview,
err: (failure) {
showToast(failure.message);
return null;
},
);

Widget _getPreviewForLatestModifiedProject(Project? project) {
Uint8List? img;
if (project != null) {
img = _getProjectPreview(project.imagePreviewPath);
}
return _ImagePreview(img: img, color: Colors.white54);
}

void _clearCanvas() {
ref.read(CanvasState.provider.notifier)
..clearBackgroundImageAndResetDimensions()
Expand Down Expand Up @@ -124,8 +107,10 @@ class _LandingPageState extends ConsumerState<LandingPage> {
_openProject(latestModifiedProject!, ioHandler);
}
},
child: _getPreviewForLatestModifiedProject(
latestModifiedProject,
child: _ImagePreview(
project: latestModifiedProject,
imageService: imageService,
color: Colors.white54,
),
),
),
Expand Down Expand Up @@ -180,13 +165,11 @@ class _LandingPageState extends ConsumerState<LandingPage> {
itemBuilder: (context, position) {
if (position != 0) {
Project project = snapshot.data![position];
Uint8List? img =
_getProjectPreview(project.imagePreviewPath);
return Card(
// margin: const EdgeInsets.all(5),
child: ListTile(
leading: _ImagePreview(
img: img,
project: project,
imageService: imageService,
width: 80,
color: Colors.white,
),
Expand Down Expand Up @@ -282,24 +265,43 @@ class _LandingPageFAB extends StatelessWidget {
}

class _ImagePreview extends StatelessWidget {
final Uint8List? img;
final Project? project;
final double? width;
final Color color;
final IImageService imageService;

const _ImagePreview({Key? key, this.img, this.width, required this.color})
: super(key: key);
const _ImagePreview({
Key? key,
this.width,
required this.color,
this.project,
required this.imageService,
}) : super(key: key);

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

Uint8List? _getProjectPreview(String? path) =>
imageService.getProjectPreview(path).when(
ok: (preview) => preview,
err: (failure) {
showToast(failure.message);
return null;
},
);

@override
Widget build(BuildContext context) {
Uint8List? img;
if (project != null) {
img = _getProjectPreview(project!.imagePreviewPath);
}
var imgPreview = BoxDecoration(color: color);
if (img != null) {
imgPreview = BoxDecoration(
color: color,
image: DecorationImage(
image: _getProjectPreviewImageProvider(img!),
image: _getProjectPreviewImageProvider(img),
),
);
}
Expand Down

0 comments on commit e59a847

Please sign in to comment.