Skip to content
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

Extra customization of the text widget container #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions flutter_highlight/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,22 @@ class _MyHomePageState extends State<MyHomePage> {
],
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
HighlightView(
exampleMap[language],
language: language,
theme: themeMap[theme],
padding: EdgeInsets.all(12),
textStyle: TextStyle(
fontFamily:
'SFMono-Regular,Consolas,Liberation Mono,Menlo,monospace'),
)
],
child: Padding(
padding: EdgeInsets.all(5.0),
child: HighlightView(
exampleMap[language],
language: language,
theme: themeMap[theme],
padding: EdgeInsets.all(5.0),
textStyle:
TextStyle(fontFamily: 'SFMono-Regular,Consolas,Liberation Mono,Menlo,monospace'),
wrapText: false,
border: Border.all(
color: Colors.black26,
width: 3.0,
),
borderRadius: BorderRadius.circular(8.0),
),
),
),
);
Expand Down
50 changes: 41 additions & 9 deletions flutter_highlight/lib/flutter_highlight.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,30 @@ class HighlightView extends StatelessWidget {
/// Specify text styles such as font family and font size
final TextStyle textStyle;

/// Text wrapping
///
/// Toggle automatic text wrapping
final bool wrapText;

/// Container border
///
/// Specify custom border of the container
final Border border;

/// Container border radius
///
/// Specify the radius of the border, in case one is set
final BorderRadius borderRadius;

HighlightView(
String input, {
this.language,
this.theme = const {},
this.padding,
this.textStyle,
this.wrapText = true,
this.border,
this.borderRadius,
int tabSize = 8, // TODO: https://github.com/flutter/flutter/issues/50087
}) : source = input.replaceAll('\t', ' ' * tabSize);

Expand Down Expand Up @@ -79,22 +97,36 @@ class HighlightView extends StatelessWidget {

@override
Widget build(BuildContext context) {
return Container(
padding: padding,
decoration: BoxDecoration(
color: theme[_rootKey]?.backgroundColor ?? _defaultBackgroundColor,
border: border,
borderRadius: border == null ? null : borderRadius,
),
child: SingleChildScrollView(
child: wrapText
? _getTextWidget()
: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: _getTextWidget(),
),
),
);
}

Widget _getTextWidget() {
var _textStyle = TextStyle(
fontFamily: _defaultFontFamily,
color: theme[_rootKey]?.color ?? _defaultFontColor,
);
if (textStyle != null) {
_textStyle = _textStyle.merge(textStyle);
}

return Container(
color: theme[_rootKey]?.backgroundColor ?? _defaultBackgroundColor,
padding: padding,
child: RichText(
text: TextSpan(
style: _textStyle,
children: _convert(highlight.parse(source, language: language).nodes),
),
return RichText(
text: TextSpan(
style: _textStyle,
children: _convert(highlight.parse(source, language: language).nodes),
),
);
}
Expand Down