code_field
1.0.0
構文の強調表示をサポートするカスタマイズ可能なコードテキストフィールド
いくつかの言語 /テーマの組み合わせを紹介するライブデモ
実験的なVM dloxは、オンラインエディターでコードフィールドを使用します
コード修飾子は、インデントを自動的に管理するのに役立ちます
編集者は、長い線を処理するために水平なスクロール可能な容器に包まれています
Flutterプロジェクトのpubspec.yamlで、次の依存関係を追加します。
dependencies :
...
code_text_field : <latest_version>最新バージョン
ライブラリに次のインポートを追加します。
import 'package:code_text_field/code_text_field.dart' ;コードフィールドウィジェットは、言語に従ってテキスト入力を動的に解析し、テーママップでレンダリングするコーデコントローラーで動作します
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 Font Source Code ProがAssetsフォルダーとPubSpec.yamlファイルに追加されました。
言語定義に加えて、単語ごとのスタイリングをStringMapフィールドで指定できます
_codeController = CodeController (
//...
stringMap : {
"Hello" : TextStyle (fontWeight : FontWeight .bold, color : Colors .red),
"world" : TextStyle (fontStyle : FontStyle .italic, color : Colors .green),
},
);パターンマップでは、より複雑な正規表現も使用できます。ただし、言語が使用されると、その正規表現パターンがパターンマップとStringMapよりも優先されます。
_codeController = CodeController (
//...
patternMap : {
r"B#[a-zA-Z0-9]+b" :
TextStyle (fontWeight : FontWeight .bold, color : Colors .purpleAccent),
},
);パターンマップと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チャンネルにTextEditingControllerの壊れた変更が導入されました。ブランチベータは、これらの変更に準拠する必要があります。