Skip to content

Commit

Permalink
Update the example app
Browse files Browse the repository at this point in the history
  • Loading branch information
JSUYA committed Jan 21, 2025
1 parent d78c4f3 commit d9a4b38
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions packages/url_launcher/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,29 @@ class _MyHomePageState extends State<MyHomePage> {
}
}

Future<void> _launchInWebViewOrVC(Uri url) async {
Future<void> _launchInBrowserView(Uri url) async {
if (!await launchUrl(url, mode: LaunchMode.inAppBrowserView)) {
throw Exception('Could not launch $url');
}
}

Future<void> _launchInWebView(Uri url) async {
if (!await launchUrl(url, mode: LaunchMode.inAppWebView)) {
throw Exception('Could not launch $url');
}
}

Future<void> _launchInAppWithBrowserOptions(Uri url) async {
if (!await launchUrl(
url,
mode: LaunchMode.inAppBrowserView,
browserConfiguration: const BrowserConfiguration(showTitle: true),
)) {
throw Exception('Could not launch $url');
}
}

Future<void> _launchAsInAppWebViewWithCustomHeaders(Uri url) async {
if (!await launchUrl(
url,
mode: LaunchMode.inAppWebView,
Expand Down Expand Up @@ -93,15 +115,15 @@ class _MyHomePageState extends State<MyHomePage> {
}
}

Future<void> _launchUniversalLinkIos(Uri url) async {
Future<void> _launchUniversalLinkIOS(Uri url) async {
final bool nativeAppLaunchSucceeded = await launchUrl(
url,
mode: LaunchMode.externalNonBrowserApplication,
);
if (!nativeAppLaunchSucceeded) {
await launchUrl(
url,
mode: LaunchMode.inAppWebView,
mode: LaunchMode.inAppBrowserView,
);
}
}
Expand Down Expand Up @@ -167,10 +189,16 @@ class _MyHomePageState extends State<MyHomePage> {
const Padding(padding: EdgeInsets.all(16.0)),
ElevatedButton(
onPressed: () => setState(() {
_launched = _launchInWebViewOrVC(toLaunch);
_launched = _launchInBrowserView(toLaunch);
}),
child: const Text('Launch in app'),
),
ElevatedButton(
onPressed: () => setState(() {
_launched = _launchAsInAppWebViewWithCustomHeaders(toLaunch);
}),
child: const Text('Launch in app (Custom Headers)'),
),
ElevatedButton(
onPressed: () => setState(() {
_launched = _launchInWebViewWithoutJavaScript(toLaunch);
Expand All @@ -186,22 +214,29 @@ class _MyHomePageState extends State<MyHomePage> {
const Padding(padding: EdgeInsets.all(16.0)),
ElevatedButton(
onPressed: () => setState(() {
_launched = _launchUniversalLinkIos(toLaunch);
_launched = _launchUniversalLinkIOS(toLaunch);
}),
child: const Text(
'Launch a universal link in a native app, fallback to Safari.(Youtube)'),
),
const Padding(padding: EdgeInsets.all(16.0)),
ElevatedButton(
onPressed: () => setState(() {
_launched = _launchInWebViewOrVC(toLaunch);
_launched = _launchInWebView(toLaunch);
Timer(const Duration(seconds: 5), () {
closeInAppWebView();
});
}),
child: const Text('Launch in app + close after 5 seconds'),
),
const Padding(padding: EdgeInsets.all(16.0)),
ElevatedButton(
onPressed: () => setState(() {
_launched = _launchInAppWithBrowserOptions(toLaunch);
}),
child: const Text('Launch in app with title displayed'),
),
const Padding(padding: EdgeInsets.all(16.0)),
Link(
uri: Uri.parse(
'https://pub.dev/documentation/url_launcher/latest/link/link-library.html'),
Expand Down

0 comments on commit d9a4b38

Please sign in to comment.