AnimatedWidget Widget

What is AnimatedWidget Widget?

A widget that rebuilds when the given Listenable changes value.

Abstract Class

The abstract class creates a widget that rebuilds when the given listenable changes.

abstract class AnimatedWidget extends StatefulWidget

Code snippet

The following code snippet shows how to configure AnimatedWidget widget.

class SpinningContainer extends AnimatedWidget {
  const SpinningContainer({Key key, AnimationController controller})
      : super(key: key, listenable: controller);

  Animation<double> get _progress => listenable;

  
  Widget build(BuildContext context) {
    return Transform.rotate(
      angle: _progress.value * 2.0 * math.pi,
      child: Container(
        width: 200.0,
        height: 200.0,
        color: Colors.blue[400],
      ),
    );
  }
}

Demo Code

import 'dart:math' as math;
import 'package:flutter/material.dart';

class Spinner extends StatefulWidget {
  
  _SpinnerState createState() => _SpinnerState();
}

class _SpinnerState extends State<Spinner> with TickerProviderStateMixin {
  AnimationController _controller;

  
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(seconds: 10),
      vsync: this,
    )..repeat();
  }

  
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  
  Widget build(BuildContext context) {
    return SpinningContainer(controller: _controller);
  }
}

class SpinningContainer extends AnimatedWidget {
  const SpinningContainer({Key key, AnimationController controller})
      : super(key: key, listenable: controller);

  Animation<double> get _progress => listenable;

  
  Widget build(BuildContext context) {
    return Transform.rotate(
      angle: _progress.value * 2.0 * math.pi,
      child: Container(
        width: 200.0,
        height: 200.0,
        color: Colors.blue[400],
      ),
    );
  }
}

class MyApp extends StatelessWidget {
  static const String _title = 'AnimatedWidget Demo';

  
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
          appBar: AppBar(title: const Text(_title)),
          body: Center(
            child: Spinner(),
          )),
    );
  }
}

void main() => runApp(MyApp());