From 569ca61baa0c5329b8b4edf249ba0f9c017453cf Mon Sep 17 00:00:00 2001 From: JamesCullum Date: Wed, 14 Apr 2021 23:13:25 +0200 Subject: [PATCH] Improved example by showing a full sample app --- example/lib/main.dart | 86 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 80 insertions(+), 6 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 6d55e2f..54caf34 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,27 +1,101 @@ +import 'package:app_links/app_links.dart'; import 'package:flutter/material.dart'; /////////////////////////////////////////////////////////////////////////////// -/// This example does nothing. +/// Please make sure to follow the setup instructions below /// /// Please take a look at: /// - example/android/app/main/AndroidManifest.xml for Android. /// /// - example/ios/Runner/Runner.entitlements for Universal Link sample. /// - example/ios/Runner/Info.plist for Custom URL scheme sample. +/// +/// You can launch an intent on an Android Emulator like this: +/// adb shell am start -a android.intent.action.VIEW \ +// -c android.intent.category.BROWSABLE \ +// -d "http://example.com/book/hello-world" /////////////////////////////////////////////////////////////////////////////// void main() { runApp(MyApp()); } -class MyApp extends StatelessWidget { +class MyApp extends StatefulWidget { + @override + _MyAppState createState() => _MyAppState(); +} + +class _MyAppState extends State { + final _navigatorKey = GlobalKey(); + + @override + void initState() { + initDeepLinks(); + super.initState(); + } + + void initDeepLinks() async { + final _appLinks = AppLinks( + onAppLink: (Uri uri) { + openAppLink(uri); + }, + ); + + final lastAppLinkUri = await _appLinks.getLatestAppLink(); + if (lastAppLinkUri != null) { + openAppLink(lastAppLinkUri); + } + } + + void openAppLink(Uri uri) { + _navigatorKey.currentState + .pushNamed(uri.path, arguments: uri.queryParameters); + } + @override Widget build(BuildContext context) { return MaterialApp( - home: Scaffold( - appBar: AppBar(title: const Text('Plugin example app')), - body: Center(child: Text('Foo')), - ), + navigatorKey: _navigatorKey, + initialRoute: "/", + onGenerateRoute: (RouteSettings settings) { + Widget routeWidget = defaultScreen(); + + // Parse route to extract dynamic parameters + var uri = Uri.parse(settings.name); + var firstLevel = + uri.pathSegments.length > 0 ? uri.pathSegments.first : ""; + switch(firstLevel) { + case "book": + if (uri.pathSegments.length > 1) { + // Navigated to /book/:id + routeWidget = customScreen(uri.pathSegments[1]); + } else { + // Navigated to /book without other parameters + routeWidget = customScreen("None"); + } + break; + } + + return MaterialPageRoute( + builder: (context) => routeWidget, + settings: settings, + fullscreenDialog: true, + ); + }, + ); + } + + Widget defaultScreen() { + return Scaffold( + appBar: AppBar(title: const Text('Default Screen')), + body: Center(child: const Text('Launch an intent to get to the second screen')), + ); + } + + Widget customScreen(String bookId) { + return Scaffold( + appBar: AppBar(title: const Text('Second Screen')), + body: Center(child: Text('Opened with parameter: ' + bookId)), ); } }