1

I'm working with Flutter and I aim to create an animation that transitions from the top to the bottom of the screen, similar to a fill effect. I've attached a GIF of the desired effect and what I currently have.

Desired effect:

View

Current effect:

gif

Here's the code snippet for my current implementation:

import 'dart:ui' as ui;

import 'package:flutter/material.dart';

class CustomTimePainter extends CustomPainter {
  CustomTimePainter({
    required this.animation,
    required this.backgroundColor,
    required this.color,
  }) : super(repaint: animation);

  final Animation<double> animation;
  final Color backgroundColor, color;

  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()..color = backgroundColor;
    canvas.drawPaint(paint);

    final top = ui.lerpDouble(0, size.height, animation.value)!;
    Rect rect = Rect.fromLTRB(0, 0, size.width, top);
    Path path = Path()..addRect(rect);

    canvas.drawPath(path, paint..color = color);
  }

  @override
  bool shouldRepaint(CustomTimePainter old) {
    return animation.value != old.animation.value ||
        color != old.color ||
        backgroundColor != old.backgroundColor;
  }
}

I expect the animation to fill the screen from top to bottom, but instead, it does the opposite. I've tried adjusting the lerpDouble parameters without success. Any suggestions on how I can achieve the top-to-bottom animation effect as shown in the first GIF?

Thanks for any help you can provide.

1 Answer 1

1

Switch your rect

From

 Rect rect = Rect.fromLTRB(0, 0, size.width, top);

To

Rect rect = Rect.fromLTRB(0, top, size.width, size.height);

This way bottom will be always size.height.

More about Rect.fromLTRB

2
  • Hello @YeasinSheikh, unfortunately, this doesn't work, this is my GitHub repository: github.com/codemotozu/stack-overflow-questin if you want, you can try it by yourself and see what's going on Commented Jan 26, 2023 at 23:21
  • 1
    swap the lerpDouble like final top = ui.lerpDouble(size.height, 0, animation.value)!; Commented Jan 27, 2023 at 2:33

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.