status_transitions.dart 1.69 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
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
    super.key,
15
    required this.animation,
16
  });
17

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

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

25
  @override
26
  State<StatusTransitionWidget> createState() => _StatusTransitionState();
27 28
}

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

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

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

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

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