[flutter_markdown] [bug-fix] [#86138] Forcing a unique key for RichText widgets (#415)
diff --git a/packages/flutter_markdown/CHANGELOG.md b/packages/flutter_markdown/CHANGELOG.md
index cf67529..521aee5 100644
--- a/packages/flutter_markdown/CHANGELOG.md
+++ b/packages/flutter_markdown/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 0.6.3
+
+ * Fixed `onTap`, now the changed hyperlinks are reflected even with keeping the same link name unchanged.
+
## 0.6.2
* Updated metadata for new source location
diff --git a/packages/flutter_markdown/lib/src/builder.dart b/packages/flutter_markdown/lib/src/builder.dart
index b7191a3..1e0da20 100644
--- a/packages/flutter_markdown/lib/src/builder.dart
+++ b/packages/flutter_markdown/lib/src/builder.dart
@@ -742,19 +742,23 @@
: TextSpan(children: mergedSpans);
}
- Widget _buildRichText(TextSpan? text, {TextAlign? textAlign}) {
+ Widget _buildRichText(TextSpan? text, {TextAlign? textAlign, String? key}) {
+ //Adding a unique key prevents the problem of using the same link handler for text spans with the same text
+ key = key ?? '${text.hashCode}${DateTime.now().millisecondsSinceEpoch}';
if (selectable) {
return SelectableText.rich(
text!,
textScaleFactor: styleSheet.textScaleFactor,
textAlign: textAlign ?? TextAlign.start,
onTap: onTapText,
+ key: Key(key),
);
} else {
return RichText(
text: text!,
textScaleFactor: styleSheet.textScaleFactor!,
textAlign: textAlign ?? TextAlign.start,
+ key: Key(key),
);
}
}
diff --git a/packages/flutter_markdown/pubspec.yaml b/packages/flutter_markdown/pubspec.yaml
index d36ce6c..2d87412 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/master/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.2
+version: 0.6.3
environment:
sdk: ">=2.12.0 <3.0.0"
diff --git a/packages/flutter_markdown/test/link_test.dart b/packages/flutter_markdown/test/link_test.dart
index 53cc259..2bfe4a6 100644
--- a/packages/flutter_markdown/test/link_test.dart
+++ b/packages/flutter_markdown/test/link_test.dart
@@ -103,6 +103,43 @@
);
testWidgets(
+ 'multiple inline links with same name but different urls - unique keys are assigned automatically',
+ (WidgetTester tester) async {
+ //Arange
+ final Widget toBePumped = boilerplate(
+ Column(
+ children: <Widget>[
+ MarkdownBody(
+ data: '[link](link1.com)',
+ onTapLink: (String text, String? href, String title) {},
+ ),
+ MarkdownBody(
+ data: '[link](link2.com)',
+ onTapLink: (String text, String? href, String title) {},
+ ),
+ ],
+ ),
+ );
+
+ //Act
+ await tester.pumpWidget(toBePumped);
+
+ //Assert
+ final Finder widgetFinder = find.byType(RichText);
+ final List<Element> elements = widgetFinder.evaluate().toList();
+ final List<Widget> widgets =
+ elements.map((Element e) => e.widget).toList();
+
+ final List<String> keys = widgets
+ .where((Widget w) => w.key != null && w.key.toString().isNotEmpty)
+ .map((Widget w) => w.key.toString())
+ .toList();
+ expect(keys.length, 2); //Not empty
+ expect(keys.toSet().length, 2); // Unique
+ },
+ );
+
+ testWidgets(
// Example 493 from GFM.
'simple inline link',
(WidgetTester tester) async {