Skip to content

Commit ed43ed5

Browse files
committed
fix: Add image cropper web support
1 parent d6a5c7b commit ed43ed5

File tree

2 files changed

+59
-28
lines changed

2 files changed

+59
-28
lines changed

lib/features/add_wish/widget/image_picker.dart

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,39 @@ import 'package:image_picker/image_picker.dart' as picker;
99
class ImagePicker extends StatelessWidget {
1010
const ImagePicker({super.key});
1111

12+
static picker.ImagePicker imagePicker = picker.ImagePicker();
13+
1214
/// Get from camera
1315
Future<String?> _getFromCamera() async {
14-
picker.ImagePicker imagePicker = picker.ImagePicker();
15-
16-
picker.XFile? image = await imagePicker.pickImage(source: picker.ImageSource.camera);
16+
picker.XFile? image = await imagePicker.pickImage(
17+
source: picker.ImageSource.camera,
18+
);
1719

18-
if (image != null) {
19-
return await _cropImage(filePath: image.path);
20-
}
21-
return null;
20+
return image?.path;
2221
}
2322

2423
/// Get from gallery
2524
Future<String?> _getFromGallery() async {
26-
picker.ImagePicker imagePicker = picker.ImagePicker();
27-
28-
picker.XFile? image = await imagePicker.pickImage(source: picker.ImageSource.gallery);
25+
picker.XFile? image = await imagePicker.pickImage(
26+
source: picker.ImageSource.gallery,
27+
);
2928

30-
if (image != null) {
31-
return await _cropImage(filePath: image.path);
32-
}
33-
return null;
29+
return image?.path;
3430
}
3531

3632
/// Crop Image
37-
Future<String?> _cropImage({required filePath}) async {
33+
Future<String?> _cropImage(BuildContext context, {required filePath}) async {
34+
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
35+
return filePath;
36+
}
3837
cropper.CroppedFile? croppedImage = await cropper.ImageCropper().cropImage(
3938
sourcePath: filePath,
4039
maxWidth: 1280,
4140
aspectRatio: const cropper.CropAspectRatio(ratioX: 16, ratioY: 9),
4241
uiSettings: [
4342
cropper.AndroidUiSettings(toolbarTitle: 'Pangkas Foto'),
4443
cropper.IOSUiSettings(title: 'Pangkas Foto'),
44+
cropper.WebUiSettings(context: context),
4545
],
4646
);
4747

@@ -71,14 +71,22 @@ class ImagePicker extends StatelessWidget {
7171
),
7272
child: SizedBox.expand(
7373
child: Material(
74-
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
75-
color: imagePath != null ? Colors.transparent : theme.colorScheme.surfaceVariant,
74+
shape: RoundedRectangleBorder(
75+
borderRadius: BorderRadius.circular(10),
76+
),
77+
color: imagePath != null
78+
? Colors.transparent
79+
: theme.colorScheme.surfaceVariant,
7680
child: InkWell(
7781
borderRadius: BorderRadius.circular(10),
7882
onTap: () async {
7983
await showModalBottomSheet<void>(
8084
context: context,
81-
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.vertical(top: Radius.circular(25))),
85+
shape: const RoundedRectangleBorder(
86+
borderRadius: BorderRadius.vertical(
87+
top: Radius.circular(25),
88+
),
89+
),
8290
builder: (_) => _selectImageSourceModal(context),
8391
);
8492
},
@@ -117,12 +125,22 @@ class ImagePicker extends StatelessWidget {
117125
titleText: 'Kamera',
118126
iconData: Icons.camera_alt_outlined,
119127
onTap: () async {
120-
String? path = await _getFromCamera();
128+
if (!Platform.isAndroid && !Platform.isIOS) {
129+
ScaffoldMessenger.of(context).showSnackBar(
130+
const SnackBar(content: Text('Not Supported')),
131+
);
132+
return;
133+
}
134+
String? path = await _getFromCamera().then(
135+
(path) async => await _cropImage(context, filePath: path),
136+
);
121137

122138
if (path == null) return;
123139

124140
if (context.mounted) {
125-
context.read<AddWishBloc>().add(WishImageChanged(imagePath: path));
141+
context
142+
.read<AddWishBloc>()
143+
.add(WishImageChanged(imagePath: path));
126144
Navigator.pop(context);
127145
}
128146
},
@@ -131,12 +149,16 @@ class ImagePicker extends StatelessWidget {
131149
titleText: 'Gallery',
132150
iconData: Icons.photo_library_outlined,
133151
onTap: () async {
134-
String? path = await _getFromGallery();
152+
String? path = await _getFromGallery().then(
153+
(path) async => await _cropImage(context, filePath: path),
154+
);
135155

136156
if (path == null) return;
137157

138158
if (context.mounted) {
139-
context.read<AddWishBloc>().add(WishImageChanged(imagePath: path));
159+
context
160+
.read<AddWishBloc>()
161+
.add(WishImageChanged(imagePath: path));
140162
Navigator.pop(context);
141163
}
142164
},
@@ -168,7 +190,8 @@ class _ImageSourceListTile extends StatelessWidget {
168190
foregroundColor: theme.colorScheme.onPrimary,
169191
child: Icon(iconData),
170192
),
171-
title: Text(titleText, style: const TextStyle(fontWeight: FontWeight.bold)),
193+
title:
194+
Text(titleText, style: const TextStyle(fontWeight: FontWeight.bold)),
172195
onTap: onTap,
173196
);
174197
}

web/index.html

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<!DOCTYPE html>
22
<html>
3+
34
<head>
45
<!--
56
If you are serving your web app in a path other than the root, change the
@@ -27,33 +28,40 @@
2728
<link rel="apple-touch-icon" href="icons/Icon-192.png">
2829

2930
<!-- Favicon -->
30-
<link rel="icon" type="image/png" href="favicon.png"/>
31+
<link rel="icon" type="image/png" href="favicon.png" />
3132

3233
<title>celenganku_app_clone</title>
3334
<link rel="manifest" href="manifest.json">
3435

36+
<!-- Croppie -->
37+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/croppie/2.6.5/croppie.css" />
38+
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/exif-js/2.3.0/exif.js"></script>
39+
<script src="https://cdnjs.cloudflare.com/ajax/libs/croppie/2.6.5/croppie.min.js"></script>
40+
3541
<script>
3642
// The value below is injected by flutter build, do not touch.
3743
var serviceWorkerVersion = null;
3844
</script>
3945
<!-- This script adds the flutter initialization JS code -->
4046
<script src="flutter.js" defer></script>
4147
</head>
48+
4249
<body>
4350
<script>
44-
window.addEventListener('load', function(ev) {
51+
window.addEventListener('load', function (ev) {
4552
// Download main.dart.js
4653
_flutter.loader.loadEntrypoint({
4754
serviceWorker: {
4855
serviceWorkerVersion: serviceWorkerVersion,
4956
},
50-
onEntrypointLoaded: function(engineInitializer) {
51-
engineInitializer.initializeEngine().then(function(appRunner) {
57+
onEntrypointLoaded: function (engineInitializer) {
58+
engineInitializer.initializeEngine().then(function (appRunner) {
5259
appRunner.runApp();
5360
});
5461
}
5562
});
5663
});
5764
</script>
5865
</body>
59-
</html>
66+
67+
</html>

0 commit comments

Comments
 (0)