Home > Software design >  Default values for Animation<Color> variables
Default values for Animation<Color> variables

Time:08-12

I am working on some code I did not create myself and am running into an error that I can't quite figure out.

In the app, there is a component called Spinner. Below are the requirements for Spinner.

final Widget child;
final bool loading;
final double opacity;
final Color color;
final Animation<Color> value;

Spinner({Key key, u/required this.child, u/required this.loading, this.opacity = 0.3, this.color = Colors.grey, this.value}) : super(key: key);

However, when I implemented this version of Spinner, my Android Studio flagged the last variable (i.e., value) as a problem. It wanted this to be required, though it wasn't before. So, I made it required, and it tells me that the default value cannot be Null. However, I can't seem to enter a default value. I also can't find any documentation that makes it clear how to enter a default value for this type of variable.

CodePudding user response:

In this case, the problem is not to find any animation to make the compiler happy, it is to find the animation that make sense to the UI design.

For example, I can fit the default value like this:

final Animation<Color> value =
        Tween<Color>(begin: Colors.white, end: Colors.black).animate(
            AnimationController(
                vsync: this, duration: Duration(milliseconds: 2500)));

This transforms a white color to black in 2.5 seconds. Is this what you want the default behaviour to look like? Maybe, maybe not. It depends on the design.

If you are not sure about the design, why not making it nullable for now?

CodePudding user response:

If you like to have single value for animation you can pass

value: AlwaysStoppedAnimation(Colors.red)

And for more customization, create a ColorTween like with animation controller

  • Related