How To Make Share Text Using Flutter Android App

admin_img Posted By Bajarangi soft , Posted On 27-11-2020

Flutter plugin to share content from your Flutter app via the platform's share dialog.Wraps the ACTION_SEND Intent on Android and UIActivityViewController on iOS

How To Make Share Text Using Flutter Android App

Share Text 
Step 1 We cannot directly remove the time stamp from Share Text 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+4
  google_fonts: ^1.1.0
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



Complete Code For Share Text  In Flutter
main.dart
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:share/share.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Share Files',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
          primarySwatch: Colors.deepOrange,
          visualDensity: VisualDensity.comfortable,
          brightness: Brightness.dark,
          textTheme: GoogleFonts.montserratTextTheme(
              Theme.of(context).textTheme
          )
      ),
      home: MyHomePage(),
    );
  }
}

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

class _MyHomePageState extends State<MyHomePage> {
  String text = '';
  String subject = '';

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Share Demo',
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.green,
          title: const Text('Share Demo'),
        ),
        body: Container(
          padding: EdgeInsets.all(25.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              TextField(
                decoration: const InputDecoration(
                  enabledBorder: OutlineInputBorder(
                    borderRadius: BorderRadius.all(Radius.circular(12.0)),
                    borderSide: BorderSide(color: Colors.black26),
                  ),
                  focusedBorder: OutlineInputBorder(
                    borderRadius: BorderRadius.all(Radius.circular(12.0)),
                    borderSide: BorderSide(color: Colors.grey),
                  ),
                  contentPadding: EdgeInsets.all(4),
                  labelText: ' Text',
                  hintText: 'Enter some text or link to share',
                ),
                maxLines: 2,
                onChanged: (String txt) {
                  setState(() {
                    text = txt;
                  });
                },
              ),
              SizedBox(height: 30,),
              TextField(
                decoration: const InputDecoration(
                  enabledBorder: OutlineInputBorder(
                    borderRadius: BorderRadius.all(Radius.circular(12.0)),
                    borderSide: BorderSide(color: Colors.black26),
                  ),
                  focusedBorder: OutlineInputBorder(
                    borderRadius: BorderRadius.all(Radius.circular(12.0)),
                    borderSide: BorderSide(color: Colors.grey),
                  ),
                  contentPadding: EdgeInsets.all(4),
                  labelText: ' Text',
                  hintText: 'Enter some subject to share',
                ),
                maxLines: 2,
                onChanged: (String txt) {
                  setState(() {
                    subject = txt;
                  });
                },
              ),
              SizedBox(
                height: 20,
              ),
              Builder(
                builder: (BuildContext context) {
                  return RaisedButton(
                    child: const Text('Share'),
                    color: Colors.green,
                    onPressed: text.isEmpty
                        ? null
                        : () {
                      share(context);
                    },
                  );
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

 

Related Post