Code Block
Present multi-line code on a scrollable monospace surface.
Present multi-line code with optional, theme-aware syntax highlighting and explicit wrapping.
Contract
| Axis | Values | Default |
|---|---|---|
language | Any identifier handled by the configured highlighter | plain text |
Highlighter | highlighter prop, TRCodeHighlighterProvider, none | none |
Syntax theme | Defined by the application highlighter for each brightness | plain text colors |
wrap | true, false | false |
Install
Add the package, then import its public library.
flutter pub add tinyrack_uiimport 'package:tinyrack_ui/tinyrack_ui.dart';Adapt the syntax engine used by the application to TRCodeHighlighter, then provide it above the code blocks. The highlighter owns language support, token styles, and light and dark colors; returning null marks a language as unsupported.
import 'package:material_ui/material_ui.dart';
import 'package:tinyrack_ui/tinyrack_ui.dart';
final dartKeywords = RegExp(r'\b(class|const|final|return|void)\b');
Future<TRCodeHighlightResult?> appCodeHighlighter(
TRCodeHighlightRequest request,
) async {
if (request.language != 'dart') return null;
final keywordColor = request.brightness == Brightness.dark
? Colors.lightBlueAccent
: Colors.blue;
final spans = <TextSpan>[];
var offset = 0;
for (final match in dartKeywords.allMatches(request.code)) {
if (match.start > offset) {
spans.add(TextSpan(text: request.code.substring(offset, match.start)));
}
spans.add(
TextSpan(
text: match.group(0),
style: TextStyle(color: keywordColor),
),
);
offset = match.end;
}
if (offset < request.code.length) {
spans.add(TextSpan(text: request.code.substring(offset)));
}
return TRCodeHighlightResult(span: TextSpan(children: spans));
}
void main() {
runApp(
TRCodeHighlighterProvider(
highlighter: appCodeHighlighter,
child: const App(),
),
);
}
class App extends StatelessWidget {
const App({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: TinyrackTheme.light(),
darkTheme: TinyrackTheme.dark(),
home: const Scaffold(
body: TRCodeBlock(
code: 'final status = "healthy";',
language: 'dart',
),
),
);
}
}Playground
Usage
import 'package:material_ui/material_ui.dart';
import 'package:tinyrack_ui/tinyrack_ui.dart';
const TRCodeBlock(
code: "final status = 'healthy';",
language: 'dart',
)Examples
Highlighted Dart permalink
Pass a language after configuring a highlighter to render theme-aware syntax colors.
Display modes permalink
Omit language for plain text, choose an identifier supported by the highlighter, and enable wrap for constrained layouts.
Per-block override permalink
Pass highlighter directly when one block needs different language support or token colors than its provider.
Trailing action permalink
Pass trailing to pin an action to the top-trailing corner, clear of the code. An unwrapped block also pans while a pointer drags past its edge, so a selection can reach the end of a long line.
API
Missing highlighters, unsupported languages, and thrown errors keep the original source visible. Handle these outcomes with onHighlightFailure on a block or provider.
| Axis | Type | Default | Description |
|---|---|---|---|
code | String | required | Source text to display. |
language | String? | null | Language identifier passed to the highlighter; omit it for plain text. |
highlighter | TRCodeHighlighter? | provider value | Overrides the provider for this block. |
onHighlightFailure | ValueChanged<TRCodeHighlightFailure>? | provider value | Receives noHighlighter, unsupportedLanguage, and highlightFailed. |
wrap | bool | false | Wraps long lines instead of exposing horizontal scrolling. |