status_transitions.dart 1.75 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

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
  const StatusTransitionWidget({
16
    Key key,
17
    @required this.animation,
18 19
  }) : assert(animation != null),
       super(key: key);
20

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

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

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

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

39
  @override
40
  void didUpdateWidget(StatusTransitionWidget oldWidget) {
41
    super.didUpdateWidget(oldWidget);
42 43 44
    if (widget.animation != oldWidget.animation) {
      oldWidget.animation.removeStatusListener(_animationStatusChanged);
      widget.animation.addStatusListener(_animationStatusChanged);
45 46 47
    }
  }

48
  @override
49
  void dispose() {
50
    widget.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
  Widget build(BuildContext context) {
62
    return widget.build(context);
63 64
  }
}