다양한 프로그래밍 언어를 지원하는 가볍지만 강력한 텍스트 입력 솔루션을 필요로 하는 개발자를 위한 Flutter용 고도로 사용자 정의 가능한 코드 에디터 위젯입니다. 구문 강조 및 코드 접기 기능을 제공하며, Flutter 앱에 원활하게 통합되어 UI 내에서 풍부한 코드 편집 경험을 가능하게 합니다. 이 패키지는 지속적으로 유지보수되고 오픈소스이므로 커뮤니티 기여와 확장이 가능합니다.
사용 사례
IDE 유사 앱에서 실시간 코드 편집
프로그래밍 학습을 위한 교육 도구
인터랙티브 편집 기능을 갖춘 코드 스니펫 뷰어
문서 플랫폼 내 임베디드 코드 편집기
Flutter 앱 내의 사용자 지정 개발 환경
주요 기능
다중 언어의 구문 강조
가독성 향상을 위한 코드 접기
사용자 지정 테마 및 스타일
플러그인 통합을 위한 확장 가능한 API
가볍고 성능이 뛰어난 렌더링
적합한 대상
코드 중심 애플리케이션을 개발하는 개발자
코드 예제가 포함된 교육 또는 튜토리얼 앱
인라인 코드 편집이 필요한 도구
고급 텍스트 입력 기능이 필요한 플러터 앱
오픈소스이고 유지보수가 가능한 코드 에디터 솔루션을 원하는 팀
고려 사항
완전한 언어 지원을 위해 추가 설정이 필요할 수 있습니다
매우 큰 파일에서는 성능이 달라질 수 있습니다
제한된 내장 자동 완성(외부 플러그인 권장)
전체 IDE 기능을 위한 설계가 아님
텍스트 선택 및 커서 관리에 주의 깊은 조작이 필요합니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
Flutter Code Editor
Flutter Code Editor is a multi-platform code editor supporting:
import'package:highlight/languages/python.dart'; // Each language is defined in its file.final controller = CodeController(
text: '...', // Initial code
language: python,
);
Language can be dynamically changed on a controller:
Flutter Code Editor can detect and fold code blocks. Code blocks folding is supported for the following languages:
Dart
Go
Java
Python
Scala
Code blocks folding may support other languages in experimental mode.
Code Analysis
The editor supports pluggable analyzers to highlight errors and show error messages:
We ship the following analyzers:
DefaultLocalAnalyzer highlights unmatched pair characters for supported languages.
It works on the client locally,
and is selected by default on CodeController if no other analyzer is specified.
DartPadAnalyzer for Dart language, calls upon the DartPad backend for analysis.
For other languages, you can write custom analyzers that access your backend.
See the code for DartPadAnalyzer for the implementation example.
Note: Code analysis is an experimental feature.
We may introduce breaking changes to Analyzer subclasses without following the semver contract.
If you only use the analyzers we ship, then this will not affect you.
Themes
Pre-defined Themes
Flutter Code Editor supports themes from the highlight package —
see the full list of the pre-defined themes
here.
Use CodeTheme widget to set the theme for underlying editors:
To use a custom theme, create a map of styles under the pre-defined class names —
see this example.
Hiding Line Numbers, Errors, and Folding Handles
A lot of styling can be tuned with a GutterStyle object passed to a CodeField widget.
See
this example
that dynamically changes the properties listed here.
CodeController extends the Flutter's built-in TextEditingController and is immediately
usable as one. However, code folding and other features have impact on built-in properties:
text returns and sets the visible text. If any code is folded, it will not be returned.
value returns and sets the TextEditingValue with the visible text and selection.
If any code is folded, it will not be returned.
fullText returns and sets the entire text, including any folded blocks and hidden
service comments (see below).
Named Sections
To manipulate parts of the source code, Flutter Code Editor supports named sections.
They are defined in the code by adding tags that Flutter Code Editor recognizes.
To define a named section in your source code, add comments to tag the start and the end of the section:
Add comment [START section_name] to tag the beginning of the section.
Add comment [END section_name] to tag the end of the section.
Here is an example to define a named section section1:
final text = '''
class MyClass {
public static void main(String[] args) {// [START section1]
int num = 5;
System.out.println("Factorial of " + num + " is " + factorial(5));
}// [END section1]
}
''';
To process named sections in the Flutter Code Editor,
pass the named section parser to the controller:
final controller = CodeController(
text: text,
language: java,
namedSectionParser: constBracketsStartEndNamedSectionParser(), // NEW
);
The example above creates a section named section1.
The built-in BracketsStartEndNamedSectionParser class is designed to parse sections
from the code comments using the above syntax.
It also hides any single-line comment that has a section tag with the above syntax,
although such comments are still present in the editor's hidden state
and will be revealed when copying the text.
To customize section parsing using any other syntax, subclass AbstractNamedSectionParser.
Read-Only Code Blocks
Flutter Code Editor allows you to define read-only code blocks.
This may be useful for learning use cases when users are guided to modify certain code blocks
while other code is meant to be protected from changes.
To make a named section read-only, pass a set of named sections to the controller.readOnlySectionNames:
controller.readOnlySectionNames = {'section1'};
This locks the given sections from modifications in the Flutter Code Editor.
Any non-existent section names in this set are ignored.
To make the code editable again, pass an updated set to controller.readOnlySectionNames.
When using this feature, text and value properties cannot be used to change the text
programmatically because they have the same effect as the user input.
This means that locking affects them as well.
To change a partially locked controller, set the fullText property.
Advanced Code Blocks Folding
Folding the First Comment/License
Many code snippets contain a license as their first comment, which can distract readers.
To fold the first comment, use:
controller.foldCommentAtLineZero();
This method has no effect if there is no comment starting at the first line.
Folding Imports
In many languages, the editor recognizes sequential import lines
and an optional package line as one foldable block.
To fold such blocks:
controller.foldImports();
Named Sections
The editor supports folding all blocks except for specific named sections.
This helps the user focus on those sections while
all source code is still there and can be expanded and copied by the user.
To fold all blocks except those overlapping with the given named sections:
controller.foldOutsideSections(['section1']);
Folding Specific Blocks
To fold and unfold blocks at a given line number:
controller.foldAt(1);
controller.unfoldAt(1);
If there is no block at a given line, this has no effect.
Note: For the controller, line numbers start at 0 although
the widget displays them starting at 1.
Accessing Folded Blocks
To get the currently folded blocks, read controller.code.foldedBlocks.
Hiding Text
The editor allows you to completely hide all code except for a specific named section.
This is useful to achieve even more focus than with folding.
To hide all the code except the given named section:
controller.visibleSectionNames = {'section1'};
When hiding text, the full text is still preserved
and available via fullText property in the Flutter Code Editor.
Hiding text preserves line numbering, which is not possible by just showing a cropped snippet.
Preserving hidden text is also useful if you later need to send
the full code for further processing but still want to hide
non-informative parts.
Hiding text also makes the entire editor read-only
to prevent users from modifying the code, adding imports, etc.
which may conflict with the hidden parts.
Only one visible section at a time is currently supported. The behavior of passing more than
one section is undefined.
Autocompletion
The editor suggests words as they are typed. Suggested words are:
All keywords of the current language.
All words already in the text.
Words set with controller.autocompleter.setCustomWords(['word1', 'word2'])
All those words are merged into an unstructured dictionary.
The editor does not perform any syntax analysis, so it cannot tell if a given class really has
the method the user is typing. This feature is meant to simplify typing, but should not be relied on
when exploring classes and methods.