Skip to content

Added new animated text - Scramble Text Animation #362

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/ios/Flutter/AppFrameworkInfo.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>8.0</string>
<string>12.0</string>
</dict>
</plist>
13 changes: 8 additions & 5 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -127,7 +127,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1020;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = "";
TargetAttributes = {
97C146ED1CF9000F007C117D = {
Expand Down Expand Up @@ -171,10 +171,12 @@
/* Begin PBXShellScriptBuildPhase section */
3B06AD1E1E4923F5004D2608 /* Thin Binary */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${TARGET_BUILD_DIR}/${INFOPLIST_PATH}",
);
name = "Thin Binary";
outputPaths = (
Expand All @@ -185,6 +187,7 @@
};
9740EEB61CF901F6004384FC /* Run Script */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand Down Expand Up @@ -272,7 +275,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down Expand Up @@ -354,7 +357,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -403,7 +406,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1020"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -50,6 +50,7 @@
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
enableGPUValidationMode = "1"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
Expand Down
2 changes: 1 addition & 1 deletion example/ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import UIKit
import Flutter

@UIApplicationMain
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
Expand Down
4 changes: 4 additions & 0 deletions example/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,9 @@
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
</dict>
</plist>
48 changes: 48 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ List<AnimatedTextExample> animatedTextExamples({VoidCallback? onTap}) {
final wavyTextController = AnimatedTextController();
final flickerController = AnimatedTextController();
final combinationController = AnimatedTextController();
final scrambleController = AnimatedTextController();

return <AnimatedTextExample>[
AnimatedTextExample(
Expand Down Expand Up @@ -448,5 +449,52 @@ List<AnimatedTextExample> animatedTextExamples({VoidCallback? onTap}) {
],
),
),
AnimatedTextExample(
label: 'Scramble',
color: Colors.orange[800],
controller: rotateController,
child: ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const SizedBox(
width: 20.0,
height: 100.0,
),
const Text(
'Im a',
style: TextStyle(fontSize: 43.0),
),
const SizedBox(
width: 16.0,
height: 100.0,
),
DefaultTextStyle(
style: TextStyle(
fontSize: 40.0,
),
child: AnimatedTextKit(
animatedTexts: [
ScrambleAnimatedText(
'Mobile Dev.',
speed: const Duration(
milliseconds: 200,
),
),
ScrambleAnimatedText('Explorer'),
],
controller: scrambleController,
onTap: onTap,
isRepeatingAnimation: true,
totalRepeatCount: 10,
),
),
],
),
],
),
),
];
}
1 change: 1 addition & 0 deletions lib/animated_text_kit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export 'src/scale.dart';
export 'src/text_liquid_fill.dart';
export 'src/wavy.dart';
export 'src/flicker.dart';
export 'src/scramble.dart';
118 changes: 118 additions & 0 deletions lib/src/scramble.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import 'dart:async';
import 'dart:math';

import 'package:animated_text_kit/animated_text_kit.dart';
import 'package:flutter/material.dart';

class ScrambleAnimatedText extends AnimatedText {
// duration of single character
final Duration speed;

ScrambleAnimatedText(
String text, {
TextAlign textAlign = TextAlign.start,
TextStyle? textStyle,
this.speed = const Duration(milliseconds: 500),
}) : super(
text: text,
textAlign: textAlign,
textStyle: textStyle,
duration: speed * text.characters.length,
);

late Animation<double> _scrambleText;

@override
void initAnimation(AnimationController controller) {
_scrambleText = CurveTween(
curve: Curves.linear,
).animate(controller);
}

@override
Widget animatedBuilder(context, child) {
// get the count / text text length base from the animation value
final count = (_scrambleText.value * textCharacters.length).round();

return RichText(
text: TextSpan(
children: List.generate(
count,
(index) {
return WidgetSpan(
child: ScrambledChar(
char: textCharacters.elementAt(index),
scrambleDuration: speed,
style: textStyle,
),
);
},
),
),
);
}
}

class ScrambledChar extends StatefulWidget {
final String char;
final Duration scrambleDuration;
final TextStyle? style;

const ScrambledChar({
required this.char,
this.style,
required this.scrambleDuration,
}) : assert(char.length == 1, "Char need to only 1 character");

@override
State<ScrambledChar> createState() => _ScrambledCharState();
}

class _ScrambledCharState extends State<ScrambledChar> {
final Random _random = Random();
static const String _allChar = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'abcdefghijklmnopqrstuvwxyz'
'0123456789'
'!@#\$%^&*()_+-=[]{}|;:\'",.<>?/`~';

late String _displayChar;
late Timer _timer;

@override
void initState() {
super.initState();
_displayChar = _generateScrambledChar(widget.char);
_startScrambling();
}

@override
void dispose() {
_timer.cancel();
super.dispose();
}

String _generateScrambledChar(String text) {
return _allChar[_random.nextInt(_allChar.length)];
}

void _startScrambling() {
int elapsed = 0;
_timer = Timer.periodic(const Duration(milliseconds: 50), (timer) {
if (elapsed >= widget.scrambleDuration.inMilliseconds) {
setState(() => _displayChar = widget.char);
timer.cancel();
} else {
setState(() => _displayChar = _generateScrambledChar(widget.char));
elapsed += 50;
}
});
}

@override
Widget build(BuildContext context) {
return Text(
_displayChar,
style: widget.style,
);
}
}