Skip to content

Commit 5a24613

Browse files
committed
feat: Retain Last Finalized Image for ePaper After App Restart.
1 parent f26a6be commit 5a24613

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

lib/provider/image_loader.dart

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,67 @@
1+
import 'dart:io';
12
import 'dart:typed_data';
23

34
import 'package:flutter/material.dart';
45
import 'package:image/image.dart' as img;
56
import 'package:image_cropper/image_cropper.dart';
67
import 'package:image_picker/image_picker.dart';
8+
import 'package:path_provider/path_provider.dart';
79

810
class ImageLoader extends ChangeNotifier {
911
img.Image? image;
1012
final List<img.Image> processedImgs = List.empty(growable: true);
13+
bool isLoading = false;
1114

12-
void pickImage({required int width, required int height}) async {
15+
Future<bool> pickImage({required int width, required int height}) async {
1316
final ImagePicker picker = ImagePicker();
1417
final XFile? file = await picker.pickImage(source: ImageSource.gallery);
15-
if (file == null) return;
18+
if (file == null) return false;
1619

1720
final croppedFile = await ImageCropper().cropImage(
1821
sourcePath: file.path,
19-
aspectRatio:
20-
CropAspectRatio(ratioX: width.toDouble(), ratioY: height.toDouble()),
22+
aspectRatio: CropAspectRatio(
23+
ratioX: width.toDouble(),
24+
ratioY: height.toDouble(),
25+
),
2126
);
22-
if (croppedFile == null) return;
27+
if (croppedFile == null) return false;
2328

2429
processedImgs.clear();
2530
image = await img.decodeImageFile(croppedFile.path);
2631

2732
notifyListeners();
33+
return true;
34+
}
35+
36+
Future<void> saveFinalizedImageBytes(Uint8List bytes) async {
37+
final dir = await getApplicationDocumentsDirectory();
38+
final file = File('${dir.path}/last_finalized.png');
39+
await file.writeAsBytes(bytes);
40+
}
41+
42+
Future<void> loadFinalizedImage({
43+
required int width,
44+
required int height,
45+
}) async {
46+
isLoading = true;
47+
notifyListeners();
48+
49+
try {
50+
final dir = await getApplicationDocumentsDirectory();
51+
final file = File('${dir.path}/last_finalized.png');
52+
53+
if (await file.exists()) {
54+
final bytes = await file.readAsBytes();
55+
final decoded = img.decodeImage(bytes);
56+
if (decoded != null) {
57+
final resized = img.copyResize(decoded, width: width, height: height);
58+
image = resized;
59+
}
60+
}
61+
} finally {
62+
isLoading = false;
63+
notifyListeners();
64+
}
2865
}
2966

3067
Future<void> updateImage({

0 commit comments

Comments
 (0)