status_transitions.dart 1.73 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 6
import 'package:meta/meta.dart';

7
import 'basic.dart';
8 9
import 'framework.dart';

10 11
/// A widget that rebuilds when the given animation changes status.
abstract class StatusTransitionWidget extends StatefulWidget {
12 13 14
  /// Initializes fields for subclasses.
  ///
  /// The [animation] argument must not be null.
15
  StatusTransitionWidget({
16
    Key key,
17
    @required this.animation
18
  }) : super(key: key) {
19
    assert(animation != null);
20 21
  }

22
  /// The animation to which this widget is listening.
23
  final Animation<double> animation;
24

25
  /// Override this method to build widgets that depend on the current status
26
  /// of the animation.
27 28
  Widget build(BuildContext context);

29
  @override
30
  _StatusTransitionState createState() => new _StatusTransitionState();
31 32
}

33
class _StatusTransitionState extends State<StatusTransitionWidget> {
34
  @override
35 36
  void initState() {
    super.initState();
37
    config.animation.addStatusListener(_animationStatusChanged);
38 39
  }

40
  @override
41
  void didUpdateConfig(StatusTransitionWidget oldConfig) {
42
    if (config.animation != oldConfig.animation) {
43 44
      oldConfig.animation.removeStatusListener(_animationStatusChanged);
      config.animation.addStatusListener(_animationStatusChanged);
45 46 47
    }
  }

48
  @override
49
  void dispose() {
50
    config.animation.removeStatusListener(_animationStatusChanged);
51 52 53
    super.dispose();
  }

54
  void _animationStatusChanged(AnimationStatus status) {
55
    setState(() {
56
      // The animation's state is our build state, and it changed already.
57 58 59
    });
  }

60
  @override
61 62 63 64
  Widget build(BuildContext context) {
    return config.build(context);
  }
}