@@ -9,39 +9,39 @@ import 'package:image_picker/image_picker.dart' as picker;
99class 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 }
0 commit comments