Skip to content

Commit

Permalink
Replace RichText with Text.rich to support SelectionArea
Browse files Browse the repository at this point in the history
  • Loading branch information
heubergindustries committed Feb 16, 2024
1 parent 872d6d2 commit 774bbf9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
37 changes: 13 additions & 24 deletions packages/flutter_markdown/lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class MarkdownBuilder implements md.NodeVisitor {
/// Creates an object that builds a [Widget] tree from parsed Markdown.
MarkdownBuilder({
required this.delegate,
@Deprecated('To make markdown text selectable wrap it in SelectionArea')
required this.selectable,
required this.styleSheet,
required this.imageDirectory,
Expand All @@ -124,6 +125,7 @@ class MarkdownBuilder implements md.NodeVisitor {
/// If true, the text is selectable.
///
/// Defaults to false.
@Deprecated('To make markdown text selectable wrap it in SelectionArea')
final bool selectable;

/// Defines which [TextStyle] objects to use for each type of element.
Expand Down Expand Up @@ -718,19 +720,17 @@ class MarkdownBuilder implements md.NodeVisitor {
) {
final List<Widget> mergedTexts = <Widget>[];
for (final Widget child in children) {
if (mergedTexts.isNotEmpty &&
mergedTexts.last is RichText &&
child is RichText) {
final RichText previous = mergedTexts.removeLast() as RichText;
final TextSpan previousTextSpan = previous.text as TextSpan;
if (mergedTexts.isNotEmpty && mergedTexts.last is Text && child is Text) {
final Text previous = mergedTexts.removeLast() as Text;
final TextSpan previousTextSpan = previous.textSpan! as TextSpan;
final List<TextSpan> children = previousTextSpan.children != null
? previousTextSpan.children!
.map((InlineSpan span) => span is! TextSpan
? TextSpan(children: <InlineSpan>[span])
: span)
.toList()
: <TextSpan>[previousTextSpan];
children.add(child.text as TextSpan);
children.add(child.textSpan! as TextSpan);
final TextSpan? mergedSpan = _mergeSimilarTextSpans(children);
mergedTexts.add(_buildRichText(
mergedSpan,
Expand Down Expand Up @@ -867,24 +867,13 @@ class MarkdownBuilder implements md.NodeVisitor {
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
final Key k = key == null ? UniqueKey() : Key(key);
if (selectable) {
return SelectableText.rich(
text!,
// ignore: deprecated_member_use
textScaleFactor: styleSheet.textScaleFactor,
textAlign: textAlign ?? TextAlign.start,
onTap: onTapText,
key: k,
);
} else {
return RichText(
text: text!,
// ignore: deprecated_member_use
textScaleFactor: styleSheet.textScaleFactor!,
textAlign: textAlign ?? TextAlign.start,
key: k,
);
}
return Text.rich(
text!,
// ignore: deprecated_member_use
textScaleFactor: styleSheet.textScaleFactor!,
textAlign: textAlign ?? TextAlign.start,
key: k,
);
}

/// This allows a value of type T or T? to be treated as a value of type T?.
Expand Down
2 changes: 2 additions & 0 deletions packages/flutter_markdown/lib/src/widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ abstract class MarkdownWidget extends StatefulWidget {
const MarkdownWidget({
super.key,
required this.data,
@Deprecated('To make markdown text selectable wrap it in SelectionArea')
this.selectable = false,
this.styleSheet,
this.styleSheetTheme = MarkdownStyleSheetBaseTheme.material,
Expand Down Expand Up @@ -196,6 +197,7 @@ abstract class MarkdownWidget extends StatefulWidget {
/// If true, the text is selectable.
///
/// Defaults to false.
@Deprecated('To make markdown text selectable wrap it in SelectionArea')
final bool selectable;

/// The styles to use when displaying the Markdown.
Expand Down

0 comments on commit 774bbf9

Please sign in to comment.