launchUrl function

Future<void> launchUrl(
  1. Uri url, {
  2. bool prefersDeepLink = false,
  3. LaunchOptions options = const LaunchOptions(),
})

Passes url with options to the underlying platform for launching a Custom Tab.

Example:

final theme = ...;
try {
  await launchUrl(
    Uri.parse('https://flutter.cn'),
    options: LaunchOptions(
      barColor: theme.colorScheme.surface,
      onBarColor: theme.colorScheme.onSurface,
      barFixingEnabled: false,
    ),
  );
} catch (e) {
  // If the URL launch fails, an exception will be thrown. (For example, if no browser app is installed on the Android device.)
}

Implementation

Future<void> launchUrl(
  Uri url, {
  bool prefersDeepLink = false,
  LaunchOptions options = const LaunchOptions(),
}) async {
  if (url.scheme != 'http' && url.scheme != 'https') {
    throw ArgumentError.value(
      url,
      'url',
      'must have an http or https scheme.',
    );
  }

  await CustomTabsPlatform.instance.launch(
    url.toString(),
    prefersDeepLink: prefersDeepLink,
    customTabsOptions: options.toCustomTabsOptions(),
    safariVCOptions: options.toSafariViewControllerOptions(),
  );
}