Flutter/Firebase 的各位,大家好
我正在尝试实现一个简单的游戏,该游戏接受 Firebase 动态链接并路由到应用程序的特定页面。
用例:
具有路由器的主页,该路由器将根据动态链接请求将页面路由到应用程序
设想:
用户从手机的网络浏览器点击动态链接,如果用户有一个应用程序并进入特定页面,应用程序就会打开。
我能做什么?
我可以在应用程序中打开特定页面。但是,当用户点击动态链接 url 后,它首先打开应用程序的主页,然后被重定向到子页面。首次登陆首页后,登陆特定页面有1-2秒的延迟。我想要的是 - 省略用户登陆主页,只登陆动态链接的特定页面
**Code Format:**
Home Page : It has some Game
The router has 4 different subpages :
Default Home Page that will show game, and the rest other pages are based on the dynamic link provided Ex: Page_1, Page_2, and Page_3.
**What am I looking help or guidance?**
The suggestion I am looking is to avoid landing in home page (game landing page) when dynamic link is clicked from web url of the mobile.
void main() {
runApp(new MaterialApp(
title: 'Game Name',
home: _MainScreen(),
routes: <String, WidgetBuilder>{
'/NSef': (BuildContext context) => new SocialAppsPage(
"page-1-title",
"page-1"),
'/82AY': (BuildContext context) => new SocialAppsPage(
"page-2-title",
"page-2"),
'/DW7Y': (BuildContext context) => new SocialAppsPage(
"page-3-title",
"page-3"),
'/core': (BuildContext context) => new GameHomePage()
}, ));
}
class _MainScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() => new _MainScreenState();
}
class _MainScreenState extends State<_MainScreen> with WidgetsBindingObserver {
Timer _timerLink;
@override
BuildContext get context => super.context;
@override
void initState() {
WidgetsBinding.instance.addObserver(this);
super.initState();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
_timerLink = new Timer(const Duration(milliseconds: 1), () {
_retrieveDynamicLink();
});
}
}
Future<void> _retrieveDynamicLink() async {
final PendingDynamicLinkData data =
await FirebaseDynamicLinks.instance.retrieveDynamicLink();
final Uri deepLink = data?.link;
if (deepLink != null) {
Navigator.pushNamed(context, deepLink.path);
} else {
Navigator.pushNamed(context, "/core");
}
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
if (_timerLink != null) {
_timerLink.cancel();
}
super.dispose();
}
@override
Widget build(BuildContext context) {
return (new MaterialApp(
title: 'Game Name',
home: Center(
child: CircularProgressIndicator(),
),
navigatorObservers: <NavigatorObserver>[observer],
routes: <String, WidgetBuilder>{
'/NSef': (BuildContext context) => new SocialAppsPage(
"page-1-title",
"Page-1"),
'/82AY': (BuildContext context) => new SocialAppsPage(
"page-2-title",
"Page-2"),
'/DW7Y': (BuildContext context) => new SocialAppsPage(
"page-3-title",
"page-3"),
'/core': (BuildContext context) => new GameHomePage()
},
));
}
}
Any help or suggestions is appreciated.
Thanks!
您可以做的只是在您的第一个屏幕上显示加载屏幕(可能类似于启动屏幕?)。
@override
Widget build(BuildContext context) {
return (new MaterialApp(
title: 'Game',
home: Center(
child: CircularProgressIndicator(),
),
));
}
代码更新:
void main() {
runApp(new MaterialApp(
title: 'Game Name',
home: _MainScreen(),
routes: <String, WidgetBuilder>{
'/NSef': (BuildContext context) => new SocialAppsPage(
"page-1-title",
"page-1"),
'/82AY': (BuildContext context) => new SocialAppsPage(
"page-2-title",
"page-2"),
'/DW7Y': (BuildContext context) => new SocialAppsPage(
"page-3-title",
"page-3"),
'/core': (BuildContext context) => new GameHomePage()
}, ));
}
class _MainScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() => new _MainScreenState();
}
class _MainScreenState extends State<_MainScreen> with WidgetsBindingObserver {
Timer _timerLink;
@override
BuildContext get context => super.context;
bool fromDynamicLink = false;
verifyOrigin() async {
await Future.delayed(Duration(seconds: 1));
if (!fromDynamicLink){
fromDynamicLink = true;
Navigator.pushNamed(context, "/core");
}
}
@override
void initState() {
WidgetsBinding.instance.addObserver(this);
verifyOrigin();
super.initState();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
fromDynamicLink = true;
_retrieveDynamicLink();
}
}
Future<void> _retrieveDynamicLink() async {
final PendingDynamicLinkData data =
await FirebaseDynamicLinks.instance.retrieveDynamicLink();
final Uri deepLink = data?.link;
if (deepLink != null) {
Navigator.pushNamed(context, deepLink.path);
} else {
Navigator.pushNamed(context, "/core");
}
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
if (_timerLink != null) {
_timerLink.cancel();
}
super.dispose();
}
@override
Widget build(BuildContext context) {
return (new Scaffold(
appBar: AppBar(title: Text('Game Name'),
),
body: Center(
child: CircularProgressIndicator(),
),
));
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句