Skip to content

Commit

Permalink
Code optimizations for TextLiquidFill.
Browse files Browse the repository at this point in the history
  • Loading branch information
awhitford committed Dec 6, 2020
1 parent df9c63a commit 8cc3f82
Showing 1 changed file with 12 additions and 19 deletions.
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

0 comments on commit 8cc3f82

Please sign in to comment.