code_field.dart
2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
/// A widget that displays code with syntax highlighting and a copy button.
///
/// The [CodeField] widget takes a [name] parameter which is displayed as a label
/// above the code block, and a [codes] parameter containing the actual code text
/// to display.
///
/// Features:
/// - Displays code in a Material container with rounded corners
/// - Shows the code language/name as a label
/// - Provides a copy button to copy code to clipboard
/// - Visual feedback when code is copied
/// - Themed colors that adapt to light/dark mode
class CodeField extends StatefulWidget {
const CodeField({super.key, required this.name, required this.codes});
final String name;
final String codes;
@override
State<CodeField> createState() => _CodeFieldState();
}
class _CodeFieldState extends State<CodeField> {
bool _copied = false;
@override
Widget build(BuildContext context) {
return Material(
color: Theme.of(context).colorScheme.onInverseSurface,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Row(
children: [
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16.0,
vertical: 8,
),
child: Text(widget.name),
),
const Spacer(),
TextButton.icon(
style: TextButton.styleFrom(
foregroundColor: Theme.of(context).colorScheme.onSurface,
textStyle: const TextStyle(fontWeight: FontWeight.normal),
),
onPressed: () async {
await Clipboard.setData(
ClipboardData(text: widget.codes),
).then((value) {
setState(() {
_copied = true;
});
});
await Future.delayed(const Duration(seconds: 2));
setState(() {
_copied = false;
});
},
icon: Icon(
(_copied) ? Icons.done : Icons.content_paste,
size: 15,
),
label: Text((_copied) ? "Copied!" : "Copy code"),
),
],
),
const Divider(height: 1),
SingleChildScrollView(
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.all(16),
child: Text(widget.codes),
),
],
),
);
}
}