BenSabo
BenSabo

Reputation: 551

How to rebuild an AnimatedWidget in Flutter

I'm new in Flutter.

I have an AnimatedWidget which basically toggles an input textfield from a hidden area of the screen onto the visible part, however I need to reload view in some cases.

As far as I know the AnimatedWidget is somehow extended the StatefulWidget, however there is no way to call setState.

Thanks

Upvotes: 2

Views: 828

Answers (1)

Andrii Turkovskyi
Andrii Turkovskyi

Reputation: 29438

class MyState extends State<MyPage> with TickerProviderStateMixin {    
  AnimationController controller;

  @override
  void initState() {
    controller = AnimationController(duration: const Duration(milliseconds: 500), vsync: this);
    loadDbData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final colorTween =
      ColorTween(begin: Theme.of(context).primaryColor, end: Theme.of(context).primaryColorDark)
        .animate(controller);
    return AnimatedBuilder(
        animation: colorTween,
        builder: (buildContext, child) {
          return Container(
              color: colorTween.value,
          );
        });
  }

And for starting animation you can use:

controller.forward();
controller.forward(from: 0.5);
controller.reverse();

Or you can set listener like this:

colorTween.addStatusListener((status) {
    if (status == AnimationStatus.completed) {
      controller.reverse();
    } else if (status == AnimationStatus.dismissed) {
      controller.forward();
    }
  });

Hope it's what you need

Upvotes: 3

Related Questions