我正在尝试将某些表单详细信息解析到onPressed提交按钮上的另一个屏幕上,但是我似乎迷失了方向。
我想将新功能页面中填充的所有数据与代码一起传递给下面的功能页面图像。
这是代码。
@override
_NewFeaturedState createState() => _NewFeaturedState();
}
class _NewFeaturedState extends State<NewFeatured> {
String baseUrl = 'http://api.com/api/api';
String selectedCountry;
File _image;
File _imageFile;
bool _isUploading = false;
List<Note> _notesList = List();
String _selectedLocation;
void _add(Note note) {
setState(() {
_notesList.insert(0, note);
});
}
void _update(int index, Note updatedNote) {
setState(() {
_notesList.removeAt(index);
_notesList.insert(index, updatedNote);
});
}
void switchSelectedCountry(selection) {
events = selection;
scoops = selection;
setState(() {
_selectedLocation = selection;
});
}
void _getImage(BuildContext context, ImageSource source) async {
File image = await ImagePicker.pickImage(source: source);
setState(() {
_imageFile = image;
});
// Closes the bottom sheet
Navigator.pop(context);
}
Future getImage() async {
var image = await ImagePicker.pickImage(source: ImageSource.camera);
setState(() {
_imageFile = image;
});
Navigator.pop(context);
}
Future<Map<String, dynamic>> _uploadImage(File image) async {
setState(() {
_isUploading = true;
});
final mimeTypeData =
lookupMimeType(image.path, headerBytes: [0xFF, 0xD8]).split('/');
final imageUploadRequest =
http.MultipartRequest('POST', Uri.parse(baseUrl));
final file = await http.MultipartFile.fromPath('image', image.path,
contentType: MediaType(mimeTypeData[0], mimeTypeData[1]));
imageUploadRequest.fields['ext'] = mimeTypeData[1];
imageUploadRequest.files.add(file);
try {
final streamedResponse = await imageUploadRequest.send();
final response = await http.Response.fromStream(streamedResponse);
if (response.statusCode != 200) {
return null;
}
final Map<String, dynamic> responseData = json.decode(response.body);
_resetState();
return responseData;
} catch (e) {
print(e);
return null;
}
}
void _startUploading() async {
final Map<String, dynamic> response = await _uploadImage(_imageFile);
print(response);
if (response == null || response.containsKey("error")) {
Toast.show("Image Upload Successfully!!!", context,
duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM);
} else {
Toast.show("Image Uploaded Successfully!!!", context,
duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM);
}
}
void _resetState() {
setState(() {
_isUploading = false;
_imageFile = null;
});
}
void _openImagePickerModal(BuildContext context) {
final flatButtonColor = Theme.of(context).primaryColor;
print('Image Picker Modal Called');
showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return Container(
height: 150.0,
padding: EdgeInsets.all(10.0),
child: Column(
children: <Widget>[
Text(
'Pick an image',
style: TextStyle(fontWeight: FontWeight.bold),
),
SizedBox(
height: 10.0,
),
FlatButton(
textColor: flatButtonColor,
child: Text('Use Camera'),
onPressed: () {
_getImage(context, ImageSource.camera);
},
),
FlatButton(
textColor: flatButtonColor,
child: Text('Use Gallery'),
onPressed: () {
_getImage(context, ImageSource.gallery);
},
),
],
),
);
});
}
Widget _buildUploadBtn() {
Widget btnWidget = Container();
if (_isUploading) {
// File is being uploaded then show a progress indicator
btnWidget = Container(
margin: EdgeInsets.only(top: 10.0),
child: CircularProgressIndicator());
} else if (!_isUploading && _imageFile != null) {
// If image is picked by the user then show a upload btn
btnWidget = Container(
margin: EdgeInsets.only(top: 10.0),
child: RaisedButton(
child: Text('Upload'),
onPressed: () {
_startUploading();
},
color: Colors.red,
textColor: Colors.white,
),
);
}
return btnWidget;
}
Future getImageFromGallery() async{
var image = await ImagePicker.pickImage(source: ImageSource.gallery, );
setState(() {
_image = image;
});
}
Widget _getNotesList() {
return ListView.builder(
itemCount: _notesList.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
leading: Icon(Icons.note),
title: Text(
_notesList[index].title,
style: TextStyle(fontSize: 22, fontWeight: FontWeight.w500),
),
subtitle: Text(
_notesList[index].document.toPlainText(),
maxLines: 1,
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => EditorPage(
update: _update,
noteIndex: index,
note: _notesList[index],
),
),
);
},
);
},
);
}
@override
Widget build(BuildContext context) {
return new StoreConnector<AppState, VoidCallback>(converter: (store) {
}, builder: (context, callback) {
return Scaffold(
appBar: AppBar(
title: Text(
'New Featured',
style: TextStyle(
color: Colors.black,
),
),
elevation: 0.0,
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.only(top: 24.0, left: 24.0, right: 24.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
SizedBox(height: MediaQuery.of(context).size.height * 0.05),
FormFieldWithCenteredPrefixIcon(
showDividerUnderneath: false,
iconWidget: Icon(
AppIcons.smiley,
color: Colors.black,
size: 16.0,
),
formField: FixDropdownButtonFormField(
value: _selectedLocation,
hint: Text('Select'),
items: <String>['Scoops', 'Events'].map((String value) {
return new FixDropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
onChanged: (newValue) {
setState(() {
_selectedLocation = newValue;
});
},
),
),
Padding(
padding: const EdgeInsets.all(15.0),
child: Form(
child: Column(
children: <Widget>[
FormFieldWithCenteredPrefixIcon(
iconWidget: Icon(
Icons.title,
size: 16.0,
color: Colors.black,
),
formField: TextFormField(
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Title'),
),
),
SizedBox(
height: MediaQuery.of(context).size.height * 0.0125),
new Column(
children: <Widget>[
Card(
color: Colors.white,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
decoration: InputDecoration(
suffixIcon: Padding(
padding: const EdgeInsets.all(24.0),
child: FloatingActionButton(
onPressed: () {
Navigator.push(context, MaterialPageRoute(
builder: (BuildContext context) => EditorPage(add: _add)),
);
},
tooltip: 'Add New Notes',
child: Icon(
Icons.add,
color: Colors.black,
),
),
),
),
),
),
),
Padding(
padding: const EdgeInsets.all(28.0),
child: Center(
child: _image == null
? Text("No Image Selected")
: Image.file(_image),
),
),
FlatButton.icon(
icon: Icon(Icons.wallpaper,
color: Colors.red),
label: Text("Select multiple Image",
style: TextStyle(color: Colors.red)),
onPressed: getImageFromGallery,
),
Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 40.0, left: 10.0, right: 10.0),
child: OutlineButton(
onPressed: () => _openImagePickerModal(context),
borderSide:
BorderSide(color: Theme.of(context).accentColor, width: 1.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(Icons.camera_alt),
SizedBox(
width: 5.0,
),
Text('Add cover image'),
],
),
),
),
_imageFile == null
? Text('Please pick an image')
: Image.file(
_imageFile,
fit: BoxFit.cover,
height: 300.0,
alignment: Alignment.topCenter,
width: MediaQuery.of(context).size.width,
),
_buildUploadBtn(),
],
),
SizedBox(
height: 4.0,
),
Padding(
padding: const EdgeInsets.all(25.0),
child: WideButton(
'Submit',
onTap: () {},
),
),
],
),
],
),
),
),
],
),
),
),
);
}
);
}
}`