buildTextSpan method

  1. @override
TextSpan buildTextSpan({
  1. required BuildContext context,
  2. required bool withComposing,
  3. TextStyle? style,
})
override

Builds TextSpan from current editing value.

By default makes text in composing range appear as underlined. Descendants can override this method to customize appearance of text.

Implementation

@override
TextSpan buildTextSpan({required BuildContext context, required bool withComposing, TextStyle? style}) {
  final String t = text;
  final TextStyle base = style ?? const TextStyle();
  if (t.isEmpty || spans.isEmpty) return TextSpan(style: base, text: t);

  final ColorScheme cs = Theme.of(context).colorScheme;
  final List<InlineSpan> children = <InlineSpan>[];
  int runStart = 0;
  TextStyle current = styleAt(0, base, cs);
  for (int i = 1; i <= t.length; i++) {
    final TextStyle st = i < t.length ? styleAt(i, base, cs) : current;
    if (i == t.length || st != current) {
      children.add(TextSpan(text: t.substring(runStart, i), style: current));
      runStart = i;
      current = st;
    }
  }
  return TextSpan(style: base, children: children);
}