[flutter_markdown] fix for latest pkg:markdown (#2777)
diff --git a/packages/flutter_markdown/CHANGELOG.md b/packages/flutter_markdown/CHANGELOG.md
index 78e58d1..518e26a 100644
--- a/packages/flutter_markdown/CHANGELOG.md
+++ b/packages/flutter_markdown/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 0.6.13
+
+* Support changes in the latest `package:markdown`.
+
## 0.6.12
* Markdown Lists now take into account `fitContent` instead of always expanding to the maximum horizontally ([flutter/flutter#108976](https://github.com/flutter/flutter/issues/108976)).
diff --git a/packages/flutter_markdown/lib/src/builder.dart b/packages/flutter_markdown/lib/src/builder.dart
index 30e4a55..293f12c 100644
--- a/packages/flutter_markdown/lib/src/builder.dart
+++ b/packages/flutter_markdown/lib/src/builder.dart
@@ -470,9 +470,27 @@
current.children.add(_buildRichText(const TextSpan(text: '\n')));
} else if (tag == 'th' || tag == 'td') {
TextAlign? align;
+ // `style` was using in pkg:markdown <= 6.0.1
+ // Can be removed when min pkg:markedwn > 6.0.1
final String? style = element.attributes['style'];
if (style == null) {
- align = tag == 'th' ? styleSheet.tableHeadAlign : TextAlign.left;
+ // `align` is using in pkg:markdown > 6.0.1
+ final String? alignAttribute = element.attributes['align'];
+ if (alignAttribute == null) {
+ align = tag == 'th' ? styleSheet.tableHeadAlign : TextAlign.left;
+ } else {
+ switch (alignAttribute) {
+ case 'left':
+ align = TextAlign.left;
+ break;
+ case 'center':
+ align = TextAlign.center;
+ break;
+ case 'right':
+ align = TextAlign.right;
+ break;
+ }
+ }
} else {
final RegExp regExp = RegExp(r'text-align: (left|center|right)');
final Match match = regExp.matchAsPrefix(style)!;
diff --git a/packages/flutter_markdown/pubspec.yaml b/packages/flutter_markdown/pubspec.yaml
index ec3bcaf..8f1259f 100644
--- a/packages/flutter_markdown/pubspec.yaml
+++ b/packages/flutter_markdown/pubspec.yaml
@@ -4,7 +4,7 @@
formatted with simple Markdown tags.
repository: https://github.com/flutter/packages/tree/main/packages/flutter_markdown
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_markdown%22
-version: 0.6.12
+version: 0.6.13
environment:
sdk: ">=2.12.0 <3.0.0"
diff --git a/packages/flutter_markdown/test/table_test.dart b/packages/flutter_markdown/test/table_test.dart
index 1a17f1a..b1af7f1 100644
--- a/packages/flutter_markdown/test/table_test.dart
+++ b/packages/flutter_markdown/test/table_test.dart
@@ -5,8 +5,13 @@
import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:flutter_test/flutter_test.dart';
+import 'package:markdown/markdown.dart' as md show version;
+
import 'utils.dart';
+// TODO(kevmoo): delete this once the min version of pkg:markdown is updated
+final bool _newMarkdown = md.version.compareTo('6.0.1') > 0;
+
void main() => defineTests();
void defineTests() {
@@ -390,25 +395,43 @@
expectTableSize(3, 2);
- expect(find.byType(RichText), findsNWidgets(6));
- final List<String?> text = find
- .byType(RichText)
- .evaluate()
- .map((Element e) => e.widget)
- .cast<RichText>()
- .map((RichText richText) => richText.text)
- .cast<TextSpan>()
- .map((TextSpan e) => e.text)
- .toList();
- expect(text[0], 'abc');
- expect(text[1], 'def');
- expect(text[2], 'bar');
- expect(text[3], 'baz');
- expect(text[4], 'bar');
- expect(table.defaultColumnWidth, columnWidth);
+ if (!_newMarkdown) {
+ // For pkg:markdown <= v6.0.1
+ expect(find.byType(RichText), findsNWidgets(6));
+ final List<String?> text = find
+ .byType(RichText)
+ .evaluate()
+ .map((Element e) => e.widget)
+ .cast<RichText>()
+ .map((RichText richText) => richText.text)
+ .cast<TextSpan>()
+ .map((TextSpan e) => e.text)
+ .toList();
+ expect(text[0], 'abc');
+ expect(text[1], 'def');
+ expect(text[2], 'bar');
+ expect(text[3], 'baz');
+ expect(text[4], 'bar');
+ expect(table.defaultColumnWidth, columnWidth);
- // Paragraph text
- expect(text[5], 'bar');
+ // Paragraph text
+ expect(text[5], 'bar');
+ } else {
+ // For pkg:markdown > v6.0.1
+ expect(find.byType(RichText), findsNWidgets(7));
+ final List<String?> text = find
+ .byType(RichText)
+ .evaluate()
+ .map((Element e) => e.widget)
+ .cast<RichText>()
+ .map((RichText richText) => richText.text)
+ .cast<TextSpan>()
+ .map((TextSpan e) => e.text)
+ .toList();
+ expect(
+ text, <String>['abc', 'def', 'bar', 'baz', 'bar', '', 'bar']);
+ expect(table.defaultColumnWidth, columnWidth);
+ }
},
);
@@ -441,9 +464,7 @@
.toList();
expect(text[0], '| abc | def | | --- | | bar |');
},
- // TODO(mjordan56): Remove skip once the issue #341 in the markdown package
- // is fixed and released. https://github.com/dart-lang/markdown/issues/341
- skip: true,
+ skip: !_newMarkdown,
);
testWidgets(
@@ -468,22 +489,39 @@
expectTableSize(3, 2);
- expect(find.byType(RichText), findsNWidgets(5));
- final List<String?> cellText = find
- .byType(RichText)
- .evaluate()
- .map((Element e) => e.widget)
- .cast<RichText>()
- .map((RichText richText) => richText.text)
- .cast<TextSpan>()
- .map((TextSpan e) => e.text)
- .toList();
- expect(cellText[0], 'abc');
- expect(cellText[1], 'def');
- expect(cellText[2], 'bar');
- expect(cellText[3], 'bar');
- expect(cellText[4], 'baz');
- expect(table.defaultColumnWidth, columnWidth);
+ if (!_newMarkdown) {
+ // For pkg:markdown <= v6.0.1
+ expect(find.byType(RichText), findsNWidgets(5));
+ final List<String?> cellText = find
+ .byType(RichText)
+ .evaluate()
+ .map((Element e) => e.widget)
+ .cast<RichText>()
+ .map((RichText richText) => richText.text)
+ .cast<TextSpan>()
+ .map((TextSpan e) => e.text)
+ .toList();
+ expect(cellText[0], 'abc');
+ expect(cellText[1], 'def');
+ expect(cellText[2], 'bar');
+ expect(cellText[3], 'bar');
+ expect(cellText[4], 'baz');
+ expect(table.defaultColumnWidth, columnWidth);
+ } else {
+ // For pkg:markdown > v6.0.1
+ expect(find.byType(RichText), findsNWidgets(6));
+ final List<String?> cellText = find
+ .byType(RichText)
+ .evaluate()
+ .map((Element e) => e.widget)
+ .cast<RichText>()
+ .map((RichText richText) => richText.text)
+ .cast<TextSpan>()
+ .map((TextSpan e) => e.text)
+ .toList();
+ expect(cellText, <String>['abc', 'def', 'bar', '', 'bar', 'baz']);
+ expect(table.defaultColumnWidth, columnWidth);
+ }
},
);