-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/line numbers #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: release/0.2.0
Are you sure you want to change the base?
Changes from 2 commits
38fd616
dffde41
d03edd4
150d366
b6fde22
e0526ec
f707803
2384f89
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,60 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:kode_view/src/presentation/line_numbers_wrapper.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, | ||
|
||
// Core functionality | ||
|
||
this.controller, | ||
this.maxLines, | ||
this.options, | ||
this.showCursor, | ||
this.onTap, | ||
this.language, | ||
this.theme, | ||
this.textStyle, | ||
this.maxLines, | ||
|
||
// Appearance | ||
this.decoration, | ||
this.enableLineNumbers = false, | ||
this.lineNumberColor, | ||
this.lineNumberBackgroundColor, | ||
|
||
// Interaction | ||
this.showCursor, | ||
this.options, | ||
this.onTap, | ||
|
||
// Debugging | ||
this.debug = false, | ||
|
||
// Flutter widget key | ||
super.key, | ||
}); | ||
|
||
// Required | ||
final String code; | ||
|
||
// Core functionality | ||
final SyntaxHighlightingController? controller; | ||
final String? language; | ||
final String? theme; | ||
final TextStyle? textStyle; | ||
final int? maxLines; | ||
|
||
// Appearance | ||
final InputDecoration? decoration; | ||
final bool enableLineNumbers; | ||
final Color? lineNumberColor; | ||
final Color? lineNumberBackgroundColor; | ||
|
||
// Interaction | ||
final bool? showCursor; | ||
final TextSelectionOptions? options; | ||
final InputDecoration? decoration; | ||
final GestureTapCallback? onTap; | ||
final SyntaxHighlightingController? controller; | ||
final bool debug; | ||
|
||
final String? language; | ||
final String? theme; | ||
final TextStyle? textStyle; | ||
// Debugging | ||
final bool debug; | ||
|
||
@override | ||
State<CodeEditText> createState() => _CodeEditTextState(); | ||
|
@@ -42,8 +67,11 @@ class _CodeEditTextState extends State<CodeEditText> { | |
void initState() { | ||
super.initState(); | ||
_controller = widget.controller ?? | ||
SyntaxHighlightingController(text: widget.code, debug: widget.debug, | ||
)..addListener(() { | ||
SyntaxHighlightingController( | ||
text: widget.code, | ||
debug: widget.debug, | ||
) | ||
..addListener(() { | ||
_controller.updateSyntaxHighlighting( | ||
code: _controller.text, | ||
language: widget.language, | ||
|
@@ -65,20 +93,24 @@ class _CodeEditTextState extends State<CodeEditText> { | |
return ValueListenableBuilder( | ||
valueListenable: _controller.textSpansNotifier, | ||
builder: (context, __, _) { | ||
return TextField( | ||
controller: _controller, | ||
style: widget.textStyle, | ||
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, | ||
return LineNumbersWrapper( | ||
enableLineNumbers: widget.enableLineNumbers, | ||
linesNumber: _controller.text.split('\n').length, | ||
child: TextField( | ||
controller: _controller, | ||
style: widget.textStyle, | ||
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, | ||
), | ||
); | ||
}, | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:kode_view/src/presentation/styles/text_styles.dart'; | ||
|
||
class LineNumbersWrapper extends StatelessWidget { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Brilliant idea to use wrapper for that! |
||
const LineNumbersWrapper({ | ||
required this.enableLineNumbers, | ||
required this.linesNumber, | ||
required this.child, | ||
this.textStyle, | ||
this.lineNumberBackgroundColor, | ||
this.lineNumberColor, | ||
super.key, | ||
}); | ||
final bool enableLineNumbers; | ||
final int linesNumber; | ||
final Color? lineNumberBackgroundColor; | ||
final Color? lineNumberColor; | ||
final TextStyle? textStyle; | ||
final Widget child; | ||
@override | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing space |
||
Widget build(BuildContext context) { | ||
return Row( | ||
children: [ | ||
if (enableLineNumbers) ...[ | ||
Container( | ||
color: lineNumberBackgroundColor, | ||
width: 40, | ||
|
||
child: ListView.builder( | ||
shrinkWrap: true, | ||
itemCount: linesNumber, | ||
itemBuilder: (context, index) { | ||
return Text( | ||
'${index + 1}', | ||
textAlign: TextAlign.right, | ||
style: TextStyle( | ||
color: lineNumberColor ?? Colors.black54, | ||
fontSize: textStyle?.fontSize ?? | ||
const TextStyles.code('').style!.fontSize, | ||
), | ||
); | ||
}, | ||
), | ||
), | ||
const SizedBox( | ||
width: 4, | ||
), | ||
], | ||
Expanded( | ||
child: child, | ||
), | ||
], | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
showLineNumbers