From 8cc3f823b6d9023dea0fc795a86a8600ccf7a101 Mon Sep 17 00:00:00 2001 From: Anthony Whitford Date: Sun, 6 Dec 2020 01:09:19 -0800 Subject: [PATCH] Code optimizations for TextLiquidFill. --- lib/src/text_liquid_fill.dart | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/lib/src/text_liquid_fill.dart b/lib/src/text_liquid_fill.dart index 06fc3c6..4d1bb00 100644 --- a/lib/src/text_liquid_fill.dart +++ b/lib/src/text_liquid_fill.dart @@ -98,8 +98,7 @@ class _TextLiquidFillState extends State vsync: this, duration: widget.loadDuration, ); - - _loadValue = Tween(begin: 0.0, end: 100.0).animate(_loadController) + _loadValue = Tween(begin: 0.0, end: 1.0).animate(_loadController) ..addStatusListener((status) { if (AnimationStatus.completed == status) { // Stop the repeating wave when the load has completed @@ -113,10 +112,8 @@ class _TextLiquidFillState extends State @override void dispose() { - _waveController?.stop(); - _waveController?.dispose(); - _loadController?.stop(); - _loadController?.dispose(); + _waveController.dispose(); + _loadController.dispose(); super.dispose(); } @@ -133,8 +130,8 @@ class _TextLiquidFillState extends State return CustomPaint( painter: _WavePainter( textKey: _textKey, - waveAnimation: _waveController, - percentValue: _loadValue.value, + waveValue: _waveController.value, + loadValue: _loadValue.value, boxHeight: widget.boxHeight, waveColor: widget.waveColor, ), @@ -170,17 +167,17 @@ class _TextLiquidFillState extends State } class _WavePainter extends CustomPainter { - final _pi2 = 2 * pi; + static const _pi2 = 2 * pi; final GlobalKey textKey; - final Animation 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, }); @@ -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);