Themes
The 8 built-in themes side by side, theming the Flutter widgets app-wide, adjusting or building a theme, and importing a VS Code theme.
A ValTheme maps scopes such as keyword or string to a Style. The same
theme drives the Flutter widgets and the HTML output.
Built-in themes
Section titled “Built-in themes”







Each picture is CodeView drawing this sample in one theme:
import 'package:material_ui/material_ui.dart';import 'package:val_highlight/languages/dart.dart';import 'package:val_highlight_flutter/val_highlight_flutter.dart';
const source = r'''/// A greeting, repeated [count] times.@immutableclass Greeter { const Greeter(this.name, {this.count = 3});
final String name; final int count;
// One line per greeting. String greet() => 'Hi, $name!\n' * count; bool get isEmpty => name.isEmpty || count < 1;}''';
class ThemeSample extends StatelessWidget { const ThemeSample({super.key, required this.theme});
final ValTheme theme;
@override Widget build(BuildContext context) { return CodeView(source, language: dartLanguage, theme: theme); }}| Theme | Import | Name | Dark | Text on background | Description |
|---|---|---|---|---|---|
lightTheme |
themes/light.dart |
light |
no | #2b2f36 on #fafafb |
The default light theme. |
darkTheme |
themes/dark.dart |
dark |
yes | #d6dae0 on #16181d |
The default dark theme. |
sepiaTheme |
themes/sepia.dart |
sepia |
no | #3b3226 on #f6efe1 |
A warm, paper-like light theme. |
dimTheme |
themes/dim.dart |
dim |
yes | #c4c8ce on #22262c |
A soft, low-glare dark theme. |
midnightTheme |
themes/midnight.dart |
midnight |
yes | #d8e0f0 on #0e1628 |
A deep blue dark theme. |
forestTheme |
themes/forest.dart |
forest |
yes | #d6e2d0 on #15201a |
A green-tinted dark theme. |
highContrastLightTheme |
themes/high_contrast_light.dart |
high-contrast-light |
no | #000000 on #ffffff |
A high-contrast light theme: every colour has at least 7:1 contrast. |
highContrastDarkTheme |
themes/high_contrast_dark.dart |
high-contrast-dark |
yes | #ffffff on #000000 |
A high-contrast dark theme: every colour has at least 7:1 contrast. |
Import one from package:val_highlight/themes/<file>, or all of them, with
the list allThemes, from themes/all.dart. The package’s tests check every
built-in theme: each colour has a contrast ratio of at least 4.5:1 against
its background (WCAG AA), 7:1 in the two high-contrast themes; every theme
styles the same scopes as lightTheme; and dark matches the background.
In Flutter
Section titled “In Flutter”By default, CodeView, CodeField and HighlightTextController use
lightTheme in a light app and darkTheme in a dark one. Pick a theme for
one widget with theme, or for an editor with the controller’s theme:
CodeView(source, language: dartLanguage, theme: midnightTheme)
final controller = HighlightTextController( language: dartLanguage, theme: sepiaTheme,);App-wide defaults
Section titled “App-wide defaults”Add a CodeTheme to your ThemeData.extensions to set defaults once.
Anything set on a widget wins.
const codeTheme = CodeTheme( light: sepiaTheme, dark: dimTheme, textStyle: TextStyle(fontFamily: 'JetBrains Mono'), lineNumbers: true,);
MaterialApp( theme: ThemeData(extensions: const [codeTheme]), darkTheme: ThemeData( brightness: Brightness.dark, extensions: const [codeTheme], ), home: const MyHome(),)CodeTheme is read from the ThemeData in effect, so add it to both
theme and darkTheme when you set both. padding and lineNumbers apply
to CodeView; the themes and textStyle to every widget. A font family only
applies if your app bundles the font or the platform has it.
CodeTheme.resolve(context) returns the ValTheme a widget would use, for
painting a matching background. The default code text style is the
monospace family — with Menlo, Consolas, Roboto Mono and Courier New as
fallbacks — at 14 pixels and a line height of 1.5, with letter and word
spacing fixed at zero so an app-wide text theme can’t stretch the code.
Adjust a theme
Section titled “Adjust a theme”extend returns a new theme: root is merged over the current root style,
and each entry in styles replaces the style for that scope.
final mine = lightTheme.extend( name: 'mine', styles: { Scopes.keyword: const Style(color: 0xFFE8590C, bold: true), },);copyWith replaces whole fields instead of merging them.
Build a theme
Section titled “Build a theme”Colours are ARGB integers, so a theme can be const. A token’s style is its
scopes’ styles merged from the outside in, over root, so unset fields
inherit.
const myTheme = ValTheme( name: 'my-theme', dark: true, root: Style(color: 0xFFE6E6E6, background: 0xFF1E1E2E), styles: { Scopes.comment: Style(color: 0xFF7F849C, italic: true), Scopes.string: Style(color: 0xFFA6E3A1), Scopes.number: Style(color: 0xFFFAB387), Scopes.keyword: Style(color: 0xFFCBA6F7, bold: true), Scopes.type: Style(color: 0xFFF9E2AF), Scopes.function: Style(color: 0xFF89B4FA), },);Style has color, background, bold, italic, underline and
strikethrough. Set dark: true on a dark theme: the widgets choose their
line-highlight colours by it.
Scope lookup cascades: a token in keyword.control.flow uses that scope’s
style if the theme has one, then keyword.control, then keyword. A new
scope from your own grammar rules needs only a theme entry. The
standard scopes are constants on Scopes.
Import a VS Code theme
Section titled “Import a VS Code theme”ValTheme.fromVsCodeJson reads the text of a VS Code colour theme file —
comments and trailing commas allowed — and ValTheme.fromVsCode the decoded
JSON object.
// Flutter, from an asset:final theme = ValTheme.fromVsCodeJson( await rootBundle.loadString('assets/my-color-theme.json'),);
// Dart, from a file:final theme = ValTheme.fromVsCodeJson( File('my-color-theme.json').readAsStringSync(),);The importer takes the root style from colors['editor.foreground'] and
colors['editor.background'], dark from the theme’s type (or from the
background’s brightness when there is none), and the scope styles from
tokenColors, matching TextMate scopes the way VS Code does. The
scope table shows which TextMate scopes each
val_highlight scope reads. The theme’s own name is kept unless you pass
name. An include of another theme file is not followed; merge included
files first.
API reference
Section titled “API reference”ValTheme
Section titled “ValTheme”A color theme: a root style plus a style per scope.
Scope lookup cascades: title.function.call tries title.function.call,
then title.function, then title.
const ValTheme({ required String name, Style root = const Style(), Map<String, Style> styles = const {}, bool dark = false,})Creates a theme.
| Field | Type | Default | Description |
|---|---|---|---|
name |
String |
required | Theme name. |
root |
Style |
const Style() |
Base style for all code, including the background color. |
styles |
Map<String, Style> |
const {} |
Style per scope. |
dark |
bool |
false |
Whether this is a dark theme. |
| Member | Type | Description |
|---|---|---|
ValTheme.fromVsCode(Map<String, Object?> json, {String? name}) |
ValTheme |
Builds a theme from a decoded VS Code color theme (the JSON object of a *-color-theme.json file). |
ValTheme.fromVsCodeJson(String source, {String? name}) |
ValTheme |
Like ValTheme.fromVsCode, from the text of a theme file. Comments and trailing commas (JSONC) are allowed. |
lookup(String scope) |
Style? |
Style for scope alone, using the cascade, without inheritance. |
resolve(ScopeTable table) |
List<Style> |
Effective style of every stack in table, indexed by stack id. |
copyWith({…}) |
ValTheme |
Returns a copy with the given fields replaced. |
extend({String? name, Style? root, Map<String, Style>? styles}) |
ValTheme |
Returns a theme built on this one. |
toCss({String classPrefix = 'vh-', String rootClass = 'vh'}) |
String |
CSS for this theme, matching the classes written by HtmlRenderer. |
ValTheme.fromVsCode: more
Uses the editor foreground and background, the theme type for
dark, and tokenColors, matching TextMate scopes the way VS Code
does. name defaults to the theme’s own name.
resolve: more
Each stack inherits from its parent, starting from root.
extend: more
root is merged over the current root. Each entry in styles replaces
the style for that scope.
toCss: more
rootClass is the class of the wrapping element. Scope a.b maps to
class <classPrefix>a-b.
Visual style for one scope. Unset fields inherit from the enclosing scope.
Colors are ARGB integers such as 0xFF1E7A3A, so styles are const and
cost nothing to convert on any platform.
const Style({ int? color, int? background, bool? bold, bool? italic, bool? underline, bool? strikethrough,})Creates a style.
| Field | Type | Description |
|---|---|---|
color |
int? |
Foreground color as ARGB. |
background |
int? |
Background color as ARGB. |
bold |
bool? |
Bold text. |
italic |
bool? |
Italic text. |
underline |
bool? |
Underlined text. |
strikethrough |
bool? |
Struck-through text. |
| Member | Type | Description |
|---|---|---|
isEmpty |
bool |
Whether every field is unset. |
merge(Style? other) |
Style |
Returns this style with the set fields of other applied on top. |
copyWith({…}) |
Style |
Returns a copy with the given fields replaced. |
toCss() |
String |
CSS declarations for this style, such as color:#1e7a3a;. |
CodeTheme
Section titled “CodeTheme”App-wide defaults for code widgets, set once on ThemeData.extensions.
MaterialApp( theme: ThemeData(extensions: [CodeTheme(light: myTheme)]),)Anything set directly on a widget wins over these defaults.
const CodeTheme({ ValTheme? light, ValTheme? dark, TextStyle? textStyle, EdgeInsetsGeometry? padding, bool? lineNumbers,})Creates code defaults.
| Field | Type | Description |
|---|---|---|
light |
ValTheme? |
Theme used when the app is light. Defaults to lightTheme. |
dark |
ValTheme? |
Theme used when the app is dark. Defaults to darkTheme. |
textStyle |
TextStyle? |
Base text style for code, merged over the default monospace style. |
padding |
EdgeInsetsGeometry? |
Padding around code blocks. |
lineNumbers |
bool? |
Whether code blocks show line numbers. |
| Member | Type | Description |
|---|---|---|
static maybeOf(BuildContext context) |
CodeTheme? |
The nearest CodeTheme, if any. |
static resolve(BuildContext context, [ValTheme? override]) |
ValTheme |
The ValTheme to use: override if given, otherwise the app default for the current brightness. |
static textStyleOf(BuildContext context, [TextStyle? override]) |
TextStyle |
The default code text style: monospace, merged with CodeTheme and then override. |
static textStyleOf: more
Letter and word spacing are fixed at zero so an app-wide text theme cannot stretch code or its line numbers.
allThemes
Section titled “allThemes”Every built-in theme.
const List<ValTheme> allThemes