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
import 'basic.dart';
6 7
import 'framework.dart';

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

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

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

26
  @override
27
  _StatusTransitionState createState() => _StatusTransitionState();
28 29
}

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

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

46
  @override
47
  void dispose() {
48
    widget.animation.removeStatusListener(_animationStatusChanged);
49 50 51
    super.dispose();
  }

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

58
  @override
59
  Widget build(BuildContext context) {
60
    return widget.build(context);
61 62
  }
}