connect

connect

connect

FlutterDevs Quick Stats

150+

Open Source Contribution

10+

Flutter Custom Plugin

200+

Blogs

5+

Session & Meetups

50+

Project Delivered

10+

Years Mobility Experience

30+

Flutter Expert

50+

Themes

Awards Winning Teams

Google Devfest Speakers

Share Data To Another Application In Flutter

share | Flutter Package

A Flutter plugin to share content from your Flutter app via the platform’s share dialog. Wraps the ACTION_SEND Intent…

pub. dev

Table Of Contents::

Share Data

Demo module ::

Image for post

Image for post

Demo Module

Implementation :

share: ^0.6.5+4
image_picker:
import 'package:image_picker/image_picker.dart';
import 'package:share/share.dart';
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true

How to implement code in dart file :

Image for post

Image for post

Share Data With Fields
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;
  }),
),
String text = '';
String subject = '';
Builder(
  builder: (BuildContext context) {
    return RaisedButton(
      color: Colors.orangeAccent[100],
      child: const Text('Share'),
      onPressed: text.isEmpty
          ? null
          : () => _onShareData(context),
    );
  },
),
_onShareData(BuildContext context) async {

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

Image for post

Final Output
Image for post

Image for post

Share Data With Empty Fields

We will make a button and onPressed method called _onShareWithEmptyFields() widget on this button.

Builder(
  builder: (BuildContext context) {
    return RaisedButton(
      color: Colors.orangeAccent[100],
      child: const Text('Share With Empty Fields'),
      onPressed: () => _onShareWithEmptyFields(context),
    );
  },
),
_onShareWithEmptyFields(BuildContext context) async {
  await Share.share("text");
}
Image for post

Image for post

Final Output
Image for post

Image for post

Share Image Inside Our Gallery
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);
      });
    }
  },
),
String pickedFile = imagePaths ==null?"":imagePaths.toString();
String trimmedFileName = pickedFile.split("/").last;
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);
  }
}
Image for post

Image for post

Final Output

Code File

Conclusion:

Flutter is a cross-platform development framework made by Google to permit a single codebase for the two iOS and Android applications. In every case, it’s better to communicate vis-à-vis as opposed to a textual discussion; it has an amazing effect on the subsequent individual. Likewise, present-day applications are additionally developing by making next edge highlights like having an ever-increasing number of approaches to allow users to associate with one another.

Post a Comment