How Do I Share Image On Ios And Android Using Flutter

admin_img Posted By Bajarangi soft , Posted On 21-09-2020

Share image in flutter android app

How Do I Share Image On Ios And Android Using Flutter

Step 1
We cannot directly remove the time stamp from Share Image  but using the intl.dart package we can easily filter the date stamp from time stamp. So open your flutter project’s pubspec.yaml in code

dependencies:
  flutter:
    sdk: flutter
  share: ^0.6.5+1
  image_picker: ^0.6.7+9
  firebase_storage: ^4.0.0
  html: ^0.14.0+3

step 2
After done saving the pubspec.yaml file, Open your flutter project root folder in Command Prompt or Terminal and execute flutter pub get command. 
flutter pub get

Step 3
 Open your project’s main.dart file and import material.dart and   share: ^0.6.5+1 AND image_picker: ^0.6.7+9. dart package.
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:share/share.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'dart:io';
import 'package:path/path.dart';

Complete Code For Share Image In Flutter
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:share/share.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'dart:io';
import 'package:path/path.dart';

void main() {
  runApp(DemoApp());
}

class DemoApp extends StatefulWidget {
  @override
  DemoAppState createState() => DemoAppState();
}

class DemoAppState extends State<DemoApp> {
  File _image;


  String text = '';
  String subject = '';
  List<String> imagePaths = [];

  @override
  Widget build(BuildContext context) {
    Future getImage() async {
      var image = await ImagePicker.pickImage(source: ImageSource.gallery);

      setState(() {
        _image = image;
        print('Image Path $_image');
      });
    }
    Future uploadPic(BuildContext context) async{
      String fileName = basename(_image.path);
      StorageReference firebaseStorageRef = FirebaseStorage.instance.ref().child(fileName);
      StorageUploadTask uploadTask = firebaseStorageRef.putFile(_image);
      StorageTaskSnapshot taskSnapshot=await uploadTask.onComplete;
      setState(() {
        print("Profile Picture uploaded");
        Scaffold.of(context).showSnackBar(SnackBar(content: Text('Profile Picture Uploaded')));
      });
    }


    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Share Plugin Demo',
      home: Scaffold(
          appBar: AppBar(
            centerTitle: true,
            backgroundColor: Colors.indigo,
            title: const Text('Share Plugin Image Demo'),
            leading: Icon(Icons.chevron_left),
          ),
          body: SingleChildScrollView(
            child: Padding(
              padding: const EdgeInsets.all(24.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  ListTile(
                    title: RaisedButton.icon(
                      color: Colors.pink,
                      onPressed: () {
                        getImage();
                      },
                      label: Text('Add Image', style: TextStyle(color: Colors.white),),
                      icon: Icon(Icons.add, color: Colors.white,),
                    ),
                  ),
                  SizedBox(height: 20.0,),
                  Container(
                    child: (_image!=null)?Image.file(
                      _image,
                      fit: BoxFit.fill,
                    ):Image.network("https://images.unsplash.com/photo-1502164980785-f8aa41d53611?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60",
                      fit: BoxFit.fill,
                    ),
                  ),
                  const Padding(padding: EdgeInsets.only(top: 12.0)),
                  Builder(
                    builder: (BuildContext context) {
                      return RaisedButton(
                        color: Colors.indigo,
                        child: const Text('Share',style: TextStyle(color:Colors.white),),
                        onPressed: text.isEmpty && imagePaths.isEmpty
                            ? null
                            : () => _onShare(context),
                      );
                    },
                  ),
                ],
              ),
            ),
          )),
    );
  }

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

Related Post