Используя плагин flutter_share, я могу успешно обмениваться текстом из моих коллекций пожарных в моем ListView Flutter с:
child: IconButton(
icon: Icon(
Icons.share,
onPressed: () => Share.share(
'${(snapshot.data.documents[index]['title'])}',
))),
Я теперь добавил новый раздел списка флаттера в ListView, который включает фотографию из image_picker плагин, где пользователи могут выбрать фотографию из своей галереи или сделать новую фотографию и добавить ее под текстом данных firestore.
void _openImagePicker(BuildContext context) {
showModalBottomSheet(context: context, builder: (BuildContext context) {
return Container(
height: 180.0,
padding: EdgeInsets.all(10.0),
child: Column(
children: [
Text('Choose photo',
style:TextStyle(fontWeight: FontWeight.bold),),
SizedBox(height: 10.0),
FlatButton(
textColor: Theme.of(context).primaryColor,
child: Text('Use Camera'),
onPressed: () {
_getImage(context, ImageSource.camera);
},),
FlatButton(
textColor: Theme.of(context).primaryColor,child:
Text('Open Gallery'),
onPressed: () {
_getImage(context, ImageSource.gallery);
},),
]
),);
});
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
OutlineButton(
onPressed: () {
_openImagePicker(context);
},
child:Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Icon(Icons.camera_alt),
SizedBox(
width:1.0,
),
// Text('Add Image'),
]
),//Row
),//outline button
SizedBox(height: 10.0),
_imageFile == null ? Text('Add an Image') : Image.file(_imageFile,
fit: BoxFit.cover,
height: 200.0,
width: 200.0,
alignment: Alignment.topCenter,
),
],
);}}
Мой вопрос, есть ли способ с этим плагином flutter_share или другим плагином объединить новое изображение с исходным общим текстом пожарного депо, чтобы пользователи могли совместно использовать как текст, так и фотографию, а не только текст? Или это возможно только для отправки того или другого?
Изображение не будет сохранено в Firestore, только текст, поэтому изображение может быть снято с камеры пользователя или загружено из их галереи, а затем добавлено к общему тексту.
Вот полный код с добавленной ссылкой esys, он не отправляет изображение image_picker
class ImageInput extends StatefulWidget {
@override
_ImageInputState createState() => _ImageInputState();
}
class _ImageInputState extends State<ImageInput> {
File _imageFile;
void _getImage(BuildContext context, ImageSource source){
ImagePicker.pickImage(source: source, maxWidth: 200.0).then((File image){
setState((){
_imageFile = image;
// _imageFile = await ImagePicker.pickImage(source: source);
// List<int> imageBytes = await _imageFile.readAsBytes();
// var uint8List = Uint8List.fromList(imageBytes);
});
Navigator.pop(context);
});
}
void _openImagePicker(BuildContext context) {
showModalBottomSheet(context: context, builder: (BuildContext context) {
return Container(
height: 180.0,
padding: EdgeInsets.all(10.0),
child: Column(
children: [
Text('Choose photo',
style:TextStyle(fontWeight: FontWeight.bold),),
SizedBox(height: 10.0),
FlatButton(
textColor: Theme.of(context).primaryColor,
child: Text('Use Camera'),
onPressed: () {
_getImage(context, ImageSource.camera);
},),
FlatButton(
textColor: Theme.of(context).primaryColor,child:
Text('Open Gallery'),
onPressed: () {
_getImage(context, ImageSource.gallery);
},)
]
),);
});
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
OutlineButton(
onPressed: () {
_openImagePicker(context);
},
child:Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Icon(Icons.camera_alt),
SizedBox(
width:1.0,
),
// Text('Add Image'),
]
),//Row
),//outline button
SizedBox(height: 10.0),
_imageFile == null ? Text('Add an Image') : Image.file(_imageFile,
fit: BoxFit.cover,
height: 200.0,
width: 200.0,
// width: MediaQuery.of(context).size.width,
alignment: Alignment.topCenter,
),
MaterialButton(
child: Text('Share mixed'),
onPressed: () async => await _shareMixed(),
),
],
);
}
}
Future<void> _shareMixed() async {
try {
final ByteData bytes1 = await rootBundle.load('_imageFile');
//final ByteData bytes2 = await rootBundle.load('assets/image2.png');
// final ByteData bytes3 = await rootBundle.load('assets/addresses.csv');
await Share.files(
'esys images',
{
'_imageFile': bytes1.buffer.asUint8List(),
//'bluedan.png': bytes2.buffer.asUint8List(),
//'addresses.csv': bytes3.buffer.asUint8List(),
},
'*/*',
text: 'My optional text.');
} catch (e) {
print('error: $e');
}
}