Custom rendering
Turn a highlight result into TextSpans for your own widgets, with theme styles resolved and cached by CodeStyles.

import 'package:material_ui/material_ui.dart';import 'package:val_highlight/languages/json.dart';import 'package:val_highlight/themes/dark.dart';import 'package:val_highlight_flutter/val_highlight_flutter.dart';
/// Highlighted once; the result is plain data and can be kept.final result = const Highlighter().highlight( '{ "status": "paid", "total": 42.5 }', language: jsonLanguage,);
/// Styles per scope stack, cached by `CodeStyles`.final styles = CodeStyles(darkTheme);
/// A chat message with a highlighted line of JSON inside it.class Message extends StatelessWidget { const Message({super.key});
@override Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text('The webhook sent this body:'), const SizedBox(height: 8), DecoratedBox( decoration: BoxDecoration( color: Color(darkTheme.root.background!), borderRadius: BorderRadius.circular(8), ), child: Padding( padding: const EdgeInsets.all(12), child: Text.rich( TextSpan( style: styles.rootStyle(CodeTheme.textStyleOf(context)), children: documentSpans(result, styles), ), ), ), ), ], ), ); }}When CodeView is more than you need — a line of code inside a chat message,
a cell in a table, a widget of your own — build the spans yourself. These are
the pieces CodeView and HighlightTextController are made of.
The pieces
Section titled “The pieces”- A
HighlightResult, fromHighlighter, or from aCodeHighlighterfor its cache and background isolates. A result is plain data: highlight once and keep it. - A
CodeStylesfor a theme. It resolves each scope stack’s style once, with the inherited styles merged, and caches it. Make one per theme and keep it. - Spans, from one of three functions:
| Function | Gives you |
|---|---|
lineSpans(result, line, styles) |
The spans of one line, without its line break. For one widget per line. |
documentSpans(result, styles) |
Every line, joined with \n. The \r of a \r\n line break is dropped, so it never renders. |
exactSpans(result, styles) |
Spans that cover every character exactly, as a text field needs. It can underline the composing range, and colour only part of the text. |
Plain text gets no style of its own, so it takes the style of the span
around it. Use styles.rootStyle(base) for that: your base style with the
theme’s text colour. The theme’s background is left out — paint it on the
container, as the example does.
textStyleFromStyle converts a single Style from a theme into a
TextStyle, leaving unset fields unset.
API reference
Section titled “API reference”CodeStyles
Section titled “CodeStyles”Text styles for the scope stacks of highlight results, resolved once per stack and cached.
Stack 0 (plain text) gets an empty style so it inherits the root span.
Every other stack gets its full inherited style, so spans can be flat.
The cache grows as a table gains stacks, which lets incremental results
that share one table reuse it.
CodeStyles(ValTheme theme)Creates a style cache for theme.
| Field | Type | Default | Description |
|---|---|---|---|
theme |
ValTheme |
required | The theme being applied. |
| Member | Type | Description |
|---|---|---|
rootStyle(TextStyle base) |
TextStyle |
Root style: the theme’s base color (no background; containers paint that) merged over base. |
of(ScopeTable table, int stack) |
TextStyle |
Style for stack in table. |
textStyleFromStyle
Section titled “textStyleFromStyle”Converts a val_highlight Style to a TextStyle. Unset fields stay
unset, so the result inherits from its parent span.
TextStyle textStyleFromStyle(Style style)lineSpans
Section titled “lineSpans”Spans for line line of result, without the line break.
List<TextSpan> lineSpans(HighlightResult result, int line, CodeStyles styles)documentSpans
Section titled “documentSpans”Spans for every line of result, joined with \n. The \r of a
\r\n line break is dropped, so it never renders.
List<TextSpan> documentSpans(HighlightResult result, CodeStyles styles)exactSpans
Section titled “exactSpans”Spans covering every character of result exactly, as a text field
needs. When composing is a valid range, that range is underlined.
Only code within styled (all of it by default) is coloured; the rest
is plain text, which keeps huge documents cheap to lay out. Adjacent
pieces with the same style share one span.
List<TextSpan> exactSpans( HighlightResult result, CodeStyles styles, { TextRange composing = TextRange.empty, TextRange? styled,})