85 lines
2.5 KiB
Dart
85 lines
2.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_highlight/flutter_highlight.dart';
|
|
import 'package:flutter_highlight/themes/atom-one-dark.dart';
|
|
|
|
class CodeBlock extends StatelessWidget {
|
|
final String code;
|
|
final String? language;
|
|
|
|
const CodeBlock({
|
|
super.key,
|
|
required this.code,
|
|
this.language,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Container(
|
|
margin: const EdgeInsets.symmetric(vertical: 8),
|
|
decoration: BoxDecoration(
|
|
color: const Color(0xFF282C34),
|
|
borderRadius: BorderRadius.circular(8),
|
|
border: Border.all(color: Colors.grey.shade800),
|
|
),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
// Header with language and copy button
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
|
decoration: BoxDecoration(
|
|
color: Colors.grey.shade900,
|
|
borderRadius: const BorderRadius.only(
|
|
topLeft: Radius.circular(8),
|
|
topRight: Radius.circular(8),
|
|
),
|
|
),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Text(
|
|
language ?? 'code',
|
|
style: TextStyle(
|
|
color: Colors.grey.shade500,
|
|
fontSize: 12,
|
|
),
|
|
),
|
|
InkWell(
|
|
onTap: () {
|
|
Clipboard.setData(ClipboardData(text: code));
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text('Copied to clipboard'),
|
|
duration: Duration(seconds: 1),
|
|
),
|
|
);
|
|
},
|
|
child: Icon(
|
|
Icons.copy,
|
|
size: 16,
|
|
color: Colors.grey.shade500,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
// Code content
|
|
Padding(
|
|
padding: const EdgeInsets.all(12),
|
|
child: HighlightView(
|
|
code,
|
|
language: language ?? 'plaintext',
|
|
theme: atomOneDarkTheme,
|
|
textStyle: const TextStyle(
|
|
fontFamily: 'JetBrainsMono',
|
|
fontSize: 13,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|