void _upLoadFile() async{ images= await ImagePicker.pickImage(source: ImageSource.gallery,maxHeight: 100); setState(() { CImages=images; }); } void _take() async{ images= await ImagePicker.pickImage(source: ImageSource.camera,maxHeight: 100); setState(() { CImages=images; _upLoad(); }); } void _upLoad() async{ FormData formData = FormData.fromMap({ "file":await MultipartFile.fromFile(CImages.path, filename: "aa.jpg") }); Response response= await new Dio().post("http://192.168.42.244:9102/file/upload",data: formData); print("上传成功===========${response.data}"); } Widget _buildsImage(){ if(CImages==null){ return Text("重新选择"); }else{ return Image.file(CImages); } } @override Widget build(BuildContext context) { // This method is rerun every time setState is called, for instance as done // by the _incrementCounter method above. // // The Flutter framework has been optimized to make rerunning build methods // fast, so that you can just rebuild anything that needs updating rather // than having to individually change instances of widgets. return Scaffold( appBar: AppBar( // Here we take the value from the MyHomePage object that was created by // the App.build method, and use it to set our appbar title. title: Text(widget.title), ), body:Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: <Widget>[ RaisedButton( child: Text("打开相册"), onPressed: (){ _upLoadFile(); }, ), RaisedButton( child: Text("拍照"), onPressed: (){ _take(); }, ), _buildsImage() ], ) // This trailing comma makes auto-formatting nicer for build methods. ); } }