Created
February 2, 2020 17:58
-
-
Save carzacc/b592ac6630b6158bcaefd955b9a50509 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class UploadPage extends StatefulWidget { | |
UploadPage({Key key, this.url}) : super(key: key); | |
final String url; | |
@override | |
_UploadPageState createState() => _UploadPageState(); | |
} | |
class _UploadPageState extends State<UploadPage> { | |
Future<String> uploadImage(filename, url) async { | |
var request = http.MultipartRequest('POST', Uri.parse(url)); | |
request.files.add(await http.MultipartFile.fromPath('picture', filename)); | |
var res = await request.send(); | |
return res.reasonPhrase; | |
} | |
String state = ""; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Flutter File Upload Example'), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text(state) | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () async { | |
var file = await ImagePicker.pickImage(source: ImageSource.gallery); | |
var res = await uploadImage(file.path, widget.url); | |
setState(() { | |
state = res; | |
print(res); | |
}); | |
}, | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment