Widget which detects and hyperlinks URL's from a string. I've used this within social media platforms I've created to automatically detect URL's from user posts.
Example: Here is a link to google: https://google.com -> here is a link to google: https://google.com/
import 'package:flutter_linkify/flutter_linkify.dart';
import 'package:url_launcher/url_launcher.dart';
class Flutterlinkify extends StatefulWidget {
const Flutterlinkify(
{Key? key, this.width, this.height, this.text, this.fontSize, required this.color})
: super(key: key);
final double? width;
final double? height;
final String? text;
final double? fontSize;
final Color color;
@override
_FlutterlinkifyState createState() => _FlutterlinkifyState();
}
class _FlutterlinkifyState extends State<Flutterlinkify> {
@override
Widget build(BuildContext context) {
return Container(
child: SelectableLinkify(
onOpen: (link) async {
if (await canLaunch(link.url)) {
await launch(link.url);
} else {
throw 'Could not launch $link';
}
},
text: widget.text!,
style: TextStyle(
color: widget.color,
fontSize: widget.fontSize),
linkStyle: TextStyle(
fontWeight: FontWeight.bold,
color: widget.color,
fontSize: widget.fontSize),
));
}
}