Skip to content

Commit 0737dc8

Browse files
horizontalScroll & lineNumber booleans
1 parent 86182ef commit 0737dc8

File tree

1 file changed

+40
-27
lines changed

1 file changed

+40
-27
lines changed

lib/src/code_field.dart

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class CodeField extends StatefulWidget {
9595
/// {@macro flutter.widgets.editableText.readOnly}
9696
final bool readOnly;
9797

98+
/// {@macro flutter.widgets.textField.isDense}
9899
final bool isDense;
99100

100101
final Color? background;
@@ -103,6 +104,8 @@ class CodeField extends StatefulWidget {
103104
final TextSelectionThemeData? textSelectionTheme;
104105
final FocusNode? focusNode;
105106
final void Function()? onTap;
107+
final bool lineNumbers;
108+
final bool horizontalScroll;
106109

107110
const CodeField({
108111
Key? key,
@@ -127,6 +130,8 @@ class CodeField extends StatefulWidget {
127130
this.isDense = false,
128131
this.smartQuotesType,
129132
this.keyboardType,
133+
this.lineNumbers = true,
134+
this.horizontalScroll = true,
130135
}) : super(key: key);
131136

132137
@override
@@ -224,6 +229,9 @@ class CodeFieldState extends State<CodeField> {
224229
right: widget.padding.right,
225230
),
226231
scrollDirection: Axis.horizontal,
232+
233+
/// Prevents the horizontal scroll if horizontalScroll is false
234+
physics: widget.horizontalScroll ? null : NeverScrollableScrollPhysics(),
227235
child: intrinsic,
228236
);
229237
}
@@ -255,32 +263,36 @@ class CodeFieldState extends State<CodeField> {
255263
);
256264
final cursorColor = widget.cursorColor ?? theme?[ROOT_KEY]?.color ?? defaultText;
257265

258-
final lineNumberCol = TextField(
259-
smartQuotesType: widget.smartQuotesType,
260-
scrollPadding: widget.padding,
261-
style: numberTextStyle,
262-
controller: _numberController,
263-
enabled: false,
264-
minLines: widget.minLines,
265-
maxLines: widget.maxLines,
266-
expands: widget.expands,
267-
scrollController: _numberScroll,
268-
decoration: InputDecoration(
269-
disabledBorder: InputBorder.none,
270-
isDense: widget.isDense,
271-
),
272-
textAlign: widget.lineNumberStyle.textAlign,
273-
);
274-
275-
final numberCol = Container(
276-
width: widget.lineNumberStyle.width,
277-
padding: EdgeInsets.only(
278-
left: widget.padding.left,
279-
right: widget.lineNumberStyle.margin / 2,
280-
),
281-
color: widget.lineNumberStyle.background,
282-
child: lineNumberCol,
283-
);
266+
TextField? lineNumberCol;
267+
Container? numberCol;
268+
if (widget.lineNumbers) {
269+
lineNumberCol = TextField(
270+
smartQuotesType: widget.smartQuotesType,
271+
scrollPadding: widget.padding,
272+
style: numberTextStyle,
273+
controller: _numberController,
274+
enabled: false,
275+
minLines: widget.minLines,
276+
maxLines: widget.maxLines,
277+
expands: widget.expands,
278+
scrollController: _numberScroll,
279+
decoration: InputDecoration(
280+
disabledBorder: InputBorder.none,
281+
isDense: widget.isDense,
282+
),
283+
textAlign: widget.lineNumberStyle.textAlign,
284+
);
285+
286+
numberCol = Container(
287+
width: widget.lineNumberStyle.width,
288+
padding: EdgeInsets.only(
289+
left: widget.padding.left,
290+
right: widget.lineNumberStyle.margin / 2,
291+
),
292+
color: widget.lineNumberStyle.background,
293+
child: lineNumberCol,
294+
);
295+
}
284296

285297
final codeField = TextField(
286298
keyboardType: widget.keyboardType,
@@ -323,10 +335,11 @@ class CodeFieldState extends State<CodeField> {
323335
return Container(
324336
decoration: widget.decoration,
325337
color: backgroundCol,
338+
padding: !widget.lineNumbers ? EdgeInsets.only(left: 8) : null,
326339
child: Row(
327340
crossAxisAlignment: CrossAxisAlignment.start,
328341
children: [
329-
numberCol,
342+
if (widget.lineNumbers && numberCol != null) numberCol,
330343
Expanded(child: codeCol),
331344
],
332345
),

0 commit comments

Comments
 (0)