share_intent_package 1.0.8
share_intent_package: ^1.0.8 copied to clipboard
The easiest Flutter share intent plugin. Receive text, images, videos, documents from other apps with automated setup. iOS ShareExtension + Android intent filters made simple.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:share_intent_package/share_intent_package.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
ShareData? _initialShare;
StreamSubscription<ShareData>? _shareSubscription;
final List<ShareData> _shareHistory = [];
@override
void initState() {
super.initState();
initPlatformState();
initShareIntent();
}
@override
void dispose() {
_shareSubscription?.cancel();
super.dispose();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
try {
platformVersion =
await ShareIntentPackage.instance.getPlatformVersion() ??
'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
Future<void> initShareIntent() async {
try {
// Initialize the share intent functionality
await ShareIntentPackage.instance.init();
// Get initial share data (when app is opened from share intent)
final initialShare = await ShareIntentPackage.instance.getInitialShare();
if (initialShare != null && initialShare.hasData) {
setState(() {
_initialShare = initialShare;
_shareHistory.insert(0, initialShare);
});
// Clear the initial share after processing
await ShareIntentPackage.instance.reset();
}
// Listen for share intents while app is running
_shareSubscription = ShareIntentPackage.instance.getShareStream().listen(
(ShareData shareData) {
if (mounted && shareData.hasData) {
setState(() {
_shareHistory.insert(0, shareData);
// Keep only last 10 shares
if (_shareHistory.length > 10) {
_shareHistory.removeLast();
}
});
}
},
onError: (error) {
debugPrint('Share intent stream error: $error');
},
);
} catch (e) {
debugPrint('Error initializing share intent: $e');
}
}
void _clearHistory() {
setState(() {
_shareHistory.clear();
_initialShare = null;
});
}
Widget _buildShareDataCard(ShareData shareData, {bool isInitial = false}) {
return Card(
margin: const EdgeInsets.all(8.0),
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(
isInitial ? Icons.launch : Icons.share,
color: isInitial ? Colors.green : Colors.blue,
),
const SizedBox(width: 8),
Text(
isInitial ? 'Initial Share (App Launch)' : 'Runtime Share',
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
),
),
],
),
const Divider(),
if (shareData.text != null) ...[
const Text(
'Text:',
style: TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 4),
Text(shareData.text!),
const SizedBox(height: 8),
],
if (shareData.filePaths != null &&
shareData.filePaths!.isNotEmpty) ...[
const Text(
'Files:',
style: TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 4),
...shareData.filePaths!.map(
(path) => Padding(
padding: const EdgeInsets.only(left: 16, bottom: 4),
child: Text('• ${path.split('/').last}'),
),
),
const SizedBox(height: 8),
],
if (shareData.mimeType != null) ...[
const Text(
'MIME Type:',
style: TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 4),
Text(shareData.mimeType!),
const SizedBox(height: 8),
],
if (shareData.extraData != null &&
shareData.extraData!.isNotEmpty) ...[
const Text(
'Extra Data:',
style: TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 4),
...shareData.extraData!.entries.map(
(entry) => Padding(
padding: const EdgeInsets.only(left: 16, bottom: 2),
child: Text('${entry.key}: ${entry.value}'),
),
),
],
],
),
),
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Share Intent Example'),
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
actions: [
IconButton(
icon: const Icon(Icons.clear_all),
onPressed: _clearHistory,
tooltip: 'Clear History',
),
],
),
body: Column(
children: [
Container(
width: double.infinity,
padding: const EdgeInsets.all(16),
color: Colors.grey[100],
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Platform: $_platformVersion',
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
Text(
'Share History: ${_shareHistory.length} items',
style: const TextStyle(color: Colors.grey),
),
],
),
),
Expanded(
child: _shareHistory.isEmpty
? const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.share, size: 64, color: Colors.grey),
SizedBox(height: 16),
Text(
'No shared content yet',
style: TextStyle(fontSize: 18, color: Colors.grey),
),
SizedBox(height: 8),
Text(
'Share content from other apps to see it here',
style: TextStyle(color: Colors.grey),
textAlign: TextAlign.center,
),
],
),
)
: ListView.builder(
itemCount: _shareHistory.length,
itemBuilder: (context, index) {
final shareData = _shareHistory[index];
final isInitial = shareData == _initialShare;
return _buildShareDataCard(
shareData,
isInitial: isInitial,
);
},
),
),
],
),
),
);
}
}