Skip to content
Talk to an engineerRequest access

Highlighted TextField

Highlight a TextField you already have with HighlightTextController, and give it CodeField's typing behaviour with CodeEditingFormatter.

A Material outlined TextField labelled Query, holding a highlighted SQL query, with the caret after “status”

HighlightTextController is a TextEditingController that highlights its text, so any TextField can use it — with your own InputDecoration, label and border, as in the example. Each edit re-highlights only the lines it can affect.

final controller = HighlightTextController(
language: sqlLanguage,
text: 'SELECT 1;',
);
Parameter What it does
language Required. The grammar to highlight with. Set controller.language later to switch; the text is highlighted again.
text The initial text.
theme A ValTheme. Without one, the controller follows CodeTheme and the app’s brightness. Settable as controller.theme.
registry A LanguageRegistry that resolves languages named inside the text, such as Markdown code fences.

Give the field a monospace style: CodeTheme.textStyleOf(context) is the one the package’s widgets use.

CodeEditingFormatter brings CodeField’s typing to a plain TextField: brackets and quotes that close themselves, typing over a selection to wrap it, and Enter that keeps the indentation. Add it to inputFormatters, as the example does. It takes editing and indent, like CodeField.

Keys are CodeField’s own: a plain TextField gets no Tab, Shift+Tab or Ctrl+/ shortcuts, no bracket marks and no line numbers. When you need those, use CodeField.

A plain TextField colours the whole text, and Flutter lays all of it out on every change. For long text, use CodeField, which colours only the lines on screen, or set controller.window to the zero-based lines to colour:

controller.window = (first: 0, last: 199);

A TextEditingController that highlights its text as you type.

final controller = HighlightTextController(language: dartLanguage);
TextField(controller: controller, maxLines: null, style: monospace);

Edits are highlighted incrementally: only the lines an edit can affect are re-scanned, so typing stays fast in large documents.

HighlightTextController({
String? text,
required Grammar language,
ValTheme? theme,
LanguageRegistry? registry,
})

Creates a controller for language.

theme defaults to CodeTheme or the built-in theme for the app’s brightness. registry resolves languages named inside the text, such as Markdown code fences.

Member Type Description
window ({int first, int last})? Zero-based lines to colour, or null for all of them. Settable.
language Grammar The language of the text. Setting it re-highlights everything. Settable.
theme ValTheme? The theme, or null to follow the app. Settable.
result HighlightResult The highlight result for the current text.
window: more

Laying out a text field with many thousands of styled runs is slow, so for large documents only the visible lines, plus a margin, need colours. CodeField sets this automatically as you scroll.

Applies CodeEditing to text typed into a field driven by a HighlightTextController.

CodeField installs one automatically. Add one to a plain TextField’s inputFormatters to get the same behaviour there:

TextField(
controller: controller,
maxLines: null,
inputFormatters: [CodeEditingFormatter(controller: controller)],
)

It reacts to text changes rather than key presses, so it works the same with hardware keyboards, on-screen keyboards and voice input. Text being composed by an input method is never changed. Each change it makes is a single edit, so one undo reverts it.

CodeEditingFormatter({
required HighlightTextController controller,
CodeEditing editing = const CodeEditing(),
String indent = ' ',
})

Creates a formatter for controller.

Field Type Default Description
controller HighlightTextController required The controller whose highlighting tells strings and comments apart.
editing CodeEditing const CodeEditing() The behaviour to apply.
indent String ' ' One level of indentation, for Enter.