Tinyrack

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

AxisValuesDefault
languageAny identifier handled by the configured highlighterplain text
Highlighterhighlighter prop, TRCodeHighlighterProvider, nonenone
Syntax themeDefined by the application highlighter for each brightnessplain text colors
wraptrue, falsefalse

Install

Add the package, then import its public library.

flutter pub add tinyrack_ui
import '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.

AxisTypeDefaultDescription
codeStringrequiredSource text to display.
languageString?nullLanguage identifier passed to the highlighter; omit it for plain text.
highlighterTRCodeHighlighter?provider valueOverrides the provider for this block.
onHighlightFailureValueChanged<TRCodeHighlightFailure>?provider valueReceives noHighlighter, unsupportedLanguage, and highlightFailed.
wrapboolfalseWraps long lines instead of exposing horizontal scrolling.