flutter_share 2.0.0 flutter_share: ^2.0.0 copied to clipboard
Simple way to share message, links or files from your flutter app for Android and IOS (Enter to see some gifs).
import 'dart:async';
import 'dart:io';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_share/flutter_share.dart';
import 'package:path_provider/path_provider.dart';
import 'package:screenshot/screenshot.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
final _controller = ScreenshotController();
Future<void> share() async {
await FlutterShare.share(
title: 'Example share',
text: 'Example share text',
linkUrl: 'https://flutter.dev/',
chooserTitle: 'Example Chooser Title');
}
Future<void> shareFile() async {
final result = await FilePicker.platform.pickFiles();
if (result == null || result.files.isEmpty) return null;
await FlutterShare.shareFile(
title: 'Example share',
text: 'Example share text',
filePath: result.files[0] as String,
);
}
Future<void> shareScreenshot() async {
Directory? directory;
if (Platform.isAndroid) {
directory = await getExternalStorageDirectory();
} else {
directory = await getApplicationDocumentsDirectory();
}
final String localPath =
'${directory!.path}/${DateTime.now().toIso8601String()}.png';
await _controller.captureAndSave(localPath);
await Future.delayed(Duration(seconds: 1));
await FlutterShare.shareFile(
title: 'Compartilhar comprovante',
filePath: localPath,
fileType: 'image/png'
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Screenshot(
controller: _controller,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextButton(
child: Text('Share text and link'),
onPressed: share,
),
TextButton(
child: Text('Share local file'),
onPressed: shareFile,
),
TextButton(
child: Text('Share screenshot'),
onPressed: shareScreenshot,
),
],
),
),
),
),
);
}
}