Skip to content

Commit 9c2e1fa

Browse files
committed
Initial working horizontal scrollbar
1 parent 0aeb290 commit 9c2e1fa

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_code_editor/flutter_code_editor.dart';
3+
import 'package:flutter_highlight/themes/ir-black.dart' show irBlackTheme;
4+
import 'package:highlight/languages/dart.dart';
5+
6+
void main() {
7+
runApp(const CodeEditor());
8+
}
9+
10+
const monospace = TextStyle(fontFamily: 'monospace', fontSize: 14, height: 1.2);
11+
12+
class CodeEditor extends StatefulWidget {
13+
const CodeEditor({super.key});
14+
15+
@override
16+
State<CodeEditor> createState() => _CodeEditorState();
17+
}
18+
19+
class _CodeEditorState extends State<CodeEditor> {
20+
final codeController = CodeController(language: dart);
21+
final vScrollController = ScrollController();
22+
23+
@override
24+
Widget build(BuildContext context) {
25+
return MaterialApp(
26+
theme: ThemeData(
27+
scaffoldBackgroundColor: Colors.black,
28+
scrollbarTheme: Theme.of(context).scrollbarTheme.copyWith(
29+
radius: const Radius.circular(2),
30+
thumbVisibility: const WidgetStatePropertyAll(true),
31+
trackVisibility: const WidgetStatePropertyAll(true),
32+
thumbColor: WidgetStateProperty.all(Colors.grey),
33+
),
34+
),
35+
home: Scaffold(
36+
body: SingleChildScrollView(
37+
controller: vScrollController,
38+
child: CodeTheme(
39+
data: CodeThemeData(styles: irBlackTheme),
40+
child: CodeField(
41+
gutterStyle: const GutterStyle(
42+
showFoldingHandles: false,
43+
textStyle: monospace,
44+
),
45+
textStyle: monospace,
46+
controller: codeController,
47+
enableHorizontalScrollBar: true,
48+
),
49+
),
50+
),
51+
),
52+
);
53+
}
54+
}

lib/src/code_field/code_field.dart

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ class CodeField extends StatefulWidget {
126126
/// or make the field scrollable horizontally.
127127
final bool wrap;
128128

129+
/// Whether to show horizontal scrollbar.
130+
final bool enableHorizontalScrollBar;
131+
129132
/// A CodeController instance to apply
130133
/// language highlight, themeing and modifiers.
131134
final CodeController controller;
@@ -172,6 +175,7 @@ class CodeField extends StatefulWidget {
172175
this.maxLines,
173176
this.expands = false,
174177
this.wrap = false,
178+
this.enableHorizontalScrollBar = false,
175179
this.background,
176180
this.decoration,
177181
this.textStyle,
@@ -188,6 +192,10 @@ class CodeField extends StatefulWidget {
188192
@Deprecated('Use gutterStyle instead')
189193
this.lineNumberStyle = const GutterStyle(),
190194
}) : assert(
195+
!(enableHorizontalScrollBar && wrap),
196+
'Can not wrap and show horizontal scrollbar at the same time',
197+
),
198+
assert(
191199
gutterStyle == null || lineNumbers == null,
192200
'Can not provide gutterStyle and lineNumbers at the same time. '
193201
'Please use gutterStyle and provide necessary columns to show/hide'),
@@ -366,14 +374,23 @@ class _CodeFieldState extends State<CodeField> {
366374
),
367375
);
368376

369-
return SingleChildScrollView(
377+
final scrollView = SingleChildScrollView(
370378
padding: EdgeInsets.only(
371379
right: widget.padding.right,
372380
),
373381
scrollDirection: Axis.horizontal,
374382
controller: _horizontalCodeScroll,
375383
child: intrinsic,
376384
);
385+
386+
if (widget.enableHorizontalScrollBar) {
387+
return Scrollbar(
388+
controller: _horizontalCodeScroll,
389+
child: scrollView,
390+
);
391+
} else {
392+
return scrollView;
393+
}
377394
}
378395

379396
@override

0 commit comments

Comments
 (0)