SmartText constructor
const
SmartText(
- String text, {
- Key? key,
- SmartTextStyle normalStyle = const SmartTextStyle(style: TextStyle(fontSize: 14, fontWeight: FontWeight.normal, color: Colors.black)),
- SmartTextStyle boldStyle = const SmartTextStyle(symbol: "**", style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold, color: Colors.black)),
- SmartTextStyle italicStyle = const SmartTextStyle(symbol: "__", style: TextStyle(fontSize: 14, fontWeight: FontWeight.normal, fontStyle: FontStyle.italic)),
- SmartTextStyle linkStyle = const SmartTextStyle(style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold, color: Colors.blue, decoration: TextDecoration.underline)),
- SmartTextStyle hashtagsStyle = const SmartTextStyle(style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold, color: Colors.blue)),
- TextDirection textDirection = TextDirection.ltr,
- TextAlign textAlign = TextAlign.left,
- int? maxLines,
- TextOverflow? overflow,
- void onLinkTap(
- String url
- void onHashtagTap(
- String tag
A lightweight LeafRenderObjectWidget that renders stylable, tappable text using a simple markup convention and a custom RenderObject (TextRenderBox).
The widget accepts a plain string and applies styling and interactions for:
- bold segments (default symbol:
"*") - italic segments (default symbol:
"_") - links (detected as URLs and rendered with
linkStyle) - hashtags (rendered with
hashtagsStyle)
Constructor options:
text: the source string to render (may contain markup for bold/italic, plus URLs and hashtags)normalStyle,boldStyle,italicStyle,linkStyle,hashtagsStyle: SmartTextStyle objects that define the visual appearance for each element. Reasonable defaults are provided.textDirection: directionality for layout (defaults to TextDirection.ltr)textAlign: alignment of the text within its box (defaults to TextAlign.left)maxLines: optional maximum number of lines to display; if null, text is unbounded verticallyoverflow: optional overflow handling (e.g., TextOverflow.ellipsis)onLinkTap: optional callback invoked with the tapped URL string when a link is tappedonHashtagTap: optional callback invoked with the tapped hashtag (without the leading '#')
Notes:
- This widget is a const LeafRenderObjectWidget and forwards all properties to a TextRenderBox render object. Updates to the widget update the render object via updateRenderObject.
- Use SmartTextStyle to customize symbols, fonts, colors, and other text styling details.
Implementation
const SmartText(
this.text, {
super.key,
this.normalStyle = const SmartTextStyle(
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.normal,
color: Colors.black,
),
),
this.boldStyle = const SmartTextStyle(
symbol: "**",
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
this.italicStyle = const SmartTextStyle(
symbol: "__",
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.normal,
fontStyle: FontStyle.italic,
),
),
this.linkStyle = const SmartTextStyle(
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: Colors.blue,
decoration: TextDecoration.underline,
),
),
this.hashtagsStyle = const SmartTextStyle(
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: Colors.blue,
),
),
this.textDirection = TextDirection.ltr,
this.textAlign = TextAlign.left,
this.maxLines,
this.overflow,
this.onLinkTap,
this.onHashtagTap,
});