receive_sharing_intent
receive_sharing_intent copied to clipboard
how to lunch on specific screen
By default, new share intent opening on main() function and the code will be put here. what about if my code to handle intent is putted on other file ?
@leyriel hope you found the solution to this. I'm just about to start implementing this plugin and I too need to handle intent from a specific screen.
I have a requirement for this feature and haven't been able to find a solution yet - would really appreciate any pointers if there are any workaround to achieve default route or conditional routing to specific screen after receiving shared intent request
A kind of workaround for this that you can do is to navigate to the page that you want from the main() function. The problem is that you don't have the context on there to be able to navigate so you need to do some prep and add a GlobalKey to your MaterialApp like this :
final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();
Widget build(BuildContext context) {
MaterialApp(
navigatorKey: navigatorKey,
home: Login(),
routes: {
'/login': (BuildContext context) => Login(),
'/home': (BuildContext context) => Homepage(),
},);
}
Then in your ReceiveSharingIntent Method inside the initState function you can call something like
navigatorKey.currentState.pushNamed('/HomePage');
Then ususally we need to send data to the page with the files themselves or some data inside it, for this you need to first create a class :
class Arguments{
final List<File> fileList;
Arguments(this.fileList);
}
and then you can call the pushNamed with the data like this :
navigatorKey.currentState.pushNamed('/mailEditor', arguments: Arguments(fileList));
Finally in your Receiving Widget in the build method, you can call
final Argumentsargs = ModalRoute.of(context).settings.arguments;
And you will get the list of file in this case.
Hope it helps :)
Btw, you wrote lunch in the title instead of launch :p
ReceiveSharingIntent Method inside the initState function
do I have to define this in all pages? when app is already open to some page x, how would i be able to get ReceiveSharingIntent on that page? sorry i am still struggling to implement a clean solution
Hello, through my testing i wasn't able to put the ReceiveSharingIntent method anywhere else than the main.dart file, i tried to do it somewhere else but it just doesn't work for me. Since the main.dart file is the entry point of your application, it's going through this and register the DataStreamSubscription at the start of the app, so from there you can call it at any time ! I wouldn't say my solution is the "clean one", i was struggling as well with this and it's the only way i was able to make it work, so i just share :)
This work form me also, I suggest to use a global state provider instead of widget arguments because is more easy to implement in some cases :)
Also I suggest to close this issue because if you want to lunch on specific screen you only have to move your launch to a desired screen with your favorite movie :P
A way i was able to do this is by adding this in the initstate. So the function gets called after the home page has been built.
WidgetsBinding.instance?.addPostFrameCallback(
(timeStamp) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return SampleScreen();
},
),
);
},
But with this approach of running it the main and then pushing to route, how do you dispose controller?
_intentDataStreamSubscription.cancel();
how do you know when to do this?
A way could be to also pass the _intentDataStreamSubscription
as argument to the destination, so that you can cancle()
when you are done with it.