Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convenient way to share image + text + url in flutter

I know official package to sharing from flutter app. https://pub.dartlang.org/packages/share

Its easy to share text and url but I want to share image that is coming from server means its in URL format so may be first I have to convert url to image and then I have to convert image to base64 then I think I can share image But I'm looking for easy way to share image+text+website.

How can I do by official share package? Any other package that maintained well?

like image 926
Govaadiyo Avatar asked Jan 31 '19 04:01

Govaadiyo


People also ask

How do you submit a URL on Flutter?

Step 1: Open “pubspec. yaml” file from the project folder. Step 2: In the pubspec. yaml file, type “url_launcher:” under dependencies.


2 Answers

The official share package added support for sharing files in v0.6.5, so now you can save the image to a file and share that file. See below an example with an image downloaded from 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',);
}
like image 108
georkings Avatar answered Oct 13 '22 13:10

georkings


Simple Share seems to be what you're looking for:

    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

like image 22
Yster Avatar answered Oct 13 '22 11:10

Yster