cofounderella/lib/components/external_link_widget.dart

60 lines
1.6 KiB
Dart
Raw Permalink Normal View History

import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show Clipboard, ClipboardData;
import 'package:url_launcher/url_launcher.dart';
import '../utils/helper_dialogs.dart';
class ExternalLinkWidget extends StatelessWidget {
final String url;
final BuildContext context;
const ExternalLinkWidget(
{required this.url, required this.context, super.key});
Future<void> _onTap() async {
bool? confirm = await showConfirmationDialog(
context,
'External Link',
'You are about to open an external link. '
'Please make sure you trust the link before proceeding.\n'
'$url\n'
'Do you want to open this external link?\n\n'
'Hint: Long press to copy the link to your clipboard instead of opening it.',
);
if (confirm == true) {
Uri weblink = Uri.parse(url);
if (!await launchUrl(weblink)) {
if (context.mounted) {
showErrorSnackBar(context, 'Could not launch $weblink');
}
}
}
}
void _onLongPress() {
_copyToClipboard(context, url);
}
void _copyToClipboard(BuildContext context, String text) {
Clipboard.setData(ClipboardData(text: text)).then((_) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Copied $text to clipboard')),
);
});
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: _onTap,
onLongPress: _onLongPress,
child: Text(
url,
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
style: const TextStyle(color: Colors.blue),
),
);
}
}