轻松分享图片+文字+网址的便捷方法

时间:2019-01-31 04:23:57

标签: dart flutter share

我知道从flutter应用程序共享的官方软件包。 https://pub.dartlang.org/packages/share

共享文本和url很容易,但是我想共享来自服务器的图像,这意味着它采用URL格式,因此可能首先我必须将url转换为image,然后将图像转换为base64,然后我想可以共享图像,但是我正在寻找共享图像+文本+网站的简便方法。

我该如何通过官方共享软件包?还有其他保持良好状态的软件包吗?

2 个答案:

答案 0 :(得分:2)

简单共享似乎是您想要的:

    import 'package:flutter/material.dart';
    import 'package:file_picker/file_picker.dart';
    import 'package:simple_share/simple_share.dart';
    import 'package:flutter/services.dart';

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

    class SimpleShareApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
            showPerformanceOverlay: false,
            title: 'Simple Share App',
            home: MyHomePage()
        );
      }
    }

    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => new _MyHomePageState();
    }

    class _MyHomePageState extends State<MyHomePage> {
      Future<String> getFilePath() async {
        try {
          String filePath = await FilePicker.getFilePath(type: FileType.ANY);
          if (filePath == '') {
            return "";
          }
          print("File path: " + filePath);
          return filePath;
        } on PlatformException catch (e) {
          print("Error while picking the file: " + e.toString());
          return null;
        }
      }

      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
            title: new Text('File Picker Example'),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                RaisedButton(
                  onPressed: () {
                    SimpleShare.share(
                      title: "Share my message",
                      msg:
                          "Lorem ipsum dolor sit amet, consectetur adipisci elit, sed eiusmod " +
                              "tempor incidunt ut labore et dolore magna aliqua.",
                    );
                  },
                  child: Text('Share text!'),
                ),
                RaisedButton(
                  onPressed: () async {
                    final path = await getFilePath();
                    if (path != null && path.isNotEmpty) {
                      final uri = Uri.file(path);
                      SimpleShare.share(
                          uri: uri.toString(),
                          title: "Share my file",
                          msg: "My message");
                    }
                  },
                  child: Text('Share file!'),
                ),
              ],
            ),
          ),
        );
      }
    }

Source

答案 1 :(得分:0)

官方share package在v0.6.5中添加了对共享文件的支持,因此现在您可以将图像保存到文件中并共享该文件。参见下面的示例,该示例具有从Internet下载的图像:

import 'package:http/http.dart';
import 'package:share/share.dart';
import 'package:path_provider/path_provider.dart';

void shareImage() async {
    final response = await get(imageUrl);
    final bytes = response.bodyBytes;
    final Directory temp = await getTemporaryDirectory();
    final File imageFile = File('${temp.path}/tempImage');
    imageFile.writeAsBytesSync(response.bodyBytes);
    Share.shareFiles(['${temp.path}/tempImage'], text: 'text to share',);
}