From b737ad8b6199eb867caf61b62eb03b3504ba5ddc Mon Sep 17 00:00:00 2001 From: Xoldarov Temur <69597351+xaldarof@users.noreply.github.com> Date: Sat, 29 Apr 2023 23:45:50 +0500 Subject: [PATCH 1/3] add custom selection actions to typewriter animation --- .idea/.gitignore | 3 + .idea/Animated-Text-Kit.iml | 18 +++ .idea/libraries/Dart_Packages.xml | 196 ++++++++++++++++++++++++++++ .idea/libraries/Dart_SDK.xml | 27 ++++ .idea/libraries/Flutter_Plugins.xml | 7 + .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 + lib/src/typewriter.dart | 18 ++- 8 files changed, 282 insertions(+), 1 deletion(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/Animated-Text-Kit.iml create mode 100644 .idea/libraries/Dart_Packages.xml create mode 100644 .idea/libraries/Dart_SDK.xml create mode 100644 .idea/libraries/Flutter_Plugins.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/Animated-Text-Kit.iml b/.idea/Animated-Text-Kit.iml new file mode 100644 index 0000000..a244ef9 --- /dev/null +++ b/.idea/Animated-Text-Kit.iml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml new file mode 100644 index 0000000..d88bea4 --- /dev/null +++ b/.idea/libraries/Dart_Packages.xml @@ -0,0 +1,196 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Dart_SDK.xml b/.idea/libraries/Dart_SDK.xml new file mode 100644 index 0000000..a82e8b9 --- /dev/null +++ b/.idea/libraries/Dart_SDK.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Flutter_Plugins.xml b/.idea/libraries/Flutter_Plugins.xml new file mode 100644 index 0000000..b0f6971 --- /dev/null +++ b/.idea/libraries/Flutter_Plugins.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..98863e9 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/lib/src/typewriter.dart b/lib/src/typewriter.dart index 860c9b5..3d5f328 100644 --- a/lib/src/typewriter.dart +++ b/lib/src/typewriter.dart @@ -6,6 +6,8 @@ import 'animated_text.dart'; /// /// ![Typewriter example](https://raw.githubusercontent.com/aagarwal1012/Animated-Text-Kit/master/display/typewriter.gif) class TypewriterAnimatedText extends AnimatedText { + final List? selectionActions; + // The text length is padded to cause extra cursor blinking after typing. static const extraLengthForBlinks = 8; @@ -28,6 +30,7 @@ class TypewriterAnimatedText extends AnimatedText { TextStyle? textStyle, this.speed = const Duration(milliseconds: 30), this.curve = Curves.linear, + this.selectionActions, this.cursor = '_', }) : super( text: text, @@ -54,7 +57,20 @@ class TypewriterAnimatedText extends AnimatedText { Widget completeText(BuildContext context) => RichText( text: TextSpan( children: [ - TextSpan(text: text), + if (selectionActions == null) TextSpan(text: text), + if (selectionActions != null) + WidgetSpan( + child: SelectableText( + text, + style: textStyle, + contextMenuBuilder: (context, editableTextState) { + return AdaptiveTextSelectionToolbar( + anchors: editableTextState.contextMenuAnchors, + children: selectionActions, + ); + }, + ), + ), TextSpan( text: cursor, style: const TextStyle(color: Colors.transparent), From 7228d21983c76c94a64f943b9d602e7f076a032a Mon Sep 17 00:00:00 2001 From: Xoldarov Temur <69597351+xaldarof@users.noreply.github.com> Date: Sat, 29 Apr 2023 23:56:03 +0500 Subject: [PATCH 2/3] fix readme --- README.md | 63 ++++++++++++++++++++++++++++--------------- example/lib/main.dart | 27 +++++++++++++++++-- 2 files changed, 67 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index b18288a..d1e3e93 100644 --- a/README.md +++ b/README.md @@ -281,27 +281,48 @@ return SizedBox( -```dart -return SizedBox( - width: 250.0, - child: DefaultTextStyle( - style: const TextStyle( - fontSize: 30.0, - fontFamily: 'Agne', - ), - child: AnimatedTextKit( - animatedTexts: [ - TypewriterAnimatedText('Discipline is the best tool'), - TypewriterAnimatedText('Design first, then code'), - TypewriterAnimatedText('Do not patch bugs out, rewrite them'), - TypewriterAnimatedText('Do not test bugs out, design them out'), - ], - onTap: () { - print("Tap Event"); - }, - ), - ), -); +``` + return SizedBox( + width: 250.0, + child: DefaultTextStyle( + style: const TextStyle( + fontSize: 30.0, + fontFamily: 'Agne', + ), + child: AnimatedTextKit( + animatedTexts: [ + TypewriterAnimatedText( + 'Discipline is the best tool', + selectionActions: [ + InkWell( + onTap: () { + FocusManager.instance.primaryFocus?.unfocus(); + }, + child: Row( + mainAxisSize: MainAxisSize.min, + children: [ + Container( + color: Colors.black, + padding: const EdgeInsets.all(12), + child: Text( + 'Custom action', + ), + ), + ], + ), + ), + ], + ), + TypewriterAnimatedText('Design first, then code', cursor: '|'), + TypewriterAnimatedText('Do not patch bugs out, rewrite them', + cursor: '<|>'), + TypewriterAnimatedText('Do not test bugs out, design them out', + cursor: '💡'), + ], + onTap: onTap, + ), + ), + ), ``` ## Scale diff --git a/example/lib/main.dart b/example/lib/main.dart index c5c618b..7592eb8 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -150,7 +150,9 @@ List animatedTextExamples({VoidCallback? onTap}) => ), child: AnimatedTextKit( animatedTexts: [ - RotateAnimatedText('AWESOME'), + RotateAnimatedText( + 'AWESOME', + ), RotateAnimatedText('OPTIMISTIC'), RotateAnimatedText( 'DIFFERENT', @@ -221,7 +223,28 @@ List animatedTextExamples({VoidCallback? onTap}) => ), child: AnimatedTextKit( animatedTexts: [ - TypewriterAnimatedText('Discipline is the best tool'), + TypewriterAnimatedText( + 'Discipline is the best tool', + selectionActions: [ + InkWell( + onTap: () { + FocusManager.instance.primaryFocus?.unfocus(); + }, + child: Row( + mainAxisSize: MainAxisSize.min, + children: [ + Container( + color: Colors.black, + padding: const EdgeInsets.all(12), + child: Text( + 'Custom action', + ), + ), + ], + ), + ), + ], + ), TypewriterAnimatedText('Design first, then code', cursor: '|'), TypewriterAnimatedText('Do not patch bugs out, rewrite them', cursor: '<|>'), From 43e7142525e4aa866447ca611cb3195a6c477e00 Mon Sep 17 00:00:00 2001 From: Xoldarov Temur <69597351+xaldarof@users.noreply.github.com> Date: Sun, 30 Apr 2023 00:22:49 +0500 Subject: [PATCH 3/3] add selected text callback --- example/lib/main.dart | 36 +++++++++++++++++++----------------- lib/src/typewriter.dart | 13 ++++++++++--- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 7592eb8..a127508 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -225,25 +225,27 @@ List animatedTextExamples({VoidCallback? onTap}) => animatedTexts: [ TypewriterAnimatedText( 'Discipline is the best tool', - selectionActions: [ - InkWell( - onTap: () { - FocusManager.instance.primaryFocus?.unfocus(); - }, - child: Row( - mainAxisSize: MainAxisSize.min, - children: [ - Container( - color: Colors.black, - padding: const EdgeInsets.all(12), - child: Text( - 'Custom action', + selectionActions: (selectedText) { + return [ + InkWell( + onTap: () { + FocusManager.instance.primaryFocus?.unfocus(); + }, + child: Row( + mainAxisSize: MainAxisSize.min, + children: [ + Container( + color: Colors.black, + padding: const EdgeInsets.all(12), + child: Text( + 'Custom action', + ), ), - ), - ], + ], + ), ), - ), - ], + ]; + }, ), TypewriterAnimatedText('Design first, then code', cursor: '|'), TypewriterAnimatedText('Do not patch bugs out, rewrite them', diff --git a/lib/src/typewriter.dart b/lib/src/typewriter.dart index 3d5f328..a974750 100644 --- a/lib/src/typewriter.dart +++ b/lib/src/typewriter.dart @@ -6,8 +6,6 @@ import 'animated_text.dart'; /// /// ![Typewriter example](https://raw.githubusercontent.com/aagarwal1012/Animated-Text-Kit/master/display/typewriter.gif) class TypewriterAnimatedText extends AnimatedText { - final List? selectionActions; - // The text length is padded to cause extra cursor blinking after typing. static const extraLengthForBlinks = 8; @@ -21,6 +19,9 @@ class TypewriterAnimatedText extends AnimatedText { /// By default it is set to Curves.linear. final Curve curve; + ///Action widgets for text selection + final List Function(String selectedText)? selectionActions; + /// Cursor text. Defaults to underscore. final String cursor; @@ -66,7 +67,13 @@ class TypewriterAnimatedText extends AnimatedText { contextMenuBuilder: (context, editableTextState) { return AdaptiveTextSelectionToolbar( anchors: editableTextState.contextMenuAnchors, - children: selectionActions, + children: selectionActions?.call(editableTextState + .currentTextEditingValue.text + .substring( + editableTextState + .currentTextEditingValue.selection.start, + editableTextState + .currentTextEditingValue.selection.end)), ); }, ),