diff --git a/flutter_highlight/example/lib/main.dart b/flutter_highlight/example/lib/main.dart index 74ada48..d7e9eb8 100644 --- a/flutter_highlight/example/lib/main.dart +++ b/flutter_highlight/example/lib/main.dart @@ -92,19 +92,22 @@ class _MyHomePageState extends State { ], ), body: Center( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - 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), + ), ), ), ); diff --git a/flutter_highlight/lib/flutter_highlight.dart b/flutter_highlight/lib/flutter_highlight.dart index 43b7fa1..c9a74eb 100644 --- a/flutter_highlight/lib/flutter_highlight.dart +++ b/flutter_highlight/lib/flutter_highlight.dart @@ -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); @@ -79,6 +97,25 @@ 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, @@ -86,15 +123,10 @@ class HighlightView extends StatelessWidget { 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), ), ); }