|
| 1 | +import 'package:flutter/gestures.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | + |
| 4 | +/// A widget that displays a text with inline actions. |
| 5 | +/// Supported formats: |text(action)| or |text| |
| 6 | +/// This way translations can be done with inline actions. |
| 7 | +/// |
| 8 | +/// The text is displayed as a [RichText] widget. |
| 9 | +/// The annotations are displayed as a [TextSpan] widget with optionally a [TapGestureRecognizer] attached to it (if the action is not null). |
| 10 | +/// The [actions] map is used to map the action name to the action to perform when the text is tapped. |
| 11 | +/// The [defaultStyle] is the style of the default text. |
| 12 | +/// The [annotationStyle] is the style of the annotated text. |
| 13 | +class AnnotatedText extends StatelessWidget { |
| 14 | + /// Creates a widget that displays a text with annotations. |
| 15 | + const AnnotatedText({ |
| 16 | + required this.text, |
| 17 | + required this.actions, |
| 18 | + required this.defaultStyle, |
| 19 | + required this.annotationStyle, |
| 20 | + super.key, |
| 21 | + }); |
| 22 | + |
| 23 | + /// The complete text to display. |
| 24 | + final String text; |
| 25 | + |
| 26 | + /// A map {actionName: action} of actions to perform when the text is tapped. |
| 27 | + final Map<String, VoidCallback>? actions; |
| 28 | + |
| 29 | + /// The style of the default text. |
| 30 | + final TextStyle defaultStyle; |
| 31 | + |
| 32 | + /// The style of the annotated text. |
| 33 | + final TextStyle annotationStyle; |
| 34 | + |
| 35 | + @override |
| 36 | + Widget build(BuildContext context) { |
| 37 | + return RichText( |
| 38 | + text: _buildTextSpan( |
| 39 | + text: text, |
| 40 | + defaultStyle: defaultStyle, |
| 41 | + annotationStyle: annotationStyle, |
| 42 | + actions: actions, |
| 43 | + ), |
| 44 | + ); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +TextSpan _buildTextSpan({ |
| 49 | + required String text, |
| 50 | + required TextStyle defaultStyle, |
| 51 | + required TextStyle annotationStyle, |
| 52 | + Map<String, VoidCallback>? actions, |
| 53 | +}) { |
| 54 | + /// matches |text(function)| with an action, or |text| without an action |
| 55 | + final regex = RegExp(r'\|(.+?)(?:\((.*?)\))?\|'); |
| 56 | + final spans = <TextSpan>[]; |
| 57 | + var currentIndex = 0; |
| 58 | + |
| 59 | + for (final match in regex.allMatches(text)) { |
| 60 | + final matchStart = match.start; |
| 61 | + final matchEnd = match.end; |
| 62 | + |
| 63 | + // Add normal text before match |
| 64 | + if (matchStart > currentIndex) { |
| 65 | + spans.add(TextSpan(text: text.substring(currentIndex, matchStart), style: defaultStyle)); |
| 66 | + } |
| 67 | + |
| 68 | + final displayText = match.group(1)!; |
| 69 | + final actionKey = match.group(2); |
| 70 | + final action = (actionKey != null && actionKey.isNotEmpty && actions != null) ? actions[actionKey] : null; |
| 71 | + |
| 72 | + spans.add( |
| 73 | + TextSpan( |
| 74 | + text: displayText, |
| 75 | + style: annotationStyle, |
| 76 | + recognizer: action != null ? (TapGestureRecognizer()..onTap = action) : null, |
| 77 | + ), |
| 78 | + ); |
| 79 | + |
| 80 | + currentIndex = matchEnd; |
| 81 | + } |
| 82 | + |
| 83 | + // Add remaining text |
| 84 | + if (currentIndex < text.length) { |
| 85 | + spans.add(TextSpan(text: text.substring(currentIndex), style: defaultStyle)); |
| 86 | + } |
| 87 | + |
| 88 | + return TextSpan(children: spans); |
| 89 | +} |
0 commit comments