Declare locals final where not reassigned (flutter_markdown) (#8568)
This commit is contained in:
@@ -157,17 +157,17 @@ class _MarkdownBodyRawState extends State<MarkdownBodyRaw> {
|
||||
}
|
||||
|
||||
void _buildMarkdownCache() {
|
||||
MarkdownStyleRaw markdownStyle = config.markdownStyle ?? config.createDefaultStyle(context);
|
||||
SyntaxHighlighter syntaxHighlighter = config.syntaxHighlighter ?? new _DefaultSyntaxHighlighter(markdownStyle.code);
|
||||
final MarkdownStyleRaw markdownStyle = config.markdownStyle ?? config.createDefaultStyle(context);
|
||||
final SyntaxHighlighter syntaxHighlighter = config.syntaxHighlighter ?? new _DefaultSyntaxHighlighter(markdownStyle.code);
|
||||
|
||||
_linkHandler?.dispose();
|
||||
_linkHandler = new _LinkHandler(config.onTapLink);
|
||||
|
||||
// TODO: This can be optimized by doing the split and removing \r at the same time
|
||||
List<String> lines = config.data.replaceAll('\r\n', '\n').split('\n');
|
||||
md.Document document = new md.Document();
|
||||
final List<String> lines = config.data.replaceAll('\r\n', '\n').split('\n');
|
||||
final md.Document document = new md.Document();
|
||||
|
||||
_Renderer renderer = new _Renderer();
|
||||
final _Renderer renderer = new _Renderer();
|
||||
_cachedBlocks = renderer.render(document.parseLines(lines), markdownStyle, syntaxHighlighter, _linkHandler);
|
||||
}
|
||||
|
||||
@@ -176,7 +176,7 @@ class _MarkdownBodyRawState extends State<MarkdownBodyRaw> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
List<Widget> blocks = <Widget>[];
|
||||
final List<Widget> blocks = <Widget>[];
|
||||
for (_Block block in _cachedBlocks) {
|
||||
blocks.add(block.build(context));
|
||||
}
|
||||
@@ -220,8 +220,8 @@ class _Renderer implements md.NodeVisitor {
|
||||
@override
|
||||
void visitText(md.Text text) {
|
||||
if (_currentBlock != null) { // ignore if no corresponding block
|
||||
_MarkdownNodeList topList = _currentBlock.stack.last;
|
||||
List<_MarkdownNode> top = topList.list;
|
||||
final _MarkdownNodeList topList = _currentBlock.stack.last;
|
||||
final List<_MarkdownNode> top = topList.list;
|
||||
|
||||
if (_currentBlock.tag == 'pre')
|
||||
top.add(
|
||||
@@ -243,7 +243,7 @@ class _Renderer implements md.NodeVisitor {
|
||||
else
|
||||
blockList = _currentBlock.subBlocks;
|
||||
|
||||
_Block newBlock = new _Block(element.tag, element.attributes, _markdownStyle, new List<String>.from(_listIndents), blockList.length);
|
||||
final _Block newBlock = new _Block(element.tag, element.attributes, _markdownStyle, new List<String>.from(_listIndents), blockList.length);
|
||||
blockList.add(newBlock);
|
||||
} else {
|
||||
_LinkInfo linkInfo;
|
||||
@@ -251,8 +251,8 @@ class _Renderer implements md.NodeVisitor {
|
||||
linkInfo = _linkHandler.createLinkInfo(element.attributes['href']);
|
||||
}
|
||||
|
||||
TextStyle style = _markdownStyle.styles[element.tag] ?? const TextStyle();
|
||||
List<_MarkdownNode> styleElement = <_MarkdownNode>[new _MarkdownNodeTextStyle(style, linkInfo)];
|
||||
final TextStyle style = _markdownStyle.styles[element.tag] ?? const TextStyle();
|
||||
final List<_MarkdownNode> styleElement = <_MarkdownNode>[new _MarkdownNodeTextStyle(style, linkInfo)];
|
||||
_currentBlock.stack.add(new _MarkdownNodeList(styleElement));
|
||||
}
|
||||
return true;
|
||||
@@ -265,7 +265,7 @@ class _Renderer implements md.NodeVisitor {
|
||||
|
||||
if (_isBlockTag(element.tag)) {
|
||||
if (_currentBlock.stack.isNotEmpty) {
|
||||
_MarkdownNodeList stackList = _currentBlock.stack.first;
|
||||
final _MarkdownNodeList stackList = _currentBlock.stack.first;
|
||||
_currentBlock.stack = stackList.list;
|
||||
_currentBlock.open = false;
|
||||
} else {
|
||||
@@ -273,12 +273,12 @@ class _Renderer implements md.NodeVisitor {
|
||||
}
|
||||
} else {
|
||||
if (_currentBlock.stack.length > 1) {
|
||||
_MarkdownNodeList poppedList = _currentBlock.stack.last;
|
||||
List<_MarkdownNode> popped = poppedList.list;
|
||||
final _MarkdownNodeList poppedList = _currentBlock.stack.last;
|
||||
final List<_MarkdownNode> popped = poppedList.list;
|
||||
_currentBlock.stack.removeLast();
|
||||
|
||||
_MarkdownNodeList topList = _currentBlock.stack.last;
|
||||
List<_MarkdownNode> top = topList.list;
|
||||
final _MarkdownNodeList topList = _currentBlock.stack.last;
|
||||
final List<_MarkdownNode> top = topList.list;
|
||||
top.add(new _MarkdownNodeList(popped));
|
||||
}
|
||||
}
|
||||
@@ -304,7 +304,7 @@ class _Renderer implements md.NodeVisitor {
|
||||
if (!blocks.last.open)
|
||||
return null;
|
||||
|
||||
_Block childBlock = _currentBlockInList(blocks.last.subBlocks);
|
||||
final _Block childBlock = _currentBlockInList(blocks.last.subBlocks);
|
||||
if (childBlock != null)
|
||||
return childBlock;
|
||||
|
||||
@@ -377,7 +377,7 @@ class _Block {
|
||||
Widget contents;
|
||||
|
||||
if (subBlocks.isNotEmpty) {
|
||||
List<Widget> subWidgets = <Widget>[];
|
||||
final List<Widget> subWidgets = <Widget>[];
|
||||
for (_Block subBlock in subBlocks) {
|
||||
subWidgets.add(subBlock.build(context));
|
||||
}
|
||||
@@ -387,7 +387,7 @@ class _Block {
|
||||
children: subWidgets
|
||||
);
|
||||
} else {
|
||||
TextSpan span = _stackToTextSpan(new _MarkdownNodeList(stack));
|
||||
final TextSpan span = _stackToTextSpan(new _MarkdownNodeList(stack));
|
||||
contents = new RichText(text: span);
|
||||
|
||||
if (listIndents.isNotEmpty) {
|
||||
@@ -445,10 +445,10 @@ class _Block {
|
||||
return stack.textSpan;
|
||||
|
||||
if (stack is _MarkdownNodeList) {
|
||||
List<_MarkdownNode> list = stack.list;
|
||||
_MarkdownNodeTextStyle styleNode = list[0];
|
||||
_LinkInfo linkInfo = styleNode.linkInfo;
|
||||
TextStyle style = styleNode.style;
|
||||
final List<_MarkdownNode> list = stack.list;
|
||||
final _MarkdownNodeTextStyle styleNode = list[0];
|
||||
final _LinkInfo linkInfo = styleNode.linkInfo;
|
||||
final TextStyle style = styleNode.style;
|
||||
|
||||
List<TextSpan> children = <TextSpan>[];
|
||||
for (int i = 1; i < list.length; i++) {
|
||||
@@ -461,7 +461,7 @@ class _Block {
|
||||
children = null;
|
||||
}
|
||||
|
||||
TapGestureRecognizer recognizer = linkInfo?.recognizer;
|
||||
final TapGestureRecognizer recognizer = linkInfo?.recognizer;
|
||||
|
||||
return new TextSpan(style: style, children: children, recognizer: recognizer, text: text);
|
||||
}
|
||||
@@ -478,14 +478,14 @@ class _Block {
|
||||
}
|
||||
|
||||
Widget _buildImage(BuildContext context, String src) {
|
||||
List<String> parts = src.split('#');
|
||||
final List<String> parts = src.split('#');
|
||||
if (parts.isEmpty) return new Container();
|
||||
|
||||
String path = parts.first;
|
||||
final String path = parts.first;
|
||||
double width;
|
||||
double height;
|
||||
if (parts.length == 2) {
|
||||
List<String> dimensions = parts.last.split('x');
|
||||
final List<String> dimensions = parts.last.split('x');
|
||||
if (dimensions.length == 2) {
|
||||
width = double.parse(dimensions[0]);
|
||||
height = double.parse(dimensions[1]);
|
||||
@@ -510,13 +510,13 @@ class _LinkHandler {
|
||||
MarkdownLinkCallback onTapLink;
|
||||
|
||||
_LinkInfo createLinkInfo(String href) {
|
||||
TapGestureRecognizer recognizer = new TapGestureRecognizer();
|
||||
final TapGestureRecognizer recognizer = new TapGestureRecognizer();
|
||||
recognizer.onTap = () {
|
||||
if (onTapLink != null)
|
||||
onTapLink(href);
|
||||
};
|
||||
|
||||
_LinkInfo linkInfo = new _LinkInfo(href, recognizer);
|
||||
final _LinkInfo linkInfo = new _LinkInfo(href, recognizer);
|
||||
links.add(linkInfo);
|
||||
|
||||
return linkInfo;
|
||||
|
||||
@@ -12,7 +12,7 @@ void main() {
|
||||
testWidgets('Simple string', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(new MarkdownBody(data: 'Hello'));
|
||||
|
||||
Iterable<Widget> widgets = tester.allWidgets;
|
||||
final Iterable<Widget> widgets = tester.allWidgets;
|
||||
_expectWidgetTypes(widgets, <Type>[MarkdownBody, Column, Container, Padding, RichText]);
|
||||
_expectTextStrings(widgets, <String>['Hello']);
|
||||
});
|
||||
@@ -20,7 +20,7 @@ void main() {
|
||||
testWidgets('Header', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(new MarkdownBody(data: '# Header'));
|
||||
|
||||
Iterable<Widget> widgets = tester.allWidgets;
|
||||
final Iterable<Widget> widgets = tester.allWidgets;
|
||||
_expectWidgetTypes(widgets, <Type>[MarkdownBody, Column, Container, Padding, RichText]);
|
||||
_expectTextStrings(widgets, <String>['Header']);
|
||||
});
|
||||
@@ -28,14 +28,14 @@ void main() {
|
||||
testWidgets('Empty string', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(new MarkdownBody(data: ''));
|
||||
|
||||
Iterable<Widget> widgets = tester.allWidgets;
|
||||
final Iterable<Widget> widgets = tester.allWidgets;
|
||||
_expectWidgetTypes(widgets, <Type>[MarkdownBody, Column]);
|
||||
});
|
||||
|
||||
testWidgets('Ordered list', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(new MarkdownBody(data: '1. Item 1\n1. Item 2\n2. Item 3'));
|
||||
|
||||
Iterable<Widget> widgets = tester.allWidgets;
|
||||
final Iterable<Widget> widgets = tester.allWidgets;
|
||||
_expectTextStrings(widgets, <String>[
|
||||
'1.',
|
||||
'Item 1',
|
||||
@@ -49,7 +49,7 @@ void main() {
|
||||
testWidgets('Unordered list', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(new MarkdownBody(data: '- Item 1\n- Item 2\n- Item 3'));
|
||||
|
||||
Iterable<Widget> widgets = tester.allWidgets;
|
||||
final Iterable<Widget> widgets = tester.allWidgets;
|
||||
_expectTextStrings(widgets, <String>[
|
||||
'•',
|
||||
'Item 1',
|
||||
@@ -63,7 +63,7 @@ void main() {
|
||||
testWidgets('Scrollable wrapping', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(new Markdown(data: ''));
|
||||
|
||||
List<Widget> widgets = tester.allWidgets.toList();
|
||||
final List<Widget> widgets = tester.allWidgets.toList();
|
||||
_expectWidgetTypes(widgets.take(2), <Type>[
|
||||
Markdown,
|
||||
SingleChildScrollView,
|
||||
@@ -78,8 +78,8 @@ void main() {
|
||||
testWidgets('Links', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(new Markdown(data: '[Link Text](href)'));
|
||||
|
||||
RichText textWidget = tester.allWidgets.firstWhere((Widget widget) => widget is RichText);
|
||||
TextSpan span = textWidget.text;
|
||||
final RichText textWidget = tester.allWidgets.firstWhere((Widget widget) => widget is RichText);
|
||||
final TextSpan span = textWidget.text;
|
||||
|
||||
expect(span.children[0].recognizer.runtimeType, equals(TapGestureRecognizer));
|
||||
});
|
||||
@@ -93,7 +93,7 @@ void main() {
|
||||
for (String mdLine in mdData) {
|
||||
await tester.pumpWidget(new MarkdownBody(data: mdLine));
|
||||
|
||||
Iterable<Widget> widgets = tester.allWidgets;
|
||||
final Iterable<Widget> widgets = tester.allWidgets;
|
||||
_expectTextStrings(widgets, <String>['Line 1', 'Line 2']);
|
||||
}
|
||||
});
|
||||
@@ -102,7 +102,7 @@ void main() {
|
||||
final String mdLine = 'Line 1 <\n\nc < c c\n\n< Line 2';
|
||||
await tester.pumpWidget(new MarkdownBody(data: mdLine));
|
||||
|
||||
Iterable<Widget> widgets = tester.allWidgets;
|
||||
final Iterable<Widget> widgets = tester.allWidgets;
|
||||
_expectTextStrings(widgets, <String>['Line 1 <','c < c c','< Line 2']);
|
||||
});
|
||||
|
||||
@@ -110,9 +110,9 @@ void main() {
|
||||
await tester.pumpWidget(new Markdown(data: 'Data1'));
|
||||
_expectTextStrings(tester.allWidgets, <String>['Data1']);
|
||||
|
||||
String stateBefore = WidgetsBinding.instance.renderViewElement.toStringDeep();
|
||||
final String stateBefore = WidgetsBinding.instance.renderViewElement.toStringDeep();
|
||||
await tester.pumpWidget(new Markdown(data: 'Data1'));
|
||||
String stateAfter = WidgetsBinding.instance.renderViewElement.toStringDeep();
|
||||
final String stateAfter = WidgetsBinding.instance.renderViewElement.toStringDeep();
|
||||
expect(stateBefore, equals(stateAfter));
|
||||
|
||||
await tester.pumpWidget(new Markdown(data: 'Data2'));
|
||||
@@ -120,22 +120,22 @@ void main() {
|
||||
});
|
||||
|
||||
testWidgets('Changing config - style', (WidgetTester tester) async {
|
||||
ThemeData theme = new ThemeData.light();
|
||||
final ThemeData theme = new ThemeData.light();
|
||||
|
||||
MarkdownStyle style1 = new MarkdownStyle.defaultFromTheme(theme);
|
||||
MarkdownStyle style2 = new MarkdownStyle.largeFromTheme(theme);
|
||||
final MarkdownStyle style1 = new MarkdownStyle.defaultFromTheme(theme);
|
||||
final MarkdownStyle style2 = new MarkdownStyle.largeFromTheme(theme);
|
||||
|
||||
await tester.pumpWidget(new Markdown(data: 'Test', markdownStyle: style1));
|
||||
|
||||
String stateBefore = WidgetsBinding.instance.renderViewElement.toStringDeep();
|
||||
final String stateBefore = WidgetsBinding.instance.renderViewElement.toStringDeep();
|
||||
await tester.pumpWidget(new Markdown(data: 'Test', markdownStyle: style2));
|
||||
String stateAfter = WidgetsBinding.instance.renderViewElement.toStringDeep();
|
||||
final String stateAfter = WidgetsBinding.instance.renderViewElement.toStringDeep();
|
||||
expect(stateBefore, isNot(stateAfter));
|
||||
});
|
||||
}
|
||||
|
||||
void _expectWidgetTypes(Iterable<Widget> widgets, List<Type> expected) {
|
||||
List<Type> actual = widgets.map((Widget w) => w.runtimeType).toList();
|
||||
final List<Type> actual = widgets.map((Widget w) => w.runtimeType).toList();
|
||||
expect(actual, expected);
|
||||
}
|
||||
|
||||
@@ -143,8 +143,8 @@ void _expectTextStrings(Iterable<Widget> widgets, List<String> strings) {
|
||||
int currentString = 0;
|
||||
for (Widget widget in widgets) {
|
||||
if (widget is RichText) {
|
||||
TextSpan span = widget.text;
|
||||
String text = _extractTextFromTextSpan(span);
|
||||
final TextSpan span = widget.text;
|
||||
final String text = _extractTextFromTextSpan(span);
|
||||
expect(text, equals(strings[currentString]));
|
||||
currentString += 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user