-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_edit_text.dart
More file actions
153 lines (139 loc) · 4.51 KB
/
Copy pathcode_edit_text.dart
File metadata and controls
153 lines (139 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import 'package:flutter/material.dart';
import 'package:kode_view/src/presentation/line_numbers_wrapper.dart';
import 'package:kode_view/src/presentation/styles/text_styles.dart';
import 'package:kode_view/src/presentation/syntax_highlighting_controller.dart';
import 'package:kode_view/src/presentation/text_selection_options.dart';
class CodeEditText extends StatefulWidget {
const CodeEditText({
required this.code,
this.controller,
this.language,
this.theme,
this.textStyle = const TextStyles.code(),
this.maxLines,
this.decoration,
this.showLineNumbers = false,
this.lineNumberColor,
this.lineNumberBackgroundColor,
this.showCursor,
this.options,
this.onTap,
this.debug = false,
super.key,
});
final String code;
final SyntaxHighlightingController? controller;
final String? language;
final String? theme;
final TextStyle textStyle;
final int? maxLines;
final InputDecoration? decoration;
final bool showLineNumbers;
final Color? lineNumberColor;
final Color? lineNumberBackgroundColor;
final bool? showCursor;
final TextSelectionOptions? options;
final GestureTapCallback? onTap;
final bool debug;
@override
State<CodeEditText> createState() => _CodeEditTextState();
}
class _CodeEditTextState extends State<CodeEditText> {
late SyntaxHighlightingController _controller;
final ScrollController _lineNumbersScrollController = ScrollController();
final ScrollController _textFieldScrollController = ScrollController();
final GlobalKey _textFieldKey = GlobalKey();
double _textFieldHeight = 0;
@override
void initState() {
super.initState();
_controller = widget.controller ??
SyntaxHighlightingController(
text: widget.code,
debug: widget.debug,
)
..addListener(() {
_controller.updateSyntaxHighlighting(
code: _controller.text,
language: widget.language,
theme: widget.theme,
textStyle: widget.textStyle.copyWith(
height: 1.5,
fontSize: 10.0,
),
);
});
_controller.updateSyntaxHighlighting(
code: _controller.text,
language: widget.language,
theme: widget.theme,
textStyle: widget.textStyle.copyWith(
height: 1.5,
fontSize: 10.0,
),
);
_textFieldScrollController.addListener(() {
if (_textFieldScrollController.offset !=
_lineNumbersScrollController.offset) {
_lineNumbersScrollController.jumpTo(_textFieldScrollController.offset);
}
});
}
void _getTextFieldHeight() {
final RenderBox renderBox =
_textFieldKey.currentContext?.findRenderObject() as RenderBox;
setState(() {
_textFieldHeight = renderBox.size.height;
});
}
@override
Widget build(BuildContext context) {
return ValueListenableBuilder(
valueListenable: _controller.textSpansNotifier,
builder: (context, __, _) {
WidgetsBinding.instance.addPostFrameCallback((_) {
_getTextFieldHeight();
});
return LineNumbersWrapper(
height: _textFieldHeight,
showLineNumbers: widget.showLineNumbers,
scrollController: _lineNumbersScrollController,
linesCount: _controller.text.split('\n').length,
fontSize: 10.0,
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
physics: const ClampingScrollPhysics(),
child: IntrinsicWidth(
child: TextField(
scrollPadding: EdgeInsets.zero,
key: _textFieldKey,
scrollController: _textFieldScrollController,
controller: _controller,
style: widget.textStyle.copyWith(
height: 1.5,
fontSize: 10.0,
),
onTap: widget.onTap,
minLines: 1,
maxLines: widget.maxLines,
contextMenuBuilder: widget.options != null
? (context, editableTextState) => widget.options!
.toolbarOptions(context, editableTextState)
: null,
enableInteractiveSelection: widget.options != null,
showCursor: widget.showCursor ?? true,
scrollPhysics: const ClampingScrollPhysics(),
decoration: widget.decoration,
),
),
),
);
},
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
}