I am new to flutter. I need to select image from gallery and need to display its name in text box and later along with text typed in text box i need to upload that image into server by pressing send button. How can I do this?
1 Answer
For selecting image from your gallery, you can use image_picker package. Below is a blog showing how to use it :
Now about getting the name of the file. After getting the image using the above package, you will be getting it in a file format by doing something like this :
File image = await ImagePicker.pickImage(source: ImageSource.gallery);
After that you can get the path of the file and the last path segment will give you the name of the file. Below is an example of how to achieve it :
File image = File("/storage/emulated/0/Android/data/my_app/files/Pictures/image_214.png"); // Replace the path string with any valid path on your device if you want to test it out. out
String fileName = file.path.split('/').last;
Further if you want to add the filename string to your textfield, add a TextEditingController
to your TextField and you will be able to add the filename to your textfield by accessing the text
property of that controller. Follow the link given below for an example on how to use TextEditingController :
https://flutter.dev/docs/cookbook/forms/retrieve-input
After that you can use the API of your backend to upload the image to your server along with the file name.