SmartText constructor

const SmartText(
  1. String text, {
  2. Key? key,
  3. SmartTextStyle normalStyle = const SmartTextStyle(style: TextStyle(fontSize: 14, fontWeight: FontWeight.normal, color: Colors.black)),
  4. SmartTextStyle boldStyle = const SmartTextStyle(symbol: "**", style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold, color: Colors.black)),
  5. SmartTextStyle italicStyle = const SmartTextStyle(symbol: "__", style: TextStyle(fontSize: 14, fontWeight: FontWeight.normal, fontStyle: FontStyle.italic)),
  6. SmartTextStyle linkStyle = const SmartTextStyle(style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold, color: Colors.blue, decoration: TextDecoration.underline)),
  7. SmartTextStyle hashtagsStyle = const SmartTextStyle(style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold, color: Colors.blue)),
  8. TextDirection textDirection = TextDirection.ltr,
  9. TextAlign textAlign = TextAlign.left,
  10. int? maxLines,
  11. TextOverflow? overflow,
  12. void onLinkTap(
    1. String url
    )?,
  13. void onHashtagTap(
    1. 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 vertically
  • overflow: optional overflow handling (e.g., TextOverflow.ellipsis)
  • onLinkTap: optional callback invoked with the tapped URL string when a link is tapped
  • onHashtagTap: 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,
});