Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate the color of a RaisedButton in Flutter?

Tags:

flutter

I have a RaisedButton. I want to animate it's color from green to red, and vice versa, every time the user clicks it.

How this could be done?

like image 532
Daniel Oliveira Avatar asked Nov 28 '18 13:11

Daniel Oliveira


People also ask

How do you animate colors in Flutter?

We shall use controller. reverse() function to reverse the animation. When you run this application and click on the colored button, color animation toggles using controller. forward() and controller.

How do you give a RaisedButton color in Flutter?

What you can do is create a "color" property (not required to set any value for it) in Zone class. Then set the color property of RaisedButton to zone.

How do you style a RaisedButton in Flutter?

Create RaisedButton and wrap it with Center widget. Give the child of RaisedButton as a Text widget. Perform onPressed function when the button is tapped. Perform optional onLongPress function when button is long pressed.

What is raisedbutton in flutter?

Flutter RaisedButton. In Flutter, RaisedButton widget is a material design concept of a button with elevation. It provides dimensionality to your UI along z-axis with clues of shadow. You can set many properties of RaisedButton like text color, button color, color of button when disabled, animation time, shape, elevation, padding, etc.

How to change raised button background color in flutter?

The Raised Button in flutter by default comes with a Argument or Prop named as color. The color argument is used to Set Change Raised Button Background Color in Flutter iOS Android mobile app. Color can support all the useful formats like Hex color code, ARGB, RGBA and also color constants.

How to animate color property in flutter?

When you run this application and click on the colored button, color animation toggles using controller.forward () and controller.reverse (). In this Flutter Tutorial, we learned how to animate Color property using Animation and AnimationController classes.

What is the use of raisedbutton in HTML?

It provides dimensionality to your UI along z-axis with clues of shadow. You can set many properties of RaisedButton like text color, button color, color of button when disabled, animation time, shape, elevation, padding, etc. You can also disable the button using enabled property.


2 Answers

You can use AnimatedContainer as raiseButton child . and when color changed it will be animated!

RaisedButton(
        onPressed: null,
        padding: EdgeInsets.all(0),
        child: AnimatedContainer(
          color: pageIndex == 1 ? Color(0xFF4B4B4B) : Color(0xFFD8D8D8),
          duration: Duration(milliseconds: 300),
        ),
      )

and its just a container so it can have child like text..

like image 160
Vahab Ghadiri Avatar answered Oct 24 '22 07:10

Vahab Ghadiri


class ChangeRaisedButtonColor extends StatefulWidget {
  @override
  ChangeRaisedButtonColorState createState() => ChangeRaisedButtonColorState();
}

class ChangeRaisedButtonColorState extends State<ChangeRaisedButtonColor>
    with SingleTickerProviderStateMixin {
  AnimationController _animationController;
  Animation _colorTween;

  @override
  void initState() {
    _animationController =
        AnimationController(vsync: this, duration: Duration(milliseconds: 300));
    _colorTween = ColorTween(begin: Colors.red, end: Colors.green)
        .animate(_animationController);

    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
    _animationController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _colorTween,
      builder: (context, child) => RaisedButton(
            child: Text("Change my color"),
            color: _colorTween.value,
            onPressed: () {
              if (_animationController.status == AnimationStatus.completed) {
                _animationController.reverse();
              } else {
                _animationController.forward();
              }
            },
          ),
    );
  }
}
like image 43
Andrey Turkovsky Avatar answered Oct 24 '22 07:10

Andrey Turkovsky