Hey I am using a method for picking photos for user profile picture like that
void _showPickOptionsDialog(BuildContext context, UserData userData) {
final user = Provider.of<Userforid>(context, listen: false);
showDialog(
context: context,
builder: (context) => AlertDialog(
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTile(
title: Text("Pick From Gallery"),
onTap: () {
_loadPicker(ImageSource.gallery, userData);
Navigator.pop(context);
},
),
ListTile(
title: Text("Pick from Camera"),
onTap: () {
_loadPicker(ImageSource.camera, userData);
Navigator.pop(context);
},
),
],
),
),
);
}
Future _loadPicker(ImageSource source, UserData userData) async {
final XFile file =
await picker.pickImage(source: source, imageQuality: 100);
if (this.mounted) {
// This checks if the widget is still in the tree
setState(() {
if (file != null) {
_cropImage(file, userData);
} else {
return null;
}
});
//Navigator.pop(context);
}
}
_cropImage(XFile picked, UserData userData) async {
final user = FirebaseAuth.instance.currentUser.uid;
File cropped = await ImageCropper.cropImage(
sourcePath: picked.path,
aspectRatioPresets: [
CropAspectRatioPreset.original,
CropAspectRatioPreset.ratio16x9,
CropAspectRatioPreset.ratio5x4
],
maxWidth: 800,
);
if (cropped != null) {
setState(() {
_pickedImage = cropped;
});
}
if (_pickedImage != null) {
final ref = FirebaseStorage.instance
.ref()
.child('user_profile_pictures')
.child(user + '.jpg');
await ref.putFile(_pickedImage);
final url = await ref.getDownloadURL();
_curenturl = url;
await DatbaseService(uid: user).updateUserData(
...);
}
}
And then I displaying the image like that
Container(
child: ClipRRect(
borderRadius:
BorderRadius.circular(60),
child: Container(
height: 110,
width: 110,
decoration: BoxDecoration(
color: Colors.white,
),
child: _pickedImage != null
? Image.file(
_pickedImage,
fit: BoxFit.cover,
)
: userData.url != null &&
userData.url !=
"profilepictureer"
? Image.network(
userData.url,
fit: BoxFit.cover,
)
: Image.asset(
'assets/profilepictureer.png') // Your widget is here when image is no available.
),
),
decoration: new BoxDecoration(
shape: BoxShape.circle,
border: new Border.all(
color: Colors.black,
width: 1)),
),
So a user picks a photo then he can choose if he wanna crop the image and then im creating a downloadurl which im uploading into cloud firestorm and finally uploading the image into firebase cloud storage.
When ever I wanna display the photo I just calling the url.
The problem that I have is that the image looks pixaled and not sharp.
Like this
Hope anyone can help.
My 2 cents
First, try downloading the image directly from the online storage to your computer to check if it was corrupted during the upload.
Next, use the CircleAvatar widget to check whether the problem is not with the displaying part.
CircleAvatar(
radius: 50.0,
backgroundImage: _pickedImage,
)
Related
how to preview an image is to be chosen from gallery or camera in flutter with the new version of image_picker where
PickedFile image = await _picker.getImage(...)
is replace with
final XFile? photo = await _picker.pickImage(source: ImageSource.camera);
so photo must be displayed when it is selected properly
You can do it by using dialog in flutter.
Here is how I dealt with it.
onLongPress: () {
showDialog(
context: context,
builder: (ctx) {
final width = ctx.getWidth(1);
final height = ctx.getHeight(1);
return AlertDialog(
content: Stack(
children: [
SizedBox(
width: width * 0.9,
height: height * 0.7,
child: Image.network(
food.imageUrl,
fit: BoxFit.fill,
),
),
GestureDetector(
onTap: () => Navigator.pop(context),
child: Padding(
padding: const EdgeInsets.all(3.0),
child: Container(
decoration: BoxDecoration(
color: Colors.black.withOpacity(0.5),
shape: BoxShape.circle),
child: Icon(
CupertinoIcons.xmark_circle,
color: Colors.white.withOpacity(0.9),
size: 40,
),
),
),
)
],
),
insetPadding: EdgeInsets.zero,
contentPadding: EdgeInsets.zero,
clipBehavior: Clip.antiAliasWithSaveLayer,
);
},
);
},
You can preview image which you select from gallery or camera as below :
Image.file(
File("file path will be here"), // You can access it with . operator and path property
fit: BoxFit.cover,
)
convert to file:
File newPhoto = File(photo.path);
Use:
Image.file(newPhoto);
I used image_pickers package much easier
late File imageFile = File("");
bool isloaded = false;
ElevatedButton(
onPressed: () => pickImageFromCamera(),)
Padding(
padding: const EdgeInsets.all(8.0),
child: !isloaded
? Image.network(
"https://upload.wikimedia.org/wikipedia/commons/thumb/a/ac/No_image_available.svg/2048px-No_image_available.svg.png")
: Image.file(imageFile)),
void pickImageFromCamera() async {
List<Media>? res = await ImagesPicker.openCamera(
pickType: PickType.image,
);
print(res);
if (res != null) {
print(res[0].path);
setState(() {
imageFile = File(res[0].path);
isloaded = true;
setState(() {});
});
} else {
setState(() {
isloaded = false;
});
}
}
I'm trying to find a solution for resizing an image.
The images are picked from a gallery or camera. The problem that I have Is that the image always is not sharp.
This Is How I pick the photo
void _showPickOptionsDialog(BuildContext context, UserData userData) {
final user = Provider.of<Userforid>(context, listen: false);
showDialog(
context: context,
builder: (context) => AlertDialog(
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTile(
title: Text("Pick From Gallery"),
onTap: () {
_loadPicker(ImageSource.gallery, userData);
Navigator.pop(context);
},
),
ListTile(
title: Text("Pick from Camera"),
onTap: () {
_loadPicker(ImageSource.camera, userData);
Navigator.pop(context);
},
),
],
),
),
);
}
Future _loadPicker(ImageSource source, UserData userData) async {
final XFile file =
await picker.pickImage(source: source, imageQuality: 100);
if (this.mounted) {
// This checks if the widget is still in the tree
setState(() {
if (file != null) {
_cropImage(file, userData);
} else {
return null;
}
});
//Navigator.pop(context);
}
}
_cropImage(XFile picked, UserData userData) async {
final user = FirebaseAuth.instance.currentUser.uid;
File cropped = await ImageCropper.cropImage(
sourcePath: picked.path,
aspectRatioPresets: [
CropAspectRatioPreset.original,
CropAspectRatioPreset.ratio16x9,
CropAspectRatioPreset.ratio5x4
],
maxWidth: 800,
);
if (cropped != null) {
setState(() {
_pickedImage = cropped;
});
}
if (_pickedImage != null) {
final ref = FirebaseStorage.instance
.ref()
.child('user_profile_pictures')
.child(user + '.jpg');
await ref.putFile(_pickedImage);
final url = await ref.getDownloadURL();
_curenturl = url;
await DatbaseService(uid: user).updateUserData(
...);
}
}
And this is how I display them
Container(
child: ClipRRect(
borderRadius:
BorderRadius.circular(60),
child: Container(
height: 110,
width: 110,
decoration: BoxDecoration(
color: Colors.white,
),
child: _pickedImage != null
? Image.file(
_pickedImage,
fit: BoxFit.cover,
)
: userData.url != null &&
userData.url !=
"profilepictureer"
? Image.network(
userData.url,
fit: BoxFit.cover,
)
: Image.asset(
'assets/profilepictureer.png') // Your widget is here when image is no available.
),
),
decoration: new BoxDecoration(
shape: BoxShape.circle,
border: new Border.all(
color: Colors.black,
width: 1)),
),
So the images are set as user profile photos. Probably a solution will be to upload rounded images like Instagram. So the question that I have is how can a user picks a normal photo from camera or gallery and upload it as rounded?
If not how to solve ghe problem then?
You can simply wrap the widget with a ClipOval with flutter.
Take a look at this doc: Flutter Document
or Try using BoxFit.fill
child: ClipRRect(
borderRadius: BorderRadius.circular(16),
child: Image.network(
userData.url,
fit: BoxFit.fill,
),
I wanted to add 3 circle image picker avatar and wanted to upload 3 different pictures in them but unfortunately not able to do that. I tried to do this by extracting the method and passing the image to the single extracted build method and in result the chosen one image is applied to all three avatars. Kindly help me get through it.
Below are the functions for picking image from gallery and camera :
File _image;
final Picker = ImagePicker();
_imgFromCamera() async {
final image = await Picker.pickImage(
source: ImageSource.camera, imageQuality: 50
);
setState(() {
_image = File(image.path);
});
}
_imgFromGallery() async {
final image = await Picker.pickImage(
source: ImageSource.gallery, imageQuality: 50
);
setState(() {
_image = File(image.path);
});
}
Next is function for bottom sheet for picking the image either from gallery or camera:
void _showPicker(context) {
showModalBottomSheet(
context: context,
builder: (BuildContext bc) {
return SafeArea(
child: Container(
child: new Wrap(
children: <Widget>[
new ListTile(
leading: new Icon(Icons.photo_library),
title: new Text('Photo Library'),
onTap: () {
_imgFromGallery();
Navigator.of(context).pop();
}),
new ListTile(
leading: new Icon(Icons.photo_camera),
title: new Text('Camera'),
onTap: () {
_imgFromCamera();
Navigator.of(context).pop();
},
),
],
),
),
);
}
);
}
And here is the extracted method that I want to use for once for all three images:
GestureDetector buildGestureDetector(BuildContext context) {
return GestureDetector(
onTap: () {
_showPicker(context);
},
child: CircleAvatar(
radius: 53,
backgroundColor: Colors.black,
child: _image != null
? ClipRRect(
borderRadius: BorderRadius.circular(50),
child: Image.file(
_image,
width: 100,
height: 100,
fit: BoxFit.fitHeight,
),
)
: Container(
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(50)),
width: 100,
height: 100,
child: Icon(
Icons.camera_alt,
color: Colors.grey[800],
),
),
),
);
}
}
And here is that extracted method is called inside widget :
Row(
children: [
Expanded(
child: buildGestureDetector(context),
),
],
),
Kindly help me with applying 3 different pictures using this one extracted method and all the functions used once so that I don't need to go through all this process for all three pictures again and again.
The easiest way to handle this is of course passing "which avatar" info down to _imgFromCamera or _imgFromGallery .
For example, if your 3 images have their own identities like 'A', 'B' and 'C',
onTap: () {
_showPicker(context, 'A');
},
Then pass it to the _showPicker.
void _showPicker(context, id) {
...
onTap: () {
_imgFromGallery(id); // Passed 'A' in this example
Navigator.of(context).pop();
}),
...
Then,
_imgFromGallery(id) async {
final image = await Picker.pickImage(
source: ImageSource.gallery, imageQuality: 50
);
// Depends on the id, decide where and how to save the image.
switch(id) {
case 'A': setState(...); break;
case 'B': saveIntoLocal(...); break;
case 'C': uploadToServer(...); break;
....
}
}
image_picker looks fine when i use it in emulator, when it's pressed it works properly but when i install it to my phone it does nothing when i pressed the button for image_picker no camera show just nothing change.
i think it has to be some persmission" things but i don't quite understand with it
method:
var selectedImagePath = ''.obs;
getImage(ImageSource ImageSource) async {
final pickedFile = await ImagePicker().pickImage(source: ImageSource);
if (pickedFile != null) {
selectedImagePath.value = pickedFile.path;
} else {
Get.snackbar('Error', 'No Image Selected');
}
}
display:
Scaffold(
body: SafeArea(
child: Column(
children: [
Obx(
() => Container(
width: double.infinity,
height: 400,
decoration: selectedImagePath == ''
? BoxDecoration(
color: Colors.grey,
)
: BoxDecoration(
color: Colors.grey,
image: DecorationImage(
image: FileImage(
File(
selectedImagePath.value,
),
),
fit: BoxFit.cover
),
),
),
),
SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () {
getImage(ImageSource.camera);
},
child: Text('Take a photo'),
),
ElevatedButton(
onPressed: () {
getImage(ImageSource.gallery);
},
child: Text('Take from gallery'),
),
],
),
],
),
),
);
This works for me
File? _imageFile;
Future<void> getImage(
ImageSource source) async {
XFile? pickedFile = await ImagePicker()
.pickImage(source: source);
if (pickedFile != null) {
File selected = File(pickedFile.path);
_imageFile = selected;
}
}
Let me know if it works for you.
Here is my code, I'm using Provider Library to manage it.
Step 1:
//File? _image;
String? imgString;
ImagePicker? imagePicker = ImagePicker();
Step 2: Widget Code
Future getImage() async {
final dynamic image =
await imagePicker?.pickImage(source: ImageSource.gallery);
setState(() {
if (image != null) {
YourProvider.myImage = File(image.path);
YourProvider.pickedImage = true;
imgString = YourBaseUrl + YourProvider.myImage!.path;
debugPrint("Image: $imgString");
}
});
}
Step 3: Provider
bool pickedImage = false;
File? myImage;
I cant handle with image_picker. When I select a photo, I should see the photo instead of the avatar icon, but I don't know why it doesn't work that way. I'm new in flutter and maybe it should be solved in a different way.
The orange circle should stay and the icon should change, any advice will be great for me
Widget _avatar() {
return const CircleAvatar(
radius: 83,
backgroundColor: Colors.orange,
child: CircleAvatar(
backgroundColor: Colors.white,
radius: 80,
child: Icon(Icons.person_outline, size: 60, color: Colors.orange),
),
);
}
File? _image;
final picker = ImagePicker();
Future getImage() async {
final pickedFile = await picker.getImage(
source: ImageSource.gallery, maxWidth: 1800, maxHeight: 1800);
setState(() {
if (pickedFile != null) {
_image = File(pickedFile.path);
} else {
print('No image selected.');
}
});
}
Future getCameraImage() async {
final pickedFile = await picker.getImage(
source: ImageSource.camera,
maxWidth: 1800,
maxHeight: 1800,
);
setState(() {
if (pickedFile != null) {
_image = File(pickedFile.path);
} else {
print('No image selected.');
}
});
}
void _showPicker(BuildContext context) {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext bc) {
return SafeArea(
child: Container(
child: Wrap(
children: <Widget>[
ListTile(
leading: new Icon(Icons.photo_size_select_large_sharp),
title: new Text('Photo Library'),
onTap: () {
getImage();
Navigator.of(context).pop();
}),
ListTile(
leading: new Icon(Icons.photo_camera),
title: new Text('Camera'),
onTap: () {
getCameraImage();
Navigator.of(context).pop();
},
),
],
),
),
);
});
}
Widget _changeAvatarButton(BuildContext context) {
return Column(
children: [
CircleAvatar(
radius: 83,
backgroundColor: Colors.orange,
child: _image == null ? _avatar() : Image.file(_image!),
),
CustomButtonText(
onPressed: () {
_showPicker(context);
},
title: 'photo',
textColor: Colors.teal),
],
);
}
Change Your CircleAvtar widget with the below one and if you don't want to show the orange circle then set the backgroundColor: _image == null ? Colors.orange : Colors.transparent.
CircleAvatar(
radius: 83,
backgroundColor: Colors.orange,
child: _image == null
? _avatar()
: ClipRRect(
borderRadius: BorderRadius.circular(83),
child: Image.file(
_image!,
height: 160, // Change the size up or down accordingly border radius
width: 160, // Change the size up or down accordingly border radius
fit: BoxFit.cover,
)),
),
It looks all fine with the state update and the image picking. What you should be paying attention at is the async functions, because you need to wait for the images before updating your widgets, and getCameraImage() and getImage() are a Future. Check if adding await solves the problem.
ListTile(
leading: new Icon(Icons.photo_size_select_large_sharp),
title: new Text('Photo Library'),
onTap: () async {
await getImage();
Navigator.of(context).pop();
}),
ListTile(
leading: new Icon(Icons.photo_camera),
title: new Text('Camera'),
onTap: () async {
await getCameraImage();
Navigator.of(context).pop();
},
),