code_field
1.0.0
可自定义的代码文本字段支持语法突出显示
现场演示展示一些语言 /主题组合
实验VM DLOX在其在线编辑器中使用CodeField
代码修饰符有助于自动管理凹痕
编辑器包裹在水平滚动容器中以处理长线
在您的flutter项目的pubspec.yaml中,添加以下依赖性:
dependencies :
...
code_text_field : <latest_version>最新版本
在您的库中添加以下导入:
import 'package:code_text_field/code_text_field.dart' ;Codefield小部件与编码器一起使用,该编码器根据语言动态解析文本输入并使用主题映射渲染
import 'package:flutter/material.dart' ;
import 'package:code_text_field/code_text_field.dart' ;
// Import the language & theme
import 'package:highlight/languages/dart.dart' ;
import 'package:flutter_highlight/themes/monokai-sublime.dart' ;
class CodeEditor extends StatefulWidget {
@override
_CodeEditorState createState () => _CodeEditorState ();
}
class _CodeEditorState extends State < CodeEditor > {
CodeController ? _codeController;
@override
void initState () {
super . initState ();
final source = "void main() { n print( " Hello, world! " ); n }" ;
// Instantiate the CodeController
_codeController = CodeController (
text : source,
language : dart,
);
}
@override
void dispose () {
_codeController ? . dispose ();
super . dispose ();
}
@override
Widget build ( BuildContext context) {
return CodeTheme (
data : const CodeThemeData (styles : monokaiSublimeTheme),
child : CodeField (
controller : _codeController ! ,
textStyle : const TextStyle (fontFamily : 'SourceCode' ),
),
);
}
}在这里,Monospace字体源代码Pro已添加到资产文件夹和PubSpec.yaml文件中。
除了语言定义之外,可以在StringMap字段中指定文字样式
_codeController = CodeController (
//...
stringMap : {
"Hello" : TextStyle (fontWeight : FontWeight .bold, color : Colors .red),
"world" : TextStyle (fontStyle : FontStyle .italic, color : Colors .green),
},
);更复杂的回音也可以与模式图一起使用。但是,当使用一种语言时,其Regexes模式优先于模式图和字符串图。
_codeController = CodeController (
//...
patternMap : {
r"B#[a-zA-Z0-9]+b" :
TextStyle (fontWeight : FontWeight .bold, color : Colors .purpleAccent),
},
);dattermap和stringmap均可在不指定语言的情况下使用。
_codeController = CodeController (
text : source,
patternMap : {
r'".*"' : TextStyle (color : Colors .yellow),
r'[a-zA-Z0-9]+(.*)' : TextStyle (color : Colors .green),
},
stringMap : {
"void" : TextStyle (fontWeight : FontWeight .bold, color : Colors .red),
"print" : TextStyle (fontWeight : FontWeight .bold, color : Colors .blue),
},
);可以创建代码修饰符以对特殊的击键反应。默认修饰符将选项卡处理到空间和自动凹痕。这是默认TabModifier的实现
class TabModifier extends CodeModifier {
const TabModifier () : super ( ' t ' );
@override
TextEditingValue ? updateString (
String text, TextSelection sel, EditorParams params) {
final tmp = replace (text, sel.start, sel.end, " " * params.tabSpaces);
return tmp;
}
} const CodeField ({
Key ? key,
required this .controller,
this .minLines,
this .maxLines,
this .expands = false ,
this .wrap = false ,
this .background,
this .decoration,
this .textStyle,
this .padding = EdgeInsets .zero,
this .lineNumberStyle = const LineNumberStyle (),
this .enabled,
this .onTap,
this .readOnly = false ,
this .cursorColor,
this .textSelectionTheme,
this .lineNumberBuilder,
this .focusNode,
this .onChanged,
this .isDense = false ,
this .smartQuotesType,
this .keyboardType,
this .lineNumbers = true ,
this .horizontalScroll = true ,
this .selectionControls,
this .hintText,
this .hintStyle,
}) const LineNumberStyle ({
this .width = 42.0 ,
this .textAlign = TextAlign .right,
this .margin = 10.0 ,
this .textStyle,
this .background,
}); CodeController ({
String ? text,
Mode ? language,
this .patternMap,
this .stringMap,
this .params = const EditorParams (),
this .modifiers = const [
IndentModifier (),
CloseBlockModifier (),
TabModifier (),
],
}) 在Flutter Beta,Dev和Master Channels中引入了对TextEditingController的打破更改。分支Beta应遵守这些更改。