launchLink function

void launchLink({
  1. required BuildContext context,
  2. required LaunchLinkType launchLinkType,
  3. String? account,
  4. String? url,
  5. String? message,
  6. Map<String, String> headers = const <String, String>{},
})

Implementation

void launchLink({
  required BuildContext context,
  required LaunchLinkType launchLinkType,
  String? account,
  String? url,
  String? message,
  Map<String, String> headers = const <String, String>{},
}) async {
  dynamic _url = launchLinkType == LaunchLinkType.genericLink
      ? url.toString()
      : launchLinkType == LaunchLinkType.whatsApp
          ? "https://wa.me${![
              '',
              null
            ].contains(account) ? '/55${account?.replaceAll(RegExp(r'[^0-9]'), '')}' : ''}/?text=${Uri.encodeComponent(message.toString())}"
          : 'https://${launchLinkType == LaunchLinkType.instagram ? 'instagram' : 'facebook'}.com/$account';

  bool? _canLaunch;
  if ([
    LaunchLinkType.facebook,
    LaunchLinkType.instagram,
  ].contains(launchLinkType)) {
    _canLaunch = await canLaunch(_url.toString());
  } else {
    _canLaunch = true;
  }

  if (_canLaunch) {
    await launch(
      _url,
      universalLinksOnly: true,
      headers: headers,
    );
  } else {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return const AlertDialog(
          title: Text("Erro ao acessar o perfil"),
          content: Text("App não instalado."),
        );
      },
    );
  }
}