Open ghost opened 4 years ago
This would be a killer feature! I would love to for example use Navigator.pushReplacementNamed(context, 'address_i_got_from_uni_links');
navigating to specific page in app, without copy-pasting code uni_links handler all over widgets.
@charlesrostaing
The link handling should be done in 2 places:
getInitialUri
. If the app was opened with a uri, it will have the value.getUriLinksStream
. If the app is already open the stream will have a new value with the link in it.
Note: When the user opens a link and your app already exist (in background), Android will first open your app and then you will get the event about the link. This means the user wll see the screen the app was before they moved it to the background, no matter what you do.I hope that helps!
@
@charlesrostaing
The link handling should be done in 2 places:
- When the app starts. Use
getInitialUri
. If the app was opened with a uri, it will have the value.- using
getUriLinksStream
. If the app is already open the stream will have a new value with the link in it. Note: When the user opens a link and your app already exist (in background), Android will first open your app and then you will get the event about the link. This means the user wll see the screen the app was before they moved it to the background, no matter what you do.I hope that helps!
Consider the case of app having Splash screen, Product Listing (HomeScreen), Product details screen & other screens. In this case, the shared urls are of different products which should ideally open in 'Product Details' screen.
If we add listeners in Product Listing, redirection is working fine, either from coming background or app was opened. But, if the user is in Product details & we open different link, nothing will happen unless we add listeners there too.
In such case, which place you suggest of adding listeners? Scenario being, in all cases, regardless of where the user is, redirection should happen, without adding listeners everywhere.
@rohankandwal
If we add listeners in Product Listing, redirection is working fine, either from coming background or app was opened. But, if the user is in Product details & we open different link, nothing will happen unless we add listeners there too.
You can have a common parent widget. So your top widget would be something like MyApp
, which would have the listener (and other "global" stuff), and the build()
method should call the appropriate child builder (HomeScreen
, ProductDetailsScreen
, etc).
I am trying to open a specific screen when a user clicks a URL link. If the user is on my home page or if the app is fully closed, I manage to intercept the link and show the correct page. If the user has navigated to another page of the app and the app is running (in background for example), clicking the link simply opens the app where the user left it. How can i intercept and parse the link to redirect him to the desired page no matter where he might be in the app ? So i have to copy the code on every single screen of the app ?
Thanks
`import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:myapp/app.config.dart'; import 'package:myapp/mobile/screens/login.dart'; import 'package:myapp/mobile/screens/inviteView.dart';
import 'package:uni_links/uni_links.dart';
/// The goal of this splash screen is to give time to the app /// to retrieve the current database or redirect to the login page. /// We may want to add a different splash screen natively to avoid /// having a white screen when the user launches the app class SplashScreen extends StatefulWidget { const SplashScreen({Key key}) : super(key: key);
@override _SplashScreenState createState() => _SplashScreenState(); }
class _SplashScreenState extends State {
Future ready;
Uri _latestUri;
StreamSubscription _sub;
@override void initState() { super.initState(); SystemChannels.textInput.invokeMethod('TextInput.hide'); ready = initPlatformStateForUriUniLinks(); }
@override void dispose() { if (_sub != null) _sub.cancel(); super.dispose(); }
/// An implementation using the [Uri] convenience helpers Future initPlatformStateForUriUniLinks() async {
// Attach a listener to the Uri links stream
_sub = getUriLinksStream().listen((Uri uri) {
if (!mounted) return true;
setState(() {
_latestUri = uri;
});
}, onError: (err) {
if (!mounted) return true;
setState(() {
_latestUri = null;
});
});
}
@override Widget build(BuildContext context) { return FutureBuilder( future: ready, builder: (context, AsyncSnapshot snapshot) {
if (snapshot.hasError) return Center(child: Text('Error validating invitation'));
if (!snapshot.hasData) return Center(child: Text('Loading'));
}
}`