-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathtext_morph.dart
286 lines (251 loc) · 6.87 KB
/
text_morph.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:fx_2_folder/stacked-cards/stacked_card.dart';
//Future note: Support different animation like blur fade to make it look better for another day!
class TextMorph extends StatelessWidget {
final String text;
final TextStyle? style;
const TextMorph({
super.key,
required this.text,
this.style,
});
@override
Widget build(BuildContext context) {
return FittedBox(
fit: BoxFit.scaleDown,
child: AnimatedSize(
duration: const Duration(milliseconds: 300),
curve: Curves.easeOut,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: _buildAnimatedCharacters(),
),
),
);
}
List<Widget> _buildAnimatedCharacters() {
final characters = text.split('');
return characters.asMap().entries.map((entry) {
final index = entry.key;
final char = entry.value;
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 0.5),
child: MorphCharacter(
key: ValueKey('${char}_$index'),
character: index == 0 ? char.toUpperCase() : char.toLowerCase(),
style: style,
),
);
}).toList();
}
}
class MorphCharacter extends StatefulWidget {
final String character;
final TextStyle? style;
const MorphCharacter({
super.key,
required this.character,
this.style,
});
@override
State<MorphCharacter> createState() => _MorphCharacterState();
}
class _MorphCharacterState extends State<MorphCharacter>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _scaleAnimation;
late Animation<double> _opacityAnimation;
late Animation<double> _slideAnimation;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(milliseconds: 600), // Increased duration
vsync: this,
);
// Custom curve for more dynamic animation
final customCurve = CurvedAnimation(
parent: _controller,
curve: Curves.elasticOut, // Bouncy effect
);
// Scale animation with elastic effect
_scaleAnimation = Tween<double>(
begin: 0.0,
end: 1.0,
).animate(customCurve);
// Opacity animation with custom curve
_opacityAnimation = Tween<double>(
begin: 0.0,
end: 1.0,
).animate(CurvedAnimation(
parent: _controller,
curve: const Interval(0.0, 0.5, curve: Curves.easeIn),
));
// Slide animation from bottom
_slideAnimation = Tween<double>(
begin: 10.0,
end: 0.0,
).animate(CurvedAnimation(
parent: _controller,
curve: Curves.easeOutQuad,
));
_controller.forward();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
void didUpdateWidget(MorphCharacter oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.character != widget.character) {
_controller.reset();
_controller.forward();
}
}
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _controller,
builder: (context, child) {
return Transform.translate(
offset: Offset(0, _slideAnimation.value),
child: Transform.scale(
scale: _scaleAnimation.value,
child: Opacity(
opacity: _opacityAnimation.value,
child: child,
),
),
);
},
child: Text(
widget.character,
style: widget.style?.copyWith(
fontSize: 44,
fontWeight: FontWeight.w500,
) ??
const TextStyle(
fontSize: 44,
fontWeight: FontWeight.w500,
),
),
);
}
}
class TextMorphButton extends StatefulWidget {
const TextMorphButton({super.key});
@override
State<TextMorphButton> createState() => _TextMorphButtonState();
}
class _TextMorphButtonState extends State<TextMorphButton> {
String text = 'Continue';
@override
Widget build(BuildContext context) {
final isDarkMode = Theme.of(context).brightness == Brightness.dark;
return SizedBox(
height: 40,
width: 120,
child: ElevatedButton(
onPressed: () {
setState(() {
text = text == 'Continue' ? 'Confirm' : 'Continue';
});
},
style: ElevatedButton.styleFrom(
backgroundColor: isDarkMode ? Colors.white : Colors.black,
foregroundColor: isDarkMode ? Colors.black : Colors.white,
elevation: 2,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
padding: const EdgeInsets.symmetric(horizontal: 16),
),
child: TextMorph(text: text),
),
);
}
}
//-------
class TextMorphDemo extends StatelessWidget {
const TextMorphDemo({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData.dark(),
darkTheme: ThemeData.dark(),
themeMode: ThemeMode.dark,
home: const Scaffold(
backgroundColor: Colors.black,
body: Center(child: FullScreenTextMorph()),
),
);
}
}
class FullScreenTextMorph extends StatefulWidget {
const FullScreenTextMorph({super.key});
@override
State<FullScreenTextMorph> createState() => _FullScreenTextMorphState();
}
class _FullScreenTextMorphState extends State<FullScreenTextMorph> {
final List<String> words = [
'Banter',
'Bunty',
'Bubbly',
'Biscuit',
'Circuit',
'Bangle',
'Banger',
];
int currentIndex = 0;
late Timer timer;
@override
void initState() {
super.initState();
timer = Timer.periodic(const Duration(milliseconds: 1200), (timer) {
setState(() {
currentIndex = (currentIndex + 1) % words.length;
});
});
}
@override
void dispose() {
timer.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.transparent,
body: Stack(
fit: StackFit.expand,
children: [
// Grid background
CustomPaint(
painter: GridPatternPainter(isDarkMode: true),
size: Size.infinite,
),
Container(
width: double.infinity,
height: double.infinity,
alignment: Alignment.centerLeft,
child: TextMorph(
text: words[currentIndex],
style: const TextStyle(
fontSize: 120,
fontWeight: FontWeight.bold,
color: Colors.white,
letterSpacing: -1,
),
),
),
],
),
);
}
}
// Your existing TextMorph, MorphCharacter classes remain the same...