Skip to content

Commit

Permalink
feat: add ShareReceive widget
Browse files Browse the repository at this point in the history
  • Loading branch information
tamslo committed Jun 8, 2023
1 parent 6dfccef commit f39a5ea
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions app/lib/common/widgets/share_receive.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_sharing_intent/flutter_sharing_intent.dart';
import 'package:flutter_sharing_intent/model/sharing_file.dart';

class ShareReceive extends StatefulWidget {
const ShareReceive({super.key});

@override
State<ShareReceive> createState() => _ShareReceiveState();
}

class _ShareReceiveState extends State<ShareReceive> {
late StreamSubscription _intentDataStreamSubscription;
List<SharedFile>? list;
@override
void initState() {
super.initState();
// For sharing images coming from outside the app while the app is in
// the memory
_intentDataStreamSubscription = FlutterSharingIntent.instance.getMediaStream()
.listen((sharedFiles) {
setState(() {
list = sharedFiles;
});
// ignore: avoid_print
print(
"Shared: getMediaStream ${sharedFiles.map((f) => f.value).join(",")}");
}, onError: (err) {
// ignore: avoid_print
print('getIntentDataStream error: $err');
});

// For sharing images coming from outside the app while the app is closed
FlutterSharingIntent.instance.getInitialSharing().then((sharedFiles) {
// ignore: avoid_print
print(
"Shared: getInitialMedia ${sharedFiles.map((f) => f.value).join(",")}");
setState(() {
list = sharedFiles;
});
});
}

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Container(
margin: EdgeInsets.symmetric(horizontal: 24),
child: Text('Sharing data: \n${list?.join("\n\n")}\n')),
),
),
);
}
@override
void dispose() {
_intentDataStreamSubscription.cancel();
super.dispose();
}
}

0 comments on commit f39a5ea

Please sign in to comment.