I am new here and I am trying all I can to explain with respect to the regulation here. I have a flutter image upload for a single image at a time but I am looking for a way around how to convert it into uploading multiple images at a time. Below is what my single image upload looks like:
class UploadImageDemo extends StatefulWidget {
UploadImageDemo() : super();
final String title = "Upload Image Demo";
@override
UploadImageDemoState createState() => UploadImageDemoState();
}
class UploadImageDemoState extends State<UploadImageDemo> {
//
static final String uploadEndPoint =
'http://localhost/flutter_test/upload_image.php';
Future<File> file;
String status = '';
String base64Image;
File tmpFile;
String errMessage = 'Error Uploading Image';
chooseImage() {
setState(() {
file = ImagePicker.pickImage(source: ImageSource.gallery);
});
setStatus('');
}
setStatus(String message) {
setState(() {
status = message;
});
}
startUpload() {
setStatus('Uploading Image...');
if (null == tmpFile) {
setStatus(errMessage);
return;
}
String fileName = tmpFile.path.split('/').last;
upload(fileName);
}
upload(String fileName) {
http.post(uploadEndPoint, body: {
"image": base64Image,
"name": fileName,
}).then((result) {
setStatus(result.statusCode == 200 ? result.body : errMessage);
}).catchError((error) {
setStatus(error);
});
}
Widget showImage() {
return FutureBuilder<File>(
future: file,
builder: (BuildContext context, AsyncSnapshot<File> snapshot) {
if (snapshot.connectionState == ConnectionState.done &&
null != snapshot.data) {
tmpFile = snapshot.data;
base64Image = base64Encode(snapshot.data.readAsBytesSync());
return Flexible(
child: Image.file(
snapshot.data,
fit: BoxFit.fill,
),
);
} else if (null != snapshot.error) {
return const Text(
'Error Picking Image',
textAlign: TextAlign.center,
);
} else {
return const Text(
'No Image Selected',
textAlign: TextAlign.center,
);
}
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Upload Image Demo"),
),
body: Container(
padding: EdgeInsets.all(30.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
OutlineButton(
onPressed: chooseImage,
child: Text('Choose Image'),
),
SizedBox(
height: 20.0,
),
showImage(),
SizedBox(
height: 20.0,
),
OutlineButton(
onPressed: startUpload,
child: Text('Upload Image'),
),
SizedBox(
height: 20.0,
),
Text(
status,
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.green,
fontWeight: FontWeight.w500,
fontSize: 20.0,
),
),
SizedBox(
height: 20.0,
),
],
),
),
);
}
}
Based on the above everything works perfect for single upload, onn click of chooseImage
I can pick the image and showImage
displays it and startUpload
uploads it to the server side but I want it to pick multiple images at once.