Code Block

Syntax-highlighted code block with copy button and language label. Renders code from AI responses with proper formatting and one-tap clipboard copy.

Preview

Preview
dart 📋 Copy
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); }

Installation

$ flai add code_block

Import

import 'package:my_app/flai/components/code_block/code_block.dart';

Usage

FlaiCodeBlock(
  code: '''
void main() {
  runApp(const MyApp());
}''',
  language: 'dart',
)

// Without language detection
FlaiCodeBlock(
  code: 'npm install @flai/cli',
)

// With custom action on copy
FlaiCodeBlock(
  code: myCodeString,
  language: 'python',
  onCopy: () {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('Copied!')),
    );
  },
)

Properties

PropertyTypeDefaultDescription
code String required The code string to display.
language String? null Language label (e.g., 'dart', 'python'). Shown in header.
onCopy VoidCallback? null Called after code is copied. Clipboard copy is automatic.
showLineNumbers bool false Whether to show line numbers in the gutter.

Features