Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Listview 是否可以使用Flatter\u share或其他共享插件共享来自Flatter image\u picker的文本和照片?_Listview_Flutter_Share_Flutter Dependencies - Fatal编程技术网

Listview 是否可以使用Flatter\u share或其他共享插件共享来自Flatter image\u picker的文本和照片?

Listview 是否可以使用Flatter\u share或其他共享插件共享来自Flatter image\u picker的文本和照片?,listview,flutter,share,flutter-dependencies,Listview,Flutter,Share,Flutter Dependencies,使用Flatter_share插件,我可以在我的Flatter ListView中成功地与以下用户共享firestore集合中的文本: child: IconButton( icon: Icon( Icons.share, onPressed: () => Share.share( '${(snapshot.data.documents[index]['title'])}', ))), 现在,我在ListView中添加了一个新的Flitter listtil

使用Flatter_share插件,我可以在我的Flatter ListView中成功地与以下用户共享firestore集合中的文本:

child: IconButton(
icon: Icon(
  Icons.share,
   onPressed: () => Share.share(
   '${(snapshot.data.documents[index]['title'])}',
       ))),
现在,我在ListView中添加了一个新的Flitter listtile部分,其中包括来自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,
     ),
   ],
 );}}
void\u openImagePicker(构建上下文){
showModalBottomSheet(上下文:context,builder:(BuildContext){
返回容器(
高度:180.0,
填充:所有边缘设置(10.0),
子:列(
儿童:[
文本('选择照片',
样式:TextStyle(fontWeight:fontWeight.bold),
尺寸箱(高度:10.0),
扁平按钮(
textColor:Theme.of(context.primaryColor),
子项:文本(“使用摄影机”),
已按下:(){
_getImage(上下文,ImageSource.camera);
},),
扁平按钮(
textColor:Theme.of(context).primaryColor,child:
文本(“打开库”),
已按下:(){
_getImage(上下文,ImageSource.gallery);
},),
]
),);
});
}
@凌驾
小部件构建(构建上下文){
返回列(
儿童:[
大纲按钮(
已按下:(){
_openImagePicker(上下文);
},
孩子:排(
mainAxisAlignment:mainAxisAlignment.start,
儿童:[
图标(图标、摄像头和alt),
大小盒子(
宽度:1.0,
),
//文本(“添加图像”),
]
),//行
),//大纲按钮
尺寸箱(高度:10.0),
_imageFile==null?Text('添加图像'):Image.file(\u imageFile,
适合:BoxFit.cover,
高度:200.0,
宽度:200.0,
对齐:alignment.topCenter,
),
],
);}}
我的问题是,这个Flatter_共享插件或另一个插件有没有办法将新图像与原始共享firestore文本结合起来,这样用户就可以同时共享文本和照片,而不仅仅是文本?还是只能发送一个或另一个

图像不会存储在Firestore中,只存储文本,因此可以使用用户相机拍摄图像或从他们的图库上载图像,然后附加到共享文本中


这是添加了esys共享的完整代码,它不发送图像\u选择器图像

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');
}
}
类ImageInput扩展StatefulWidget{
@凌驾
_ImageInputState createState()=>\u ImageInputState();
}
类_ImageInputState扩展状态{
文件imageFile;
void\u getImage(BuildContext上下文,ImageSource源){
pickImage(源代码:source,maxWidth:200.0)。然后((文件图像){
设置状态(){
_图像文件=图像;
//_imageFile=wait ImagePicker.pickImage(源:source);
//List imageBytes=wait_imageFile.readAsBytes();
//var uint8List=uint8List.fromList(imageBytes);
});
Navigator.pop(上下文);
});
}
void\u openImagePicker(构建上下文){
showModalBottomSheet(上下文:context,builder:(BuildContext){
返回容器(
高度:180.0,
填充:所有边缘设置(10.0),
子:列(
儿童:[
文本('选择照片',
样式:TextStyle(fontWeight:fontWeight.bold),
尺寸箱(高度:10.0),
扁平按钮(
textColor:Theme.of(context.primaryColor),
子项:文本(“使用摄影机”),
已按下:(){
_getImage(上下文,ImageSource.camera);
},),
扁平按钮(
textColor:Theme.of(context).primaryColor,child:
文本(“打开库”),
已按下:(){
_getImage(上下文,ImageSource.gallery);
},)
]
),);
});
}
@凌驾
小部件构建(构建上下文){
返回列(
儿童:[
大纲按钮(
已按下:(){
_openImagePicker(上下文);
},
孩子:排(
mainAxisAlignment:mainAxisAlignment.start,
儿童:[
图标(图标、摄像头和alt),
大小盒子(
宽度:1.0,
),
//文本(“添加图像”),
]
),//行
),//大纲按钮
尺寸箱(高度:10.0),
_imageFile==null?Text('添加图像'):Image.file(\u imageFile,
适合:BoxFit.cover,
高度:200.0,
宽度:200.0,
//宽度:MediaQuery.of(context).size.width,
对齐:alignment.topCenter,
),
材料按钮(
子项:文本(“共享混合”),
onPressed:()async=>wait _sharemix(),
),
],
);
}
}
Future\u shareMixed()异步{
试一试{
final ByteData bytes1=等待rootBundle.load(“u imageFile”);
//final ByteData bytes2=等待rootBundle.load('assets/image2.png');
//final ByteData bytes3=等待rootBundle.load('assets/addresses.csv');
等待Share.files(
"易思影像",,
{
“_imageFile”:bytes1.buffer.asUint8List(),
//'bluedan.png':bytes2.buffer.asUint8List(),
//“addresses.csv”:bytes3.buffer.asUint8List(),
},
'*/*',
文本:“我的可选文本”。);
}捕获(e){
打印('error:$e');
}
}

您可以复制粘贴运行下面的完整代码
您可以使用软件包

工作演示

完整代码

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';
import 'package:esys_flutter_share/esys_flutter_share.dart';
import 'dart:typed_data';

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);
    });
  }

  Future<void> _shareImageAndText() async {
    try {
      List<int> imageBytes = await _imageFile.readAsBytes();
      var uint8List = Uint8List.fromList(imageBytes);
      await Share.file('esys image', 'esys.jpg', uint8List, 'image/jpeg',
          text: 'My optional text.');
    } catch (e) {
      print('error: $e');
    }
  }

  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 _shareImageAndText(),
        ),
      ],
    );
  }
}

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ImageInput(),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
导入“包:image_picker/image_picker.dart”;
导入“dart:io”;
导入“包:esys_flatter_share/esys_flatter_share.dart”;
导入“dart:键入的_数据”;
类ImageInput扩展StatefulWidget{
@凌驾
_ImageInputState createState()=>\u ImageInputState();
}
类_ImageInputState扩展状态{
文件imageFile;
void\u getImage(BuildContext上下文,ImageSource源){
pickImage(源代码:source,maxWidth:200.0)。然后((文件图像){
设置状态(){
_图像文件=图像;
//_imageFile=wait ImagePicker.pickImage(源:source);
//List imageBytes=wait_imageFile.readAsBytes();
//变量uint8List=Uint8