import 'package:flutter/widgets.dart'; import 'package:url_launcher/url_launcher.dart'; /// Opens one already-filtered link target; true when the platform accepted it. typedef ChatLinkLauncher = Future Function(Uri uri); /// Opens [uri] outside the app: the system browser or mail client natively, a /// new tab on the web. Future launchChatLinkExternally(Uri uri) => launchUrl(uri, mode: LaunchMode.externalApplication); /// Supplies the launcher transcript links use; tests install a fake. Without a /// scope, links open with [launchChatLinkExternally]. class ChatLinkScope extends InheritedWidget { const ChatLinkScope({ super.key, required this.launcher, required super.child, }); final ChatLinkLauncher launcher; static ChatLinkLauncher of(BuildContext context) => context.getInheritedWidgetOfExactType()?.launcher ?? launchChatLinkExternally; @override bool updateShouldNotify(ChatLinkScope oldWidget) => launcher != oldWidget.launcher; } /// Launches [uri] with the scope's launcher, reporting a refusal or a failed /// platform call instead of letting it escape as an unhandled error. Future openChatLink(BuildContext context, Uri uri) async { final launcher = ChatLinkScope.of(context); try { if (!await launcher(uri)) debugPrint('Link not opened: $uri'); } on Object catch (error) { debugPrint('Link not opened: $uri ($error)'); } }