Skip to content
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

Code optimizations for TextLiquidFill. #165

Merged
merged 1 commit into from
Dec 6, 2020
Merged
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
31 changes: 12 additions & 19 deletions lib/src/text_liquid_fill.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ class _TextLiquidFillState extends State<TextLiquidFill>
vsync: this,
duration: widget.loadDuration,
);

_loadValue = Tween<double>(begin: 0.0, end: 100.0).animate(_loadController)
_loadValue = Tween<double>(begin: 0.0, end: 1.0).animate(_loadController)
..addStatusListener((status) {
if (AnimationStatus.completed == status) {
// Stop the repeating wave when the load has completed
Expand All @@ -113,10 +112,8 @@ class _TextLiquidFillState extends State<TextLiquidFill>

@override
void dispose() {
_waveController?.stop();
_waveController?.dispose();
_loadController?.stop();
_loadController?.dispose();
_waveController.dispose();
_loadController.dispose();
super.dispose();
}

Expand All @@ -133,8 +130,8 @@ class _TextLiquidFillState extends State<TextLiquidFill>
return CustomPaint(
painter: _WavePainter(
textKey: _textKey,
waveAnimation: _waveController,
percentValue: _loadValue.value,
waveValue: _waveController.value,
loadValue: _loadValue.value,
boxHeight: widget.boxHeight,
waveColor: widget.waveColor,
),
Expand Down Expand Up @@ -170,17 +167,17 @@ class _TextLiquidFillState extends State<TextLiquidFill>
}

class _WavePainter extends CustomPainter {
final _pi2 = 2 * pi;
static const _pi2 = 2 * pi;
final GlobalKey textKey;
final Animation<double> waveAnimation;
final double percentValue;
final double waveValue;
final double loadValue;
final double boxHeight;
final Color waveColor;

_WavePainter({
@required this.textKey,
this.waveAnimation,
this.percentValue,
this.waveValue,
this.loadValue,
this.boxHeight,
this.waveColor,
});
Expand All @@ -189,19 +186,15 @@ class _WavePainter extends CustomPainter {
void paint(Canvas canvas, Size size) {
final RenderBox textBox = textKey.currentContext.findRenderObject();
final textHeight = textBox.size.height;
final percent = percentValue / 100.0;
final baseHeight =
(boxHeight / 2) + (textHeight / 2) - (percent * textHeight);
(boxHeight / 2) + (textHeight / 2) - (loadValue * textHeight);

final width = size.width ?? 200;
final height = size.height ?? 200;
final path = Path();
path.moveTo(0.0, baseHeight);
for (var i = 0.0; i < width; i++) {
path.lineTo(
i,
baseHeight + sin((i / width * _pi2) + (waveAnimation.value * _pi2)) * 8,
);
path.lineTo(i, baseHeight + sin(_pi2 * (i / width + waveValue)) * 8);
}

path.lineTo(width, height);
Expand Down