|
| 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 | +/// |
| 14 | +/// [some text] only highlights the text, but does not trigger an action. |
| 15 | +/// [some text](action) highlights the text and triggers the action when tapped. |
| 16 | +/// [some text](action) without a defined action for the exact name 'action' will not trigger an action. |
| 17 | +/// |
| 18 | +/// Example: |
| 19 | +/// ```dart |
| 20 | +/// AnnotatedText( |
| 21 | +/// text: 'Hello [world](onWorldTapped)', |
| 22 | +/// actions: {'onWorldTapped': () => print('world')}, |
| 23 | +/// defaultStyle: TextStyle(color: Colors.black), |
| 24 | +/// annotationStyle: TextStyle(color: Colors.blue), |
| 25 | +/// ) |
| 26 | +/// ``` |
| 27 | +class AnnotatedText extends StatelessWidget { |
| 28 | + /// Creates a widget that displays a text with annotations. |
| 29 | + const AnnotatedText({ |
| 30 | + required this.text, |
| 31 | + required this.actions, |
| 32 | + required this.defaultStyle, |
| 33 | + required this.annotationStyle, |
| 34 | + super.key, |
| 35 | + }); |
| 36 | + |
| 37 | + /// The complete text to display. |
| 38 | + final String text; |
| 39 | + |
| 40 | + /// A map {actionName: action} of actions to perform when the text is tapped. |
| 41 | + final Map<String, VoidCallback>? actions; |
| 42 | + |
| 43 | + /// The style of the default text. |
| 44 | + final TextStyle defaultStyle; |
| 45 | + |
| 46 | + /// The style of the annotated text. |
| 47 | + final TextStyle annotationStyle; |
| 48 | + |
| 49 | + @override |
| 50 | + Widget build(BuildContext context) { |
| 51 | + return RichText( |
| 52 | + text: _buildTextSpan(text: text, defaultStyle: defaultStyle, annotationStyle: annotationStyle, actions: actions), |
| 53 | + ); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +TextSpan _buildTextSpan({ |
| 58 | + required String text, |
| 59 | + required TextStyle defaultStyle, |
| 60 | + required TextStyle annotationStyle, |
| 61 | + Map<String, VoidCallback>? actions, |
| 62 | +}) { |
| 63 | + /// matches [text](action) with an action, or [text] without an action |
| 64 | + final regex = RegExp(r'\[([^\]]+?)\](?:\((.*?)\))?'); |
| 65 | + final spans = <TextSpan>[]; |
| 66 | + var currentIndex = 0; |
| 67 | + |
| 68 | + for (final match in regex.allMatches(text)) { |
| 69 | + final matchStart = match.start; |
| 70 | + final matchEnd = match.end; |
| 71 | + |
| 72 | + // Add normal text before match |
| 73 | + if (matchStart > currentIndex) { |
| 74 | + spans.add(TextSpan(text: text.substring(currentIndex, matchStart), style: defaultStyle)); |
| 75 | + } |
| 76 | + |
| 77 | + final displayText = match.group(1)!; |
| 78 | + final actionKey = match.group(2); |
| 79 | + final action = (actionKey != null && actionKey.isNotEmpty && actions != null) ? actions[actionKey] : null; |
| 80 | + |
| 81 | + spans.add( |
| 82 | + TextSpan( |
| 83 | + text: displayText, |
| 84 | + style: annotationStyle, |
| 85 | + recognizer: action != null ? (TapGestureRecognizer()..onTap = action) : null, |
| 86 | + ), |
| 87 | + ); |
| 88 | + |
| 89 | + currentIndex = matchEnd; |
| 90 | + } |
| 91 | + |
| 92 | + // Add remaining text |
| 93 | + if (currentIndex < text.length) { |
| 94 | + spans.add(TextSpan(text: text.substring(currentIndex), style: defaultStyle)); |
| 95 | + } |
| 96 | + |
| 97 | + return TextSpan(children: spans); |
| 98 | +} |
0 commit comments