-
Notifications
You must be signed in to change notification settings - Fork 1
Setup annotated text with actions and highlights #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
org.gradle.jvmargs=-Xmx4G -XX:MaxMetaspaceSize=2G -XX:+HeapDumpOnOutOfMemoryError | ||
org.gradle.jvmargs=-Xmx8G -XX:MaxMetaspaceSize=4G -XX:ReservedCodeCacheSize=512m -XX:+HeapDumpOnOutOfMemoryError | ||
android.useAndroidX=true | ||
android.enableJetifier=true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import 'package:flutter/gestures.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
/// A widget that displays a text with inline actions. | ||
/// Supported formats: [text](action) or [text] | ||
/// This way translations can be done with inline actions. | ||
/// | ||
/// The text is displayed as a [RichText] widget. | ||
/// The annotations are displayed as a [TextSpan] widget with optionally a [TapGestureRecognizer] attached to it (if the action is not null). | ||
/// The [actions] map is used to map the action name to the action to perform when the text is tapped. | ||
/// The [defaultStyle] is the style of the default text. | ||
/// The [annotationStyle] is the style of the annotated text. | ||
/// | ||
/// [some text] only highlights the text, but does not trigger an action. | ||
/// [some text](action) highlights the text and triggers the action when tapped. | ||
/// [some text](action) without a defined action for the exact name 'action' will not trigger an action. | ||
/// | ||
/// Example: | ||
/// ```dart | ||
/// AnnotatedText( | ||
/// text: 'Hello [world](onWorldTapped)', | ||
/// actions: {'onWorldTapped': () => print('world')}, | ||
/// defaultStyle: TextStyle(color: Colors.black), | ||
/// annotationStyle: TextStyle(color: Colors.blue), | ||
/// ) | ||
/// ``` | ||
class AnnotatedText extends StatelessWidget { | ||
/// Creates a widget that displays a text with annotations. | ||
const AnnotatedText({ | ||
required this.text, | ||
required this.actions, | ||
required this.defaultStyle, | ||
required this.annotationStyle, | ||
super.key, | ||
}); | ||
|
||
/// The complete text to display. | ||
final String text; | ||
|
||
/// A map {actionName: action} of actions to perform when the text is tapped. | ||
final Map<String, VoidCallback>? actions; | ||
|
||
/// The style of the default text. | ||
final TextStyle defaultStyle; | ||
|
||
/// The style of the annotated text. | ||
final TextStyle annotationStyle; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return RichText( | ||
text: _buildTextSpan(text: text, defaultStyle: defaultStyle, annotationStyle: annotationStyle, actions: actions), | ||
); | ||
} | ||
} | ||
|
||
TextSpan _buildTextSpan({ | ||
required String text, | ||
required TextStyle defaultStyle, | ||
required TextStyle annotationStyle, | ||
Map<String, VoidCallback>? actions, | ||
}) { | ||
/// matches [text](action) with an action, or [text] without an action | ||
final regex = RegExp(r'\[([^\]]+?)\](?:\((.*?)\))?'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: What if the text contains a Maybe a feature for later moment when needed 😉 |
||
final spans = <TextSpan>[]; | ||
var currentIndex = 0; | ||
|
||
for (final match in regex.allMatches(text)) { | ||
final matchStart = match.start; | ||
final matchEnd = match.end; | ||
|
||
// Add normal text before match | ||
if (matchStart > currentIndex) { | ||
spans.add(TextSpan(text: text.substring(currentIndex, matchStart), style: defaultStyle)); | ||
} | ||
|
||
final displayText = match.group(1)!; | ||
final actionKey = match.group(2); | ||
final action = (actionKey != null && actionKey.isNotEmpty && actions != null) ? actions[actionKey] : null; | ||
|
||
spans.add( | ||
TextSpan( | ||
text: displayText, | ||
style: annotationStyle, | ||
recognizer: action != null ? (TapGestureRecognizer()..onTap = action) : null, | ||
), | ||
); | ||
|
||
currentIndex = matchEnd; | ||
} | ||
|
||
// Add remaining text | ||
if (currentIndex < text.length) { | ||
spans.add(TextSpan(text: text.substring(currentIndex), style: defaultStyle)); | ||
} | ||
|
||
return TextSpan(children: spans); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import 'package:dcc_toolkit/ui/annotated_text/annotated_text.dart'; | ||
import 'package:flutter/gestures.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
testWidgets('renders annotated text without action', (WidgetTester tester) async { | ||
await tester.pumpWidget( | ||
const MaterialApp( | ||
home: AnnotatedText( | ||
text: 'Hello [world]', | ||
actions: {}, | ||
defaultStyle: TextStyle(color: Colors.black), | ||
annotationStyle: TextStyle(color: Colors.blue), | ||
), | ||
), | ||
); | ||
|
||
// Get the only RichText widget in the tree | ||
final richTextWidget = tester.widget<RichText>(find.byType(RichText)); | ||
final rootSpan = richTextWidget.text as TextSpan; | ||
|
||
// Combine all spans into a single string | ||
final fullText = rootSpan.children!.map((span) => (span as TextSpan).text).join(); | ||
|
||
expect(fullText, equals('Hello world')); | ||
final annotatedSpan = rootSpan.children![1]; // "world" | ||
expect((annotatedSpan as TextSpan).style!.color, equals(Colors.blue)); | ||
}); | ||
|
||
testWidgets('annotated text applies annotationStyle', (WidgetTester tester) async { | ||
const annotationStyle = TextStyle(color: Colors.blue); | ||
|
||
await tester.pumpWidget( | ||
const MaterialApp( | ||
home: AnnotatedText( | ||
text: 'Hello [world]', | ||
actions: {}, | ||
defaultStyle: TextStyle(color: Colors.black), | ||
annotationStyle: annotationStyle, | ||
), | ||
), | ||
); | ||
|
||
final richText = tester.widget<RichText>(find.byType(RichText)); | ||
final rootSpan = richText.text as TextSpan; | ||
|
||
final annotatedSpan = rootSpan.children![1]; // "world" | ||
expect((annotatedSpan as TextSpan).style!.color, equals(annotationStyle.color)); | ||
}); | ||
|
||
testWidgets('annotated text with action has a GestureRecognizer', (WidgetTester tester) async { | ||
await tester.pumpWidget( | ||
MaterialApp( | ||
home: AnnotatedText( | ||
text: 'Click [here](onTap)', | ||
actions: {'onTap': () {}}, | ||
defaultStyle: const TextStyle(color: Colors.black), | ||
annotationStyle: const TextStyle(color: Colors.blue), | ||
), | ||
), | ||
); | ||
|
||
// Get the RichText widget | ||
final richTextWidget = tester.widget<RichText>(find.byType(RichText)); | ||
final rootSpan = richTextWidget.text as TextSpan; | ||
|
||
// Locate the annotated span (second span in the children list) | ||
final annotatedSpan = rootSpan.children![1] as TextSpan; | ||
|
||
// Verify a recognizer exists and is a TapGestureRecognizer | ||
expect(annotatedSpan.recognizer, isNotNull); | ||
expect(annotatedSpan.recognizer, isA<TapGestureRecognizer>()); | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.