| 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({super.key, required this.animation}); |
| 12 | |
| 13 | /// The animation to which this widget is listening. |
| 14 | final Animation<double> animation; |
| 15 | |
| 16 | /// Override this method to build widgets that depend on the current status |
| 17 | /// of the animation. |
| 18 | Widget build(BuildContext context); |
| 19 | |
| 20 | @override |
| 21 | State<StatusTransitionWidget> createState() => _StatusTransitionState(); |
| 22 | } |
| 23 | |
| 24 | class _StatusTransitionState extends State<StatusTransitionWidget> { |
| 25 | @override |
| 26 | void initState() { |
| 27 | super.initState(); |
| 28 | widget.animation.addStatusListener(_animationStatusChanged); |
| 29 | } |
| 30 | |
| 31 | @override |
| 32 | void didUpdateWidget(StatusTransitionWidget oldWidget) { |
| 33 | super.didUpdateWidget(oldWidget); |
| 34 | if (widget.animation != oldWidget.animation) { |
| 35 | oldWidget.animation.removeStatusListener(_animationStatusChanged); |
| 36 | widget.animation.addStatusListener(_animationStatusChanged); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | @override |
| 41 | void dispose() { |
| 42 | widget.animation.removeStatusListener(_animationStatusChanged); |
| 43 | super.dispose(); |
| 44 | } |
| 45 | |
| 46 | void _animationStatusChanged(AnimationStatus status) { |
| 47 | setState(() { |
| 48 | // The animation's state is our build state, and it changed already. |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | @override |
| 53 | Widget build(BuildContext context) { |
| 54 | return widget.build(context); |
| 55 | } |
| 56 | } |
| 57 | |