• We share information from our application to another, much the same as when you hit the button and share text, any link, and an image inside your gallery. The framework will request that you select an application to share it.

    We will create two buttons on this screen and an additional icon for an image to share text, link, and image to another application in your app, which will call the below three logics and do our work.

    Add dependencies to pubspec.yaml file.

    share: ^0.6.5+4
    image_picker:

    Run flutter packages get in the root directory of your app.

    Enable AndriodX
    org.gradle.jvmargs=-Xmx1536M
    android.enableR8=true
    android.useAndroidX=true
    android.enableJetifier=true

    create a new dart file called share_data.dart inside the lib folder.

    TextField(
      decoration: const InputDecoration(
        labelText: 'Share text:',
        labelStyle: TextStyle(color: Colors.blue),
        hintText: 'Enter some text and/or link to share',
      ),
      maxLines: 2,
      onChanged: (String value) => setState(() {
        text = value;
      }),
    ),
    TextField(
      decoration: const InputDecoration(
        labelText: 'Share subject:',
        labelStyle: TextStyle(color: Colors.blue),

        hintText: 'Enter subject to share (optional)',
      ),
      maxLines: 2,
      onChanged: (String value) => setState(() {
        subject = value;
      }),
    ),

    Make two text fields; in this text field, we will use the onchanged method and set the string is equal to the value.

    String text = '';
    String subject = '';

    We will make a button, and onPressed method, we will apply the text as empty then, the button will not work, and we will be called _onShareData() widget on this button.

    Builder(
      builder: (BuildContext context) {
        return RaisedButton(
          color: Colors.orangeAccent[100],
          child: const Text('Share'),
          onPressed: text.isEmpty
              ? null
              : () => _onShareData(context),
        );
      },
    ),

    Let’s declare _onShareData() widget

    _onShareData(BuildContext context) async {

      final RenderBox box = context.findRenderObject();
     {
        await Share.share(text,
            subject: subject,
            sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);
      }
    }

    Now, share the image inside our gallery:

    In this screen, we will share the image inside your gallery, and we will add an icon button and image text. We will apply an image picker to pick from our gallery and share on another application on this tap function, and your system will ask you to select an application to share it.

    ListTile(
      leading: Icon(Icons.add),
      title: Text("Add image"),
      onTap: () async {
        final imagePicker = ImagePicker();
        final pickedFile = await imagePicker.getImage(
          source: ImageSource.gallery,
        );
        if (pickedFile != null) {
          setState(() {
            imagePaths.add(pickedFile.path);
          });
        }
      },
    ),

    We will trim the string image path
    String pickedFile = imagePaths ==null?"":imagePaths.toString();
    String trimmedFileName = pickedFile.split("/").last;

    Same button and onPressed method we will apply the text as empty and image path also empty then the button will not work, and we will be called _onShareData() widget on this button.

    Builder(
      builder: (BuildContext context) {
        return RaisedButton(
          color: Colors.orangeAccent[100],
          child: const Text('Share'),
          onPressed: text.isEmpty && imagePaths.isEmpty
              ? null
              : () => _onShareData(context),
        );
      },
    ),

    _onShareData(BuildContext context) async {

      final RenderBox box = context.findRenderObject();

      if (imagePaths.isNotEmpty) {
        await Share.shareFiles(imagePaths,
            text: text,
            subject: subject,
            sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);
      } else {
        await Share.share(text,
            subject: subject,
            sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);
      }
    }

    Now you can see full code here:

    import 'package:flutter/material.dart';
    import 'package:image_picker/image_picker.dart';
    import 'package:share/share.dart';


    class ShareData extends StatefulWidget {
      @override
      ShareDataState createState() => ShareDataState();
    }

    class ShareDataState extends State<ShareData> {
      String text = '';
      String subject = '';
      List<String> imagePaths = [];

      @override
      Widget build(BuildContext context) {
        String pickedFile = imagePaths ==null?"":imagePaths.toString();
        String trimmedFileName = pickedFile.split("/").last;
        return  Scaffold(
            backgroundColor: Colors.blueGrey[100],

            appBar: AppBar(
                backgroundColor: Colors.blueGrey,
                title: const Text('Flutter Share Data'),
                centerTitle: true,
                automaticallyImplyLeading: false,
              ),
              body: SingleChildScrollView(
                child: Padding(
                  padding: const EdgeInsets.all(24.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      TextField(
                        decoration: const InputDecoration(
                          labelText: 'Share text:',
                          labelStyle: TextStyle(color: Colors.blue),
                          hintText: 'Enter some text and/or link to share',
                        ),
                        maxLines: 2,
                        onChanged: (String value) => setState(() {
                          text = value;
                        }),
                      ),
                      TextField(
                        decoration: const InputDecoration(
                          labelText: 'Share subject:',
                          labelStyle: TextStyle(color: Colors.blue),

                          hintText: 'Enter subject to share (optional)',
                        ),
                        maxLines: 2,
                        onChanged: (String value) => setState(() {
                          subject = value;
                        }),
                      ),
                      const Padding(padding: EdgeInsets.only(top: 12.0)),
                      ListTile(
                        leading: Icon(Icons.add),
                        title: Text("Add image"),
                        onTap: () async {
                          final imagePicker = ImagePicker();
                          final pickedFile = await imagePicker.getImage(
                            source: ImageSource.gallery,
                          );
                          if (pickedFile != null) {
                            setState(() {
                              imagePaths.add(pickedFile.path);
                            });
                          }
                        },
                      ),
                      Text(imagePaths ==null?"":trimmedFileName),

                      const Padding(padding: EdgeInsets.only(top: 12.0)),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: ]
                          Builder(
                            builder: (BuildContext context) {
                              return RaisedButton(
                                color: Colors.orangeAccent[100],
                                child: const Text('Share'),
                                onPressed: text.isEmpty && imagePaths.isEmpty
                                    ? null
                                    : () => _onShareData(context),
                              );
                            },
                          ),
                          const Padding(padding: EdgeInsets.only(top: 12.0)),
                          Builder(
                            builder: (BuildContext context) {
                              return RaisedButton(
                                color: Colors.orangeAccent[100],

                                child: const Text('Share With Empty Fields'),
                                onPressed: () => _onShareWithEmptyFields(context),
                              );
                            },
                          ),
                        [,
                      ),

                    ],
                  ),
                ),
              )
        );

      }

      _onShareData(BuildContext context) async {

        final RenderBox box = context.findRenderObject();

        if (imagePaths.isNotEmpty) {
          await Share.shareFiles(imagePaths,
              text: text,
              subject: subject,
              sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);
        } else {
          await Share.share(text,
              subject: subject,
              sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);
        }
      }

      _onShareWithEmptyFields(BuildContext context) async {
        await Share.share("text");
      }
    }
     

0 Years in
Operation
0 Loyal
Clients
0 Successful
Projects

Words from our clients

 

Tell Us About Your Project

We’ve done lot’s of work, Let’s Check some from here