BetterText constructor

BetterText(
  1. String text, {
  2. Key? key,
  3. TextStyle? defaultStyle,
  4. Map<String, TextStyle> styles = const {},
  5. Map<String, VoidCallback> actions = const {},
  6. Map<String, Color> colors = const {},
  7. Locale? locale,
  8. int? maxLines,
  9. TextOverflow overflow = TextOverflow.ellipsis,
  10. Color? selectionColor,
  11. SelectionRegistrar? selectionRegistrar,
  12. bool softWrap = true,
  13. StrutStyle? strutStyle,
  14. TextAlign textAlign = TextAlign.start,
  15. TextDirection? textDirection,
  16. TextHeightBehavior? textHeightBehavior,
  17. TextScaler textScaler = TextScaler.noScaling,
  18. TextWidthBasis textWidthBasis = TextWidthBasis.parent,
})

A widget that displays styled and interactive text.

BetterText allows you to easily create rich text with custom styles, colors, and interactive elements. It supports markdown-like syntax for applying styles and actions to specific parts of the text.

Example usage:

BetterText(
  'Hello, {bold world}! {red Click me}',
  styles: {'custom': TextStyle(fontStyle: FontStyle.italic)},
  actions: {'tap': () => print('Tapped!')},
  colors: {'primary': Colors.blue},
)

text is the string to be displayed, which can include style tokens in curly braces. defaultStyle is the base style for the text. styles is a map of custom style tokens to TextStyle objects. actions is a map of action tokens to callback functions. colors is a map of color tokens to Color objects.

The widget also supports standard text properties like textAlign, maxLines, etc.

Default tokens available:

  • Font weights: 'thin', 'extraLight', 'light', 'regular', 'medium', 'semibold', 'bold', 'extraBold', 'heavy'
  • Decorations: 'italic', 'underline', 'strike'
  • Weight shorthands: '*' (bold), '/' (italic)
  • Decoration shorthands: '_' (underline), '~' (strikethrough)
  • Colors: 'red', 'blue', 'green', 'yellow', 'orange', 'purple', 'pink', 'brown', 'grey', 'black', 'white'

Implementation

BetterText(
  this.text, {
  super.key,
  this.defaultStyle,
  this.styles = const {},
  this.actions = const {},
  this.colors = const {},
  // -- Standard rich text properties
  this.locale,
  this.maxLines,
  this.overflow = TextOverflow.ellipsis,
  this.selectionColor,
  this.selectionRegistrar,
  this.softWrap = true,
  this.strutStyle,
  this.textAlign = TextAlign.start,
  this.textDirection,
  this.textHeightBehavior,
  this.textScaler = TextScaler.noScaling,
  this.textWidthBasis = TextWidthBasis.parent,
});