1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. |
4 | |
5 | import 'basic.dart'; |
6 | import 'framework.dart'; |
7 | |
8 | /// A widget that rebuilds when the given animation changes status. |
9 | 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 | Widget build(BuildContext context); |
22 | |
23 | @override |
24 | State<StatusTransitionWidget> createState() => _StatusTransitionState(); |
25 | } |
26 | |
27 | class _StatusTransitionState extends State<StatusTransitionWidget> { |
28 | @override |
29 | void initState() { |
30 | super.initState(); |
31 | widget.animation.addStatusListener(_animationStatusChanged); |
32 | } |
33 | |
34 | @override |
35 | void didUpdateWidget(StatusTransitionWidget oldWidget) { |
36 | super.didUpdateWidget(oldWidget); |
37 | if (widget.animation != oldWidget.animation) { |
38 | oldWidget.animation.removeStatusListener(_animationStatusChanged); |
39 | widget.animation.addStatusListener(_animationStatusChanged); |
40 | } |
41 | } |
42 | |
43 | @override |
44 | void dispose() { |
45 | widget.animation.removeStatusListener(_animationStatusChanged); |
46 | super.dispose(); |
47 | } |
48 | |
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 | } |
60 | |