mirror of
https://github.com/foss42/apidash.git
synced 2025-06-06 19:38:58 +08:00
190 lines
4.8 KiB
Dart
190 lines
4.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:highlighter/highlighter.dart' show highlight, Node;
|
|
import 'package:flutter_highlighter/themes/xcode.dart';
|
|
|
|
class CodeHighlight extends StatelessWidget {
|
|
late final String source;
|
|
|
|
/// [All available languages](https://github.com/predatorx7/highlight/tree/master/highlight/lib/languages)
|
|
final String? language;
|
|
|
|
/// [All available themes](https://github.com/predatorx7/highlight/blob/master/flutter_highlighter/lib/themes)
|
|
final Map<String, TextStyle> theme;
|
|
|
|
/// Padding
|
|
final EdgeInsetsGeometry padding;
|
|
|
|
/// Specify text styles such as font family and font size
|
|
final TextStyle? textStyle;
|
|
|
|
CodeHighlight({
|
|
super.key,
|
|
required String input,
|
|
this.language,
|
|
this.textStyle,
|
|
this.padding = const EdgeInsets.all(12),
|
|
this.theme = xcodeTheme,
|
|
int tabSize = 4, // TODO: https://github.com/flutter/flutter/issues/50087
|
|
}) {
|
|
if (input.startsWith("\t")) {
|
|
source = input.replaceAll('\t', ' ' * tabSize);
|
|
} else {
|
|
source = input;
|
|
}
|
|
}
|
|
|
|
List<TextSpan> _convert(List<Node> nodes) {
|
|
List<TextSpan> spans = [];
|
|
var currentSpans = spans;
|
|
List<List<TextSpan>> stack = [];
|
|
|
|
_traverse(Node node) {
|
|
if (node.value != null) {
|
|
currentSpans.add(node.className == null
|
|
? TextSpan(text: node.value)
|
|
: TextSpan(text: node.value, style: theme[node.className!]));
|
|
} else if (node.children != null) {
|
|
List<TextSpan> tmp = [];
|
|
currentSpans
|
|
.add(TextSpan(children: tmp, style: theme[node.className!]));
|
|
stack.add(currentSpans);
|
|
currentSpans = tmp;
|
|
|
|
node.children!.forEach((n) {
|
|
_traverse(n);
|
|
if (n == node.children!.last) {
|
|
currentSpans = stack.isEmpty ? spans : stack.removeLast();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
for (var node in nodes) {
|
|
_traverse(node);
|
|
}
|
|
|
|
return spans;
|
|
}
|
|
|
|
static const _rootKey = 'root';
|
|
static const _defaultFontColor = Color(0xff000000);
|
|
static const _defaultBackgroundColor = Color(0xffffffff);
|
|
|
|
// TODO: dart:io is not available at web platform currently
|
|
// See: https://github.com/flutter/flutter/issues/39998
|
|
// So we just use monospace here for now
|
|
static const _defaultFontFamily = 'monospace';
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
var _textStyle = TextStyle(
|
|
fontFamily: _defaultFontFamily,
|
|
color: theme[_rootKey]?.color ?? _defaultFontColor,
|
|
);
|
|
if (textStyle != null) {
|
|
_textStyle = _textStyle.merge(textStyle);
|
|
}
|
|
|
|
return Padding(
|
|
//color: theme[_rootKey]?.backgroundColor ?? _defaultBackgroundColor,
|
|
padding: padding,
|
|
child: SelectableText.rich(
|
|
TextSpan(
|
|
style: _textStyle,
|
|
children:
|
|
_convert(highlight.parse(source, language: language).nodes!),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/*
|
|
CodeHighlight(
|
|
input: encoder.convert(requestHeaders),
|
|
language: 'json',
|
|
textStyle: kCodeStyle,
|
|
)
|
|
*/
|
|
|
|
const code =
|
|
'''// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
void main() => runApp(MyApp());
|
|
|
|
class MyApp extends StatelessWidget {
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
title: 'Flutter Demo',
|
|
debugShowCheckedModeBanner: false,
|
|
theme: ThemeData(
|
|
primarySwatch: Colors.blue,
|
|
),
|
|
home: const MyHomePage(title: 'Flutter Demo Home Page'),
|
|
);
|
|
}
|
|
}
|
|
|
|
class MyHomePage extends StatefulWidget {
|
|
final String title;
|
|
|
|
const MyHomePage({
|
|
Key? key,
|
|
required this.title,
|
|
}) : super(key: key);
|
|
|
|
@override
|
|
State<MyHomePage> createState() => _MyHomePageState();
|
|
}
|
|
|
|
class _MyHomePageState extends State<MyHomePage> {
|
|
int _counter = 0;
|
|
|
|
void _incrementCounter() {
|
|
setState(() {
|
|
_counter++;
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(widget.title),
|
|
),
|
|
body: Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const Text(
|
|
'You have pushed the button this many times:',
|
|
),
|
|
Text(
|
|
'\$_counter',
|
|
style: Theme.of(context).textTheme.headlineMedium,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
onPressed: _incrementCounter,
|
|
tooltip: 'Increment',
|
|
child: const Icon(Icons.add),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
''';
|
|
|
|
const code2 = '''void main() {
|
|
for (var i = 0; i < 4; i++) {
|
|
print('hello \$i');
|
|
}
|
|
}
|
|
''';
|