|
| 1 | +import 'package:flutter/services.dart'; |
| 2 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
| 3 | +import 'package:shadcn_flutter/shadcn_flutter.dart'; |
| 4 | + |
| 5 | +import '../../extensions/text_extensions.dart'; |
| 6 | +import '../../theme/cubit/theme_cubit.dart'; |
| 7 | + |
| 8 | +/// Represents a single renderable line inside a [CodeCard]. |
| 9 | +/// |
| 10 | +/// Use one of the concrete subclasses to describe what should be displayed: |
| 11 | +/// - [CodeTextLine] for regular code text (usually rendered in a monospace style) |
| 12 | +/// - [CodeCommentLine] for comment text (usually rendered muted) |
| 13 | +/// - [CodeWidgetLine] for embedding an arbitrary widget (e.g. a button, badge, input) |
| 14 | +/// |
| 15 | +/// This sealed hierarchy also allows [CodeCard] to build a “copy to clipboard” |
| 16 | +/// string by extracting text from supported line types. |
| 17 | +sealed class CodeLine { |
| 18 | + /// Base constructor for all code line types. |
| 19 | + const CodeLine(); |
| 20 | +} |
| 21 | + |
| 22 | +/// A plain text line that is rendered as code. |
| 23 | +/// |
| 24 | +/// This line is included in the copied-to-clipboard output. |
| 25 | +class CodeTextLine extends CodeLine { |
| 26 | + /// Creates a text line containing [text]. |
| 27 | + const CodeTextLine(this.text); |
| 28 | + |
| 29 | + /// The code text to render and include in the clipboard copy. |
| 30 | + final String text; |
| 31 | +} |
| 32 | + |
| 33 | +/// A comment line (e.g. `# ...` or `// ...`) that is rendered in a muted style. |
| 34 | +/// |
| 35 | +/// This line is included in the copied-to-clipboard output. |
| 36 | +class CodeCommentLine extends CodeLine { |
| 37 | + /// Creates a comment line containing [text]. |
| 38 | + const CodeCommentLine(this.text); |
| 39 | + |
| 40 | + /// The comment text to render and include in the clipboard copy. |
| 41 | + final String text; |
| 42 | +} |
| 43 | + |
| 44 | +/// A line that renders a custom [widget] inside the code block. |
| 45 | +/// |
| 46 | +/// By default, widget lines are *not* included in the copied output. |
| 47 | +/// If you want a widget line to contribute text to the clipboard copy, |
| 48 | +/// provide [includeInCopyAs]. |
| 49 | +class CodeWidgetLine extends CodeLine { |
| 50 | + /// Creates a widget line. |
| 51 | + /// |
| 52 | + /// - [widget] is rendered visually inside the code area. |
| 53 | + /// - [includeInCopyAs] (optional) is appended to the copied code string. |
| 54 | + const CodeWidgetLine(this.widget, {this.includeInCopyAs}); |
| 55 | + |
| 56 | + /// The widget to render in place of a text line. |
| 57 | + final Widget widget; |
| 58 | + |
| 59 | + /// Optional text representation used when the user copies the code. |
| 60 | + /// |
| 61 | + /// If null, this line will be omitted from the clipboard output. |
| 62 | + final String? includeInCopyAs; |
| 63 | +} |
| 64 | + |
| 65 | +/// A card widget that displays a titled code snippet with a “copy” action. |
| 66 | +/// |
| 67 | +/// [CodeCard] renders: |
| 68 | +/// - a header row with a terminal icon and [title] |
| 69 | +/// - an optional [description] |
| 70 | +/// - a code-like container that lists [lines] |
| 71 | +/// - a copy button that puts the assembled code text onto the clipboard |
| 72 | +/// |
| 73 | +/// ## Clipboard behavior |
| 74 | +/// The copied string is produced by iterating over [lines]: |
| 75 | +/// - [CodeTextLine.text] is included |
| 76 | +/// - [CodeCommentLine.text] is included |
| 77 | +/// - [CodeWidgetLine.includeInCopyAs] is included if not null |
| 78 | +/// Each included line is joined with `\n`. |
| 79 | +/// |
| 80 | +/// ## Theming |
| 81 | +/// The component adapts its colors based on the app's theme state |
| 82 | +/// (e.g. dark mode), typically read from a [ThemeCubit]. |
| 83 | +/// |
| 84 | +/// ## Example |
| 85 | +/// ```dart |
| 86 | +/// CodeCard( |
| 87 | +/// title: 'Installation', |
| 88 | +/// description: 'Get started in minutes.', |
| 89 | +/// lines: const [ |
| 90 | +/// CodeCommentLine('# Clone the repository'), |
| 91 | +/// CodeTextLine('git clone <REPO_URL>'), |
| 92 | +/// CodeTextLine('cd my_project'), |
| 93 | +/// ], |
| 94 | +/// ) |
| 95 | +/// ``` |
| 96 | +class CodeCard extends StatelessWidget { |
| 97 | + const CodeCard({ |
| 98 | + super.key, |
| 99 | + required this.title, |
| 100 | + this.description, |
| 101 | + required this.lines, |
| 102 | + }); |
| 103 | + |
| 104 | + /// The title shown in the header row. |
| 105 | + final String title; |
| 106 | + |
| 107 | + /// Optional supporting text shown below the title. |
| 108 | + final String? description; |
| 109 | + |
| 110 | + /// The content lines rendered inside the code container. |
| 111 | + /// |
| 112 | + /// Use [CodeTextLine], [CodeCommentLine], and [CodeWidgetLine]. |
| 113 | + final List<CodeLine> lines; |
| 114 | + |
| 115 | + @override |
| 116 | + Widget build(BuildContext context) { |
| 117 | + final isDarkMode = context.watch<ThemeCubit>().state.isDarkMode; |
| 118 | + |
| 119 | + final codeText = lines |
| 120 | + .map((line) { |
| 121 | + if (line is CodeTextLine) return line.text; |
| 122 | + if (line is CodeCommentLine) return line.text; |
| 123 | + if (line is CodeWidgetLine) return line.includeInCopyAs; |
| 124 | + return null; |
| 125 | + }) |
| 126 | + .whereType<String>() |
| 127 | + .join('\n'); |
| 128 | + |
| 129 | + return Card( |
| 130 | + filled: true, |
| 131 | + fillColor: isDarkMode ? Colors.gray[800] : Colors.transparent, |
| 132 | + borderColor: isDarkMode ? Colors.gray[700] : Colors.gray[300], |
| 133 | + padding: EdgeInsets.all(20), |
| 134 | + child: Column( |
| 135 | + mainAxisSize: MainAxisSize.min, |
| 136 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 137 | + children: [ |
| 138 | + Row( |
| 139 | + children: [const Icon(LucideIcons.terminal), Text(title).semiBold], |
| 140 | + ).gap(10), |
| 141 | + if (description != null) ...[Gap(10), Text(description!).muted], |
| 142 | + Gap(24), |
| 143 | + Container( |
| 144 | + width: double.infinity, |
| 145 | + padding: EdgeInsets.symmetric(horizontal: 20, vertical: 30), |
| 146 | + decoration: BoxDecoration( |
| 147 | + color: isDarkMode ? Colors.gray[700] : Colors.gray[900], |
| 148 | + borderRadius: BorderRadius.circular(10), |
| 149 | + ), |
| 150 | + child: Stack( |
| 151 | + children: [ |
| 152 | + Column( |
| 153 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 154 | + children: lines |
| 155 | + .map( |
| 156 | + (line) => (line is CodeWidgetLine |
| 157 | + ? line.widget |
| 158 | + : line is CodeTextLine |
| 159 | + ? Text( |
| 160 | + line.text, |
| 161 | + ).mono.small.setColors(lightColor: Colors.white) |
| 162 | + : line is CodeCommentLine |
| 163 | + ? Text(line.text).mono.small.muted |
| 164 | + : SizedBox.shrink()), |
| 165 | + ) |
| 166 | + .toList(), |
| 167 | + ), |
| 168 | + Positioned( |
| 169 | + right: 0, |
| 170 | + child: IconButton( |
| 171 | + size: ButtonSize.small, |
| 172 | + icon: const Icon(LucideIcons.copy), |
| 173 | + variance: ButtonVariance.secondary, |
| 174 | + onPressed: () async { |
| 175 | + await Clipboard.setData(ClipboardData(text: codeText)); |
| 176 | + }, |
| 177 | + ), |
| 178 | + ), |
| 179 | + ], |
| 180 | + ), |
| 181 | + ), |
| 182 | + ], |
| 183 | + ), |
| 184 | + ); |
| 185 | + } |
| 186 | +} |
0 commit comments