@@ -3,49 +3,64 @@ import 'dart:io';
33import 'package:celenganku_app_clone/features/add_wish/add_wish.dart' ;
44import 'package:flutter/material.dart' ;
55import 'package:flutter_bloc/flutter_bloc.dart' ;
6- import 'package:image_cropper/image_cropper.dart' as cropper ;
7- import 'package:image_picker/image_picker.dart' as picker ;
6+ import 'package:image_cropper/image_cropper.dart' ;
7+ import 'package:image_picker/image_picker.dart' ;
88
9- class ImagePicker extends StatelessWidget {
10- const ImagePicker ({super .key});
9+ class ImagePickerWidget extends StatelessWidget {
10+ const ImagePickerWidget ({super .key});
11+
12+ static ImagePicker imagePicker = ImagePicker ();
13+ static ImageCropper imageCropper = ImageCropper ();
1114
1215 /// Get from camera
1316 Future <String ?> _getFromCamera () async {
14- picker.ImagePicker imagePicker = picker.ImagePicker ();
15-
16- picker.XFile ? image = await imagePicker.pickImage (source: picker.ImageSource .camera);
17+ try {
18+ XFile ? image = await imagePicker.pickImage (source: ImageSource .camera);
1719
18- if (image != null ) {
19- return await _cropImage (filePath: image.path);
20+ return image? .path;
21+ } catch (e) {
22+ debugPrint (e.toString ());
23+ return null ;
2024 }
21- return null ;
2225 }
2326
2427 /// Get from gallery
2528 Future <String ?> _getFromGallery () async {
26- picker.ImagePicker imagePicker = picker.ImagePicker ();
27-
28- picker.XFile ? image = await imagePicker.pickImage (source: picker.ImageSource .gallery);
29+ try {
30+ XFile ? image = await imagePicker.pickImage (source: ImageSource .gallery);
2931
30- if (image != null ) {
31- return await _cropImage (filePath: image.path);
32+ return image? .path;
33+ } catch (e) {
34+ debugPrint (e.toString ());
35+ return null ;
3236 }
33- return null ;
3437 }
3538
3639 /// Crop Image
37- Future <String ?> _cropImage ({required filePath}) async {
38- cropper.CroppedFile ? croppedImage = await cropper.ImageCropper ().cropImage (
39- sourcePath: filePath,
40- maxWidth: 1280 ,
41- aspectRatio: const cropper.CropAspectRatio (ratioX: 16 , ratioY: 9 ),
42- uiSettings: [
43- cropper.AndroidUiSettings (toolbarTitle: 'Pangkas Foto' ),
44- cropper.IOSUiSettings (title: 'Pangkas Foto' ),
45- ],
46- );
40+ Future <String ?> _cropImage (BuildContext context, {required filePath}) async {
41+ try {
42+ CroppedFile ? croppedImage = await imageCropper.cropImage (
43+ sourcePath: filePath,
44+ maxWidth: 1280 ,
45+ aspectRatio: const CropAspectRatio (ratioX: 16 , ratioY: 9 ),
46+ uiSettings: [
47+ AndroidUiSettings (toolbarTitle: 'Pangkas Foto' ),
48+ IOSUiSettings (title: 'Pangkas Foto' ),
49+ WebUiSettings (
50+ context: context,
51+ viewPort: const CroppieViewPort (
52+ width: 1280 ,
53+ height: 720 ,
54+ ),
55+ ),
56+ ],
57+ );
4758
48- return croppedImage? .path;
59+ return croppedImage? .path;
60+ } catch (e) {
61+ debugPrint (e.toString ());
62+ return filePath;
63+ }
4964 }
5065
5166 @override
@@ -71,14 +86,22 @@ class ImagePicker extends StatelessWidget {
7186 ),
7287 child: SizedBox .expand (
7388 child: Material (
74- shape: RoundedRectangleBorder (borderRadius: BorderRadius .circular (10 )),
75- color: imagePath != null ? Colors .transparent : theme.colorScheme.surfaceVariant,
89+ shape: RoundedRectangleBorder (
90+ borderRadius: BorderRadius .circular (10 ),
91+ ),
92+ color: imagePath != null
93+ ? Colors .transparent
94+ : theme.colorScheme.surfaceVariant,
7695 child: InkWell (
7796 borderRadius: BorderRadius .circular (10 ),
7897 onTap: () async {
7998 await showModalBottomSheet <void >(
8099 context: context,
81- shape: const RoundedRectangleBorder (borderRadius: BorderRadius .vertical (top: Radius .circular (25 ))),
100+ shape: const RoundedRectangleBorder (
101+ borderRadius: BorderRadius .vertical (
102+ top: Radius .circular (25 ),
103+ ),
104+ ),
82105 builder: (_) => _selectImageSourceModal (context),
83106 );
84107 },
@@ -117,12 +140,16 @@ class ImagePicker extends StatelessWidget {
117140 titleText: 'Kamera' ,
118141 iconData: Icons .camera_alt_outlined,
119142 onTap: () async {
120- String ? path = await _getFromCamera ();
143+ String ? path = await _getFromCamera ().then (
144+ (path) async => await _cropImage (context, filePath: path),
145+ );
121146
122147 if (path == null ) return ;
123148
124149 if (context.mounted) {
125- context.read <AddWishBloc >().add (WishImageChanged (imagePath: path));
150+ context
151+ .read <AddWishBloc >()
152+ .add (WishImageChanged (imagePath: path));
126153 Navigator .pop (context);
127154 }
128155 },
@@ -131,12 +158,16 @@ class ImagePicker extends StatelessWidget {
131158 titleText: 'Gallery' ,
132159 iconData: Icons .photo_library_outlined,
133160 onTap: () async {
134- String ? path = await _getFromGallery ();
161+ String ? path = await _getFromGallery ().then (
162+ (path) async => await _cropImage (context, filePath: path),
163+ );
135164
136165 if (path == null ) return ;
137166
138167 if (context.mounted) {
139- context.read <AddWishBloc >().add (WishImageChanged (imagePath: path));
168+ context
169+ .read <AddWishBloc >()
170+ .add (WishImageChanged (imagePath: path));
140171 Navigator .pop (context);
141172 }
142173 },
@@ -168,7 +199,8 @@ class _ImageSourceListTile extends StatelessWidget {
168199 foregroundColor: theme.colorScheme.onPrimary,
169200 child: Icon (iconData),
170201 ),
171- title: Text (titleText, style: const TextStyle (fontWeight: FontWeight .bold)),
202+ title:
203+ Text (titleText, style: const TextStyle (fontWeight: FontWeight .bold)),
172204 onTap: onTap,
173205 );
174206 }
0 commit comments