-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathline_number_controller.dart
More file actions
41 lines (36 loc) · 1.16 KB
/
line_number_controller.dart
File metadata and controls
41 lines (36 loc) · 1.16 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
import 'package:flutter/widgets.dart';
class LineNumberController extends TextEditingController {
final TextSpan Function(int, TextStyle?)? lineNumberBuilder;
LineNumberController(
this.lineNumberBuilder,
);
@override
TextSpan buildTextSpan({
required BuildContext context,
TextStyle? style,
bool? withComposing,
}) {
final children = <InlineSpan>[];
final list = text.split('\n');
for (int k = 0; k < list.length; k++) {
final el = list[k];
// Blank lines are placeholders inserted to align with wrapped
// visual lines. They should render as empty text spans to
// preserve vertical spacing.
if (el.trim().isEmpty) {
children.add(TextSpan(text: '', style: style));
} else {
final number = int.tryParse(el) ?? 0;
var textSpan = TextSpan(text: el, style: style);
if (lineNumberBuilder != null && number != 0) {
textSpan = lineNumberBuilder!(number, style);
}
children.add(textSpan);
}
if (k < list.length - 1) {
children.add(const TextSpan(text: '\n'));
}
}
return TextSpan(children: children, style: style);
}
}