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應遵守這些更改。