status_transitions.dart 1.46 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'basic.dart';
6 7
import 'framework.dart';

8
/// A component that rebuilds when the given animation changes status.
9 10 11
abstract class StatusTransitionComponent extends StatefulComponent {
  StatusTransitionComponent({
    Key key,
12
    this.animation
13
  }) : super(key: key) {
14
    assert(animation != null);
15 16
  }

17
  /// The animation to which this component is listening.
18
  final Animation<double> animation;
19 20 21

  Widget build(BuildContext context);

22
  _StatusTransitionState createState() => new _StatusTransitionState();
23 24
}

25
class _StatusTransitionState extends State<StatusTransitionComponent> {
26 27
  void initState() {
    super.initState();
28
    config.animation.addStatusListener(_animationStatusChanged);
29 30 31
  }

  void didUpdateConfig(StatusTransitionComponent oldConfig) {
32
    if (config.animation != oldConfig.animation) {
33 34
      oldConfig.animation.removeStatusListener(_animationStatusChanged);
      config.animation.addStatusListener(_animationStatusChanged);
35 36 37 38
    }
  }

  void dispose() {
39
    config.animation.removeStatusListener(_animationStatusChanged);
40 41 42
    super.dispose();
  }

43
  void _animationStatusChanged(AnimationStatus status) {
44
    setState(() {
45
      // The animation's state is our build state, and it changed already.
46 47 48 49 50 51 52
    });
  }

  Widget build(BuildContext context) {
    return config.build(context);
  }
}