status_transitions.dart 1.64 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
  /// Initializes fields for subclasses.
11
  const StatusTransitionWidget({
12
    super.key,
13
    required this.animation,
14
  });
15

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

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

23
  @override
24
  State<StatusTransitionWidget> createState() => _StatusTransitionState();
25 26
}

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

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

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

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

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