Created
August 20, 2024 20:04
-
-
Save ffkev/4a0ae76c2af4751929e5f15b75e6f8be to your computer and use it in GitHub Desktop.
This is sample flutter code for a Text widget which will exhibit hyperlink capabilities such as showing a blue underline in the text when it is hovered on top of
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HyperlinkText extends StatefulWidget { | |
final String text; | |
final String url; | |
HyperlinkText({required this.text, required this.url}); | |
@override | |
_HyperlinkTextState createState() => _HyperlinkTextState(); | |
} | |
class _HyperlinkTextState extends State<HyperlinkText> { | |
bool _isHovered = false; | |
@override | |
Widget build(BuildContext context) { | |
return MouseRegion( | |
onEnter: (_) { | |
setState(() { | |
_isHovered = true; | |
}); | |
}, | |
onExit: (_) { | |
setState(() { | |
_isHovered = false; | |
}); | |
}, | |
child: GestureDetector( | |
onTap: () { | |
// Pass any action parameters here | |
}, | |
child: RichText( | |
text: TextSpan( | |
text: widget.text, | |
style: TextStyle( | |
color: Colors.blue, | |
decoration: _isHovered ? TextDecoration.underline : TextDecoration.none, | |
decorationColor: Colors.blue, | |
decorationThickness: 2.0, | |
height: 1.5, // Adjust the height to add padding above the underline | |
), | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment