-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c3eca6
commit 0febe13
Showing
4 changed files
with
193 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
import 'dart:io'; | ||
// import 'dart:js_interop'; | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:http/http.dart' as http; | ||
import 'package:flutter/material.dart'; | ||
import 'package:image_picker/image_picker.dart'; | ||
import 'package:modal_progress_hud_nsn/modal_progress_hud_nsn.dart'; | ||
|
||
class UploadImageScreen extends StatefulWidget { | ||
const UploadImageScreen({Key? key}) : super(key: key); | ||
|
||
@override | ||
_UploadImageScreenState createState() => _UploadImageScreenState(); | ||
} | ||
|
||
class _UploadImageScreenState extends State<UploadImageScreen> { | ||
File? imageGal, imageCam; | ||
final _pickerGal = ImagePicker(); | ||
final _pickerCam = ImagePicker(); | ||
bool showSpinner = false; | ||
|
||
Future getImageGL() async { | ||
final pickedFile_Gallery = await _pickerGal.pickImage( | ||
source: ImageSource.gallery, imageQuality: 80); | ||
|
||
if (pickedFile_Gallery != null) { | ||
imageGal = File(pickedFile_Gallery.path); | ||
setState(() {}); | ||
uploadImageFromGallery(); | ||
} else { | ||
print("No image selected"); | ||
} | ||
} | ||
|
||
Future getImageCM() async { | ||
final pickedFile_Camera = await _pickerCam.pickImage( | ||
source: ImageSource.camera, imageQuality: 80); | ||
|
||
if (pickedFile_Camera != null) { | ||
imageCam = File(pickedFile_Camera.path); | ||
setState(() {}); | ||
uploadImageFromCamera(); | ||
} else { | ||
print("No image Captured"); | ||
} | ||
} | ||
|
||
Future<void> uploadImageFromGallery() async { | ||
setState(() { | ||
showSpinner = true; | ||
}); | ||
var stream = new http.ByteStream(imageGal!.openRead()); | ||
stream.cast(); | ||
|
||
var length = await imageGal!.length(); | ||
|
||
//coming soon. | ||
var uri = Uri.parse("https://fakestoreapi.com/products"); | ||
|
||
var request = new http.MultipartRequest('POST', uri); | ||
|
||
request.fields['title'] = "Static title"; | ||
|
||
var multiport = new http.MultipartFile('image', stream, length); | ||
|
||
request.files.add(multiport); | ||
|
||
var response = await request.send(); | ||
|
||
if (response.statusCode == 200) { | ||
setState(() { | ||
showSpinner = false; | ||
}); | ||
|
||
print("image Uploaded"); | ||
} else { | ||
print("failed to upload"); | ||
setState(() { | ||
showSpinner = true; | ||
}); | ||
} | ||
} | ||
|
||
Future<void> uploadImageFromCamera() async { | ||
setState(() { | ||
showSpinner = true; | ||
}); | ||
var stream = new http.ByteStream(imageCam!.openRead()); | ||
stream.cast(); | ||
|
||
var length = await imageCam!.length(); | ||
|
||
//coming soon. | ||
var uri = Uri.parse("https://fakestoreapi.com/products"); | ||
|
||
var request = new http.MultipartRequest('POST', uri); | ||
|
||
request.fields['title'] = "Static title"; | ||
|
||
var multiport = new http.MultipartFile('image', stream, length); | ||
|
||
request.files.add(multiport); | ||
|
||
var response = await request.send(); | ||
|
||
if (response.statusCode == 200) { | ||
setState(() { | ||
showSpinner = false; | ||
}); | ||
|
||
print("image Uploaded"); | ||
} else { | ||
print("failed to upload"); | ||
setState(() { | ||
showSpinner = true; | ||
}); | ||
} | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return ModalProgressHUD( | ||
inAsyncCall: showSpinner, | ||
child: Scaffold( | ||
appBar: AppBar( | ||
title: const Text('Upload Image'), | ||
), | ||
body: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
crossAxisAlignment: CrossAxisAlignment.center, | ||
children: [ | ||
GestureDetector( | ||
onTap: () { | ||
getImageGL(); | ||
}, | ||
child: Container( | ||
child: imageGal == null | ||
? Center( | ||
child: Text("Pick an image"), | ||
) | ||
: Container( | ||
child: Center( | ||
child: Image.file( | ||
File(imageGal!.path).absolute, | ||
height: 100, | ||
width: 100, | ||
fit: BoxFit.cover, | ||
), | ||
), | ||
), | ||
), | ||
), | ||
GestureDetector( | ||
onTap: () { | ||
getImageCM(); | ||
}, | ||
child: Container( | ||
child: imageCam == null | ||
? Center( | ||
child: Text("Click an image"), | ||
) | ||
: Container( | ||
child: Center( | ||
child: Image.file( | ||
File(imageCam!.path).absolute, | ||
height: 100, | ||
width: 100, | ||
fit: BoxFit.cover, | ||
), | ||
), | ||
), | ||
), | ||
), | ||
SizedBox( | ||
height: 150, | ||
), | ||
], | ||
), | ||
)); | ||
} | ||
} |