Skip to content

Commit

Permalink
Merge pull request #120 from awhitford/emoji_typer
Browse files Browse the repository at this point in the history
Added support for Emoji characters to the Typer. Resolves #118.
  • Loading branch information
aagarwal1012 committed Oct 12, 2020
2 parents 601083b + 57d9956 commit efdccb5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
64 changes: 36 additions & 28 deletions lib/src/typer.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:characters/characters.dart';
import 'package:flutter/material.dart';
import 'dart:math';
import 'dart:async';
Expand Down Expand Up @@ -110,7 +111,12 @@ class _TyperState extends State<TyperAnimatedTextKit>
_index = -1;

widget.text.forEach((text) {
_texts.add({'text': text, 'speed': _speed, 'pause': _pause});
_texts.add({
'text': text,
'chars': text.characters,
'speed': _speed,
'pause': _pause,
});
});

// Start animation
Expand All @@ -126,29 +132,31 @@ class _TyperState extends State<TyperAnimatedTextKit>

@override
Widget build(BuildContext context) {
final text = _texts[_index]['text'];
return GestureDetector(
onTap: _onTap,
child: _isCurrentlyPausing || !_controller.isAnimating
? Text(
_texts[_index]['text'],
style: widget.textStyle,
textAlign: widget.textAlign,
)
: AnimatedBuilder(
animation: _controller,
builder: (BuildContext context, Widget child) {
final int offset =
_texts[_index]['text'].length < _typingText.value
? _texts[_index]['text'].length
: _typingText.value;

return Text(
_texts[_index]['text'].substring(0, offset),
style: widget.textStyle,
textAlign: widget.textAlign,
);
},
));
onTap: _onTap,
child: _isCurrentlyPausing || !_controller.isAnimating
? Text(
text,
style: widget.textStyle,
textAlign: widget.textAlign,
)
: AnimatedBuilder(
animation: _controller,
builder: (BuildContext context, Widget child) {
final textCharacters = _texts[_index]['chars'];
final textLen = textCharacters.length;
final int offset =
textLen < _typingText.value ? textLen : _typingText.value;

return Text(
textCharacters.take(offset).toString(),
style: widget.textStyle,
textAlign: widget.textAlign,
);
},
),
);
}

void _nextAnimation() {
Expand All @@ -174,14 +182,14 @@ class _TyperState extends State<TyperAnimatedTextKit>

if (mounted) setState(() {});

final textLen = _texts[_index]['chars'].length;
_controller = AnimationController(
duration: _texts[_index]['speed'] * _texts[_index]['text'].length,
duration: _texts[_index]['speed'] * textLen,
vsync: this,
);

_typingText = StepTween(begin: 0, end: _texts[_index]['text'].length)
.animate(_controller)
..addStatusListener(_animationEndCallback);
_typingText = StepTween(begin: 0, end: textLen).animate(_controller)
..addStatusListener(_animationEndCallback);

_controller.forward();
}
Expand Down Expand Up @@ -213,7 +221,7 @@ class _TyperState extends State<TyperAnimatedTextKit>
} else {
final int pause = _texts[_index]['pause'].inMilliseconds;
final int left = _texts[_index]['speed'].inMilliseconds *
(_texts[_index]['text'].length - _typingText.value);
(_texts[_index]['chars'].length - _typingText.value);

_controller.stop();

Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ maintainer: Ayush Agarwal (@aagarwal1012)
dependencies:
flutter:
sdk: flutter
characters: ^1.0.0

dev_dependencies:
flutter_test:
Expand All @@ -17,4 +18,4 @@ flutter:

environment:
sdk: ">=2.0.0 <3.0.0"
flutter: ">=0.3.0 <2.0.0"
flutter: ">=0.3.0 <2.0.0"

0 comments on commit efdccb5

Please sign in to comment.