status_transitions.dart 1.49 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 9 10
/// A widget that rebuilds when the given animation changes status.
abstract class StatusTransitionWidget extends StatefulWidget {
  StatusTransitionWidget({
11
    Key key,
12
    this.animation
13
  }) : super(key: key) {
14
    assert(animation != null);
15 16
  }

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

  Widget build(BuildContext context);

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

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

33
  @override
34
  void didUpdateConfig(StatusTransitionWidget oldConfig) {
35
    if (config.animation != oldConfig.animation) {
36 37
      oldConfig.animation.removeStatusListener(_animationStatusChanged);
      config.animation.addStatusListener(_animationStatusChanged);
38 39 40
    }
  }

41
  @override
42
  void dispose() {
43
    config.animation.removeStatusListener(_animationStatusChanged);
44 45 46
    super.dispose();
  }

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

53
  @override
54 55 56 57
  Widget build(BuildContext context) {
    return config.build(context);
  }
}