openLegalDocument function

Future<void> openLegalDocument(
  1. BuildContext context,
  2. LegalDocument document
)

Opens document in the browser, in the app's language.

Every legal link in the app goes through here, so they all use the same URL and launch mode. When no app can open the page, a snackbar gives the address instead of the tap silently doing nothing.

Implementation

Future<void> openLegalDocument(
  BuildContext context,
  LegalDocument document,
) async {
  final url = document.urlFor(context);
  var opened = false;
  try {
    opened = await legalDocumentLauncher(url);
  } catch (e) {
    dev.log('Could not open $url: $e', name: 'openLegalDocument');
  }
  if (opened || !context.mounted) return;
  ScaffoldMessenger.maybeOf(context)?.showSnackBar(
    SnackBar(
      content: Text(AppLocalizations.of(context).legalOpenFailed('$url')),
    ),
  );
}