forked from lxm_flutter/FlutterUnit
.
This commit is contained in:
@@ -1,40 +1,42 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'high_light_code.dart';
|
||||
|
||||
import 'highlighter_style.dart';
|
||||
|
||||
class CodeWidget extends StatelessWidget {
|
||||
|
||||
CodeWidget({Key key,@required this.code}):super(key:key);
|
||||
CodeWidget({Key key, @required this.code, this.style, this.fontSize = 13,this.fontFamily})
|
||||
: super(key: key);
|
||||
|
||||
final String code;
|
||||
final HighlighterStyle style;
|
||||
final double fontSize;
|
||||
final String fontFamily;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final SyntaxHighlighterStyle style =
|
||||
Theme.of(context).brightness == Brightness.dark
|
||||
? SyntaxHighlighterStyle.darkThemeStyle()
|
||||
: SyntaxHighlighterStyle.lightThemeStyle();
|
||||
|
||||
Widget body;
|
||||
if (code == null) {
|
||||
return Container();
|
||||
} else {
|
||||
Widget _codeWidget;
|
||||
try{
|
||||
DartSyntaxHighlighter(style).format(code);
|
||||
try {
|
||||
_codeWidget = RichText(
|
||||
text: TextSpan(
|
||||
style: const TextStyle(fontSize: 13.0),
|
||||
children: <TextSpan>[
|
||||
DartSyntaxHighlighter(style).format(code)
|
||||
],),
|
||||
style: TextStyle(fontSize: fontSize,fontFamily: fontFamily),
|
||||
children: <TextSpan>[DartHighlighter(style).format(code)],
|
||||
),
|
||||
);
|
||||
}catch (err){
|
||||
} catch (err) {
|
||||
_codeWidget = Text(code);
|
||||
}
|
||||
body = SingleChildScrollView(
|
||||
child: Container(
|
||||
child: _codeWidget,
|
||||
padding: EdgeInsets.all(10),
|
||||
decoration: BoxDecoration(
|
||||
color: style.backgroundColor ?? Color(0xffF6F8FA),
|
||||
borderRadius: BorderRadius.all(Radius.circular(5.0))),
|
||||
),
|
||||
);
|
||||
}
|
||||
return body;
|
||||
|
||||
@@ -5,69 +5,25 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:string_scanner/string_scanner.dart';
|
||||
|
||||
import 'highlighter_style.dart';
|
||||
|
||||
/// final SyntaxHighlighterStyle style = SyntaxHighlighterStyle.lightThemeStyle();
|
||||
/// DartSyntaxHighlighter(style).format(source)
|
||||
|
||||
class SyntaxHighlighterStyle {//句法高亮样式
|
||||
SyntaxHighlighterStyle({//构造函数
|
||||
this.baseStyle,//基础样式
|
||||
this.numberStyle,//数字的样式
|
||||
this.commentStyle,//注释样式
|
||||
this.keywordStyle,//关键字样式
|
||||
this.stringStyle,//字符串样式
|
||||
this.punctuationStyle,//标点符号样式
|
||||
this.classStyle,
|
||||
this.constantStyle
|
||||
});
|
||||
|
||||
static SyntaxHighlighterStyle lightThemeStyle() {
|
||||
return SyntaxHighlighterStyle(
|
||||
baseStyle: const TextStyle(color: Color(0xFF000000)),//黑色
|
||||
numberStyle: const TextStyle(color: Color(0xFF1565C0)),
|
||||
commentStyle: const TextStyle(color: Color(0xFF9E9E9E)),
|
||||
keywordStyle: const TextStyle(color: Color(0xFF9C27B0)),
|
||||
stringStyle: const TextStyle(color: Color(0xFF43A047)),
|
||||
punctuationStyle: const TextStyle(color: Color(0xFF000000)),
|
||||
classStyle: const TextStyle(color: Color(0xFF512DA8)),
|
||||
constantStyle: const TextStyle(color: Color(0xFF795548))
|
||||
);
|
||||
}
|
||||
|
||||
static SyntaxHighlighterStyle darkThemeStyle() {
|
||||
return SyntaxHighlighterStyle(
|
||||
baseStyle: const TextStyle(color: Color(0xFFFFFFFF)),
|
||||
numberStyle: const TextStyle(color: Color(0xFF1565C0)),
|
||||
commentStyle: const TextStyle(color: Color(0xFF9E9E9E)),
|
||||
keywordStyle: const TextStyle(color: Color(0xFF80CBC4)),
|
||||
stringStyle: const TextStyle(color: Color(0xFF009688)),
|
||||
punctuationStyle: const TextStyle(color: Color(0xFFFFFFFF)),
|
||||
classStyle: const TextStyle(color: Color(0xFF009688)),
|
||||
constantStyle: const TextStyle(color: Color(0xFF795548))
|
||||
);
|
||||
}
|
||||
|
||||
final TextStyle baseStyle;
|
||||
final TextStyle numberStyle;
|
||||
final TextStyle commentStyle;
|
||||
final TextStyle keywordStyle;
|
||||
final TextStyle stringStyle;
|
||||
final TextStyle punctuationStyle;
|
||||
final TextStyle classStyle;
|
||||
final TextStyle constantStyle;
|
||||
}
|
||||
|
||||
abstract class Highlighter { // ignore: one_member_abstracts
|
||||
TextSpan format(String src);
|
||||
}
|
||||
|
||||
//暗黑模式下的高亮样式
|
||||
class DartSyntaxHighlighter extends Highlighter {
|
||||
DartSyntaxHighlighter([this._style]) {
|
||||
class DartHighlighter extends Highlighter {
|
||||
DartHighlighter([this._style]) {
|
||||
_spans = <_HighlightSpan>[];
|
||||
_style ??= SyntaxHighlighterStyle.darkThemeStyle();
|
||||
_style ??= HighlighterStyle.fromColors(HighlighterStyle.lightColor);
|
||||
}
|
||||
|
||||
SyntaxHighlighterStyle _style;
|
||||
HighlighterStyle _style;
|
||||
|
||||
static const List<String> _keywords = <String>[
|
||||
'abstract', 'as', 'assert', 'async', 'await', 'break', 'case', 'catch',
|
||||
@@ -339,7 +295,7 @@ class _HighlightSpan {
|
||||
return src.substring(start, end);
|
||||
}
|
||||
|
||||
TextStyle textStyle(SyntaxHighlighterStyle style) {
|
||||
TextStyle textStyle(HighlighterStyle style) {
|
||||
if (type == _HighlightType.number)
|
||||
return style.numberStyle;
|
||||
else if (type == _HighlightType.comment)
|
||||
|
||||
124
lib/components/code/highlighter_style.dart
Normal file
124
lib/components/code/highlighter_style.dart
Normal file
@@ -0,0 +1,124 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// create by 张风捷特烈 on 2020-04-11
|
||||
/// contact me by email 1981462002@qq.com
|
||||
/// 说明:
|
||||
|
||||
|
||||
class HighlighterStyle {
|
||||
//句法高亮样式
|
||||
const HighlighterStyle(
|
||||
{ //构造函数
|
||||
this.baseStyle, //基础样式
|
||||
this.numberStyle, //数字的样式
|
||||
this.commentStyle, //注释样式
|
||||
this.keywordStyle, //关键字样式
|
||||
this.stringStyle, //字符串样式
|
||||
this.punctuationStyle, //标点符号样式
|
||||
this.classStyle, //类名
|
||||
this.backgroundColor,
|
||||
this.constantStyle});
|
||||
|
||||
static List<int> get lightColor => [
|
||||
0xFF000000, //基础
|
||||
0xFF00b0e8, //数字
|
||||
0xFF9E9E9E, //注释
|
||||
0xFF9C27B0, //关键
|
||||
0xFF43A047, //字符串
|
||||
0xFF000000, //标点符号
|
||||
0xFF3D62F5, //类名
|
||||
0xFF795548, //常量
|
||||
0xffF6F8FA, //背景
|
||||
];
|
||||
|
||||
static List<int> get darkColor => [
|
||||
0xFFFFFFFF, //基础
|
||||
0xFFDF935F, //数字
|
||||
0xFF9E9E9E, //注释
|
||||
0xFF80CBC4, //关键字
|
||||
0xFFB9CA4A, //字符串
|
||||
0xFFFFFFFF, //标点符号
|
||||
0xFF7AA6DA, //类名
|
||||
0xFF795548, //常量
|
||||
0xFF1D1F21, //背景
|
||||
];
|
||||
|
||||
static List<int> get gitHub =>
|
||||
[
|
||||
0xFF333333, //基础
|
||||
0xFF008081, //数字
|
||||
0xFF9D9D8D, //注释
|
||||
0xFF009999, //关键字
|
||||
0xFFDD1045, //字符串
|
||||
0xFF333333, //标点符号
|
||||
0xFF6F42C1, //类名
|
||||
0xFF795548, //常量
|
||||
0xFFF8F8F8, //背景
|
||||
];
|
||||
|
||||
static List<int> get zenburn => [
|
||||
0xFFDCDCDC, //普通字
|
||||
0xFF87C5C8, //数字
|
||||
0xFF8F8F8F, //注释
|
||||
0xFFE4CEAB, //关键字
|
||||
0xFFCC9493, //字符串
|
||||
0xFFDCDCDC, //标点符号
|
||||
0xFFEFEF90, //类名
|
||||
0xFFEF5350, //常量
|
||||
0xFF3F3F3F, //背景
|
||||
];
|
||||
|
||||
static List<int> get mf =>[
|
||||
0xFF707D95, //基础
|
||||
0xFF6897BB, //数字
|
||||
0xFF629755, //注释
|
||||
0xFFCC7832, //关键
|
||||
0xFFF14E9F, //字符串
|
||||
0xFFFFBB33, //标点符号
|
||||
0xFF66CCFF, //类名
|
||||
0xFF9876AA, //常量
|
||||
0xFF2B2B2B //背景
|
||||
];
|
||||
|
||||
factory HighlighterStyle.fromColors(List<int> colors) => HighlighterStyle(
|
||||
baseStyle: TextStyle(
|
||||
color: Color(colors[0]),
|
||||
),
|
||||
numberStyle: TextStyle(
|
||||
color: Color(colors[1]),
|
||||
),
|
||||
commentStyle: TextStyle(
|
||||
color: Color(
|
||||
colors[2],
|
||||
),
|
||||
fontStyle: FontStyle.italic),
|
||||
keywordStyle: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Color(
|
||||
colors[3],
|
||||
),
|
||||
),
|
||||
stringStyle: TextStyle(
|
||||
color: Color(colors[4]),
|
||||
),
|
||||
punctuationStyle: TextStyle(
|
||||
color: Color(colors[5]),
|
||||
),
|
||||
classStyle: TextStyle(
|
||||
color: Color(colors[6]),
|
||||
),
|
||||
constantStyle: TextStyle(
|
||||
color: Color(colors[7]),
|
||||
),
|
||||
backgroundColor: Color(colors[8]),
|
||||
);
|
||||
final TextStyle baseStyle;
|
||||
final TextStyle numberStyle;
|
||||
final TextStyle commentStyle;
|
||||
final TextStyle keywordStyle;
|
||||
final TextStyle stringStyle;
|
||||
final TextStyle punctuationStyle;
|
||||
final TextStyle classStyle;
|
||||
final TextStyle constantStyle;
|
||||
final Color backgroundColor;
|
||||
}
|
||||
Reference in New Issue
Block a user