Skip to content

Commit 8df9b36

Browse files
committed
🔧 Update Pack 2 #14
1 parent afc2c2a commit 8df9b36

File tree

4 files changed

+59
-20
lines changed

4 files changed

+59
-20
lines changed

example/lib/stories/text_animation.dart

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:flutter/scheduler.dart';
55
import 'package:google_fonts/google_fonts.dart';
66
import 'package:hyper_effects/hyper_effects.dart';
77
import 'package:smooth_page_indicator/smooth_page_indicator.dart';
8+
import 'package:unicode_emojis/unicode_emojis.dart';
89

910
class TextAnimation extends StatefulWidget {
1011
const TextAnimation({super.key});
@@ -78,6 +79,18 @@ class IPhone extends StatelessWidget {
7879
}
7980
}
8081

82+
final String _allEmojis =
83+
UnicodeEmojis.allEmojis.map((emoji) => emoji.emoji).join('');
84+
85+
class EmojiTapeBuilder extends CharacterTapeBuilder {
86+
@override
87+
String get characters => _allEmojis;
88+
89+
@override
90+
bool compare(String a, String b) =>
91+
_allEmojis.contains(a) && _allEmojis.contains(b);
92+
}
93+
8194
class EmojiLine extends StatefulWidget {
8295
const EmojiLine({super.key});
8396

@@ -111,7 +124,10 @@ class _EmojiLineState extends State<EmojiLine> {
111124
),
112125
)
113126
.roll(
114-
tapeStrategy: const ConsistentSymbolTapeStrategy(4),
127+
tapeStrategy:
128+
ConsistentSymbolTapeStrategy(4, characterTapeBuilders: {
129+
EmojiTapeBuilder(),
130+
}),
115131
tapeSlideDirection: TextTapeSlideDirection.alternating,
116132
staggerTapes: true,
117133
tapeCurve: Curves.easeInOutBack,
@@ -395,7 +411,9 @@ class _LikeButtonState extends State<LikeButton> {
395411
.copyWith(color: Colors.white, fontSize: 16),
396412
)
397413
.roll(
398-
tapeStrategy: const AllSymbolsTapeStrategy(false),
414+
tapeStrategy: const AllSymbolsTapeStrategy(
415+
repeatCharacters: false,
416+
),
399417
symbolDistanceMultiplier: 2,
400418
clipBehavior: Clip.none,
401419
tapeCurve: Curves.easeOutQuart,
@@ -452,8 +470,8 @@ class _LikeButtonState extends State<LikeButton> {
452470
.copyWith(color: Colors.white, fontSize: 16),
453471
)
454472
.roll(
455-
tapeStrategy:
456-
const ConsistentSymbolTapeStrategy(0, true),
473+
tapeStrategy: const ConsistentSymbolTapeStrategy(0,
474+
repeatCharacters: true),
457475
symbolDistanceMultiplier: 2,
458476
clipBehavior: Clip.none,
459477
tapeCurve:
@@ -505,8 +523,8 @@ class _LikeButtonState extends State<LikeButton> {
505523
.copyWith(color: Colors.white, fontSize: 16),
506524
)
507525
.roll(
508-
tapeStrategy:
509-
const ConsistentSymbolTapeStrategy(0, false),
526+
tapeStrategy: const ConsistentSymbolTapeStrategy(0,
527+
repeatCharacters: false),
510528
symbolDistanceMultiplier: 2,
511529
clipBehavior: Clip.none,
512530
tapeCurve: Curves.easeOutBack,

example/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies:
1515
hyper_effects:
1616
path: ../
1717
google_fonts: ^6.2.1
18+
unicode_emojis: ^0.4.0
1819
smooth_page_indicator: ^1.1.0
1920

2021
dev_dependencies:

lib/src/effects/roll/symbol_tape_strategy.dart

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import 'dart:math';
22

33
import 'package:flutter/widgets.dart';
4-
import 'package:unicode_emojis/unicode_emojis.dart';
54

65
const String _kLowerAlphabet = 'abcdefghijklmnopqrstuvwxyz ';
76
const String _kUpperAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
87
const String _kNumbers = '0123456789';
98
const String _kSymbols = '`~!@#\$%^&*()-_=+[{]}\\|;:\'",<.>/?';
109
const String _kSpace = ' ';
1110
const String _kZeroWidth = '​';
12-
final String _allEmojis =
13-
UnicodeEmojis.allEmojis.map((emoji) => emoji.emoji).join('');
1411

1512
extension _StringHelper on String {
1613
bool isSymbol() => _kSymbols.contains(this);
@@ -24,8 +21,18 @@ extension _StringHelper on String {
2421
bool isLowerAlphabet() => _kLowerAlphabet.contains(this);
2522

2623
bool isUpperAlphabet() => _kUpperAlphabet.contains(this);
24+
}
25+
26+
/// A builder that constructs a tape of characters tailored for a specific
27+
/// condition of characters such as emojis.
28+
abstract class CharacterTapeBuilder {
29+
/// The characters that are used to build the tape of characters. For example,
30+
/// an entire emoji set.
31+
String get characters;
2732

28-
bool isEmoji() => _allEmojis.contains(this);
33+
/// Compares two strings to determine if the builder should be used to
34+
/// insert the [characters] into the main tape.
35+
bool compare(String a, String b);
2936
}
3037

3138
/// Defines the strategy to create the tape of character symbols
@@ -38,9 +45,17 @@ sealed class SymbolTapeStrategy {
3845
/// be rendered and animated to roll twice instead of just once.
3946
final bool repeatCharacters;
4047

48+
/// A set of [CharacterTapeBuilder]s that can be used to build the tape
49+
/// of characters to interpolate to and from for a specific set of characters,
50+
/// like emojis.
51+
final Set<CharacterTapeBuilder> characterTapeBuilders;
52+
4153
/// Creates a new [SymbolTapeStrategy] with the given [repeatCharacters]
4254
/// property.
43-
const SymbolTapeStrategy([this.repeatCharacters = true]);
55+
const SymbolTapeStrategy({
56+
this.repeatCharacters = true,
57+
this.characterTapeBuilders = const {},
58+
});
4459

4560
/// Builds the tape of characters to interpolate to and from.
4661
String build(String a, String b) =>
@@ -68,8 +83,10 @@ sealed class SymbolTapeStrategy {
6883
if (a.isNumber() || b.isNumber()) {
6984
charKitBuffer.write(_kNumbers);
7085
}
71-
if (a.isEmoji() || b.isEmoji()) {
72-
charKitBuffer.write(_allEmojis);
86+
for (final CharacterTapeBuilder builder in characterTapeBuilders) {
87+
if (builder.compare(a, b)) {
88+
charKitBuffer.write(builder.characters);
89+
}
7390
}
7491
}
7592

@@ -107,8 +124,10 @@ sealed class SymbolTapeStrategy {
107124
if (a.isNumber() || b.isNumber()) {
108125
charKitBuffer.write(_kNumbers);
109126
}
110-
if (a.isEmoji() || b.isEmoji()) {
111-
charKitBuffer.write(_allEmojis);
127+
for (final CharacterTapeBuilder builder in characterTapeBuilders) {
128+
if (builder.compare(a, b)) {
129+
charKitBuffer.write(builder.characters);
130+
}
112131
}
113132
}
114133

@@ -137,7 +156,7 @@ sealed class SymbolTapeStrategy {
137156
class AllSymbolsTapeStrategy extends SymbolTapeStrategy {
138157
/// Creates a new [AllSymbolsTapeStrategy] with the given [repeatCharacters]
139158
/// property.
140-
const AllSymbolsTapeStrategy([super.repeatCharacters = true]);
159+
const AllSymbolsTapeStrategy({super.repeatCharacters = true});
141160
}
142161

143162
/// Constructs symbol tapes that contain all the characters
@@ -167,9 +186,11 @@ class ConsistentSymbolTapeStrategy extends SymbolTapeStrategy {
167186

168187
/// Creates a new [ConsistentSymbolTapeStrategy] with the given [distance]
169188
/// and [repeatCharacters] properties.
170-
const ConsistentSymbolTapeStrategy(this.distance,
171-
[super.repeatCharacters = true])
172-
: assert(
189+
const ConsistentSymbolTapeStrategy(
190+
this.distance, {
191+
super.repeatCharacters = true,
192+
Set<CharacterTapeBuilder> characterTapeBuilders = const {},
193+
}) : assert(
173194
distance >= 0,
174195
'Distance must be >= 0.',
175196
);

pubspec.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ dependencies:
1919
flutter:
2020
sdk: flutter
2121
equatable: ^2.0.5
22-
unicode_emojis: ^0.4.0
2322
collection: ^1.18.0
2423

2524
dev_dependencies:

0 commit comments

Comments
 (0)