| 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 'package:flutter/animation.dart'; |
| 6 | import 'package:flutter/foundation.dart'; |
| 7 | |
| 8 | import 'box.dart'; |
| 9 | import 'layer.dart'; |
| 10 | import 'object.dart'; |
| 11 | import 'shifted_box.dart'; |
| 12 | |
| 13 | /// A [RenderAnimatedSize] can be in exactly one of these states. |
| 14 | @visibleForTesting |
| 15 | enum RenderAnimatedSizeState { |
| 16 | /// The initial state, when we do not yet know what the starting and target |
| 17 | /// sizes are to animate. |
| 18 | /// |
| 19 | /// The next state is [stable]. |
| 20 | start, |
| 21 | |
| 22 | /// At this state the child's size is assumed to be stable and we are either |
| 23 | /// animating, or waiting for the child's size to change. |
| 24 | /// |
| 25 | /// If the child's size changes, the state will become [changed]. Otherwise, |
| 26 | /// it remains [stable]. |
| 27 | stable, |
| 28 | |
| 29 | /// At this state we know that the child has changed once after being assumed |
| 30 | /// [stable]. |
| 31 | /// |
| 32 | /// The next state will be one of: |
| 33 | /// |
| 34 | /// * [stable] if the child's size stabilized immediately. This is a signal |
| 35 | /// for the render object to begin animating the size towards the child's new |
| 36 | /// size. |
| 37 | /// |
| 38 | /// * [unstable] if the child's size continues to change. |
| 39 | changed, |
| 40 | |
| 41 | /// At this state the child's size is assumed to be unstable (changing each |
| 42 | /// frame). |
| 43 | /// |
| 44 | /// Instead of chasing the child's size in this state, the render object |
| 45 | /// tightly tracks the child's size until it stabilizes. |
| 46 | /// |
| 47 | /// The render object remains in this state until a frame where the child's |
| 48 | /// size remains the same as the previous frame. At that time, the next state |
| 49 | /// is [stable]. |
| 50 | unstable, |
| 51 | } |
| 52 | |
| 53 | /// A render object that animates its size to its child's size over a given |
| 54 | /// [duration] and with a given [curve]. If the child's size itself animates |
| 55 | /// (i.e. if it changes size two frames in a row, as opposed to abruptly |
| 56 | /// changing size in one frame then remaining that size in subsequent frames), |
| 57 | /// this render object sizes itself to fit the child instead of animating |
| 58 | /// itself. |
| 59 | /// |
| 60 | /// When the child overflows the current animated size of this render object, it |
| 61 | /// is clipped. |
| 62 | class RenderAnimatedSize extends RenderAligningShiftedBox { |
| 63 | /// Creates a render object that animates its size to match its child. |
| 64 | /// The [duration] and [curve] arguments define the animation. |
| 65 | /// |
| 66 | /// The [alignment] argument is used to align the child when the parent is not |
| 67 | /// (yet) the same size as the child. |
| 68 | /// |
| 69 | /// The [duration] is required. |
| 70 | /// |
| 71 | /// The [vsync] should specify a [TickerProvider] for the animation |
| 72 | /// controller. |
| 73 | /// |
| 74 | /// The arguments [duration], [curve], [alignment], and [vsync] must |
| 75 | /// not be null. |
| 76 | RenderAnimatedSize({ |
| 77 | required TickerProvider vsync, |
| 78 | required Duration duration, |
| 79 | Duration? reverseDuration, |
| 80 | Curve curve = Curves.linear, |
| 81 | super.alignment, |
| 82 | super.textDirection, |
| 83 | super.child, |
| 84 | Clip clipBehavior = Clip.hardEdge, |
| 85 | VoidCallback? onEnd, |
| 86 | }) : _vsync = vsync, |
| 87 | _clipBehavior = clipBehavior { |
| 88 | _controller = |
| 89 | AnimationController(vsync: vsync, duration: duration, reverseDuration: reverseDuration) |
| 90 | ..addListener(() { |
| 91 | if (_controller.value != _lastValue) { |
| 92 | markNeedsLayout(); |
| 93 | } |
| 94 | }); |
| 95 | _animation = CurvedAnimation(parent: _controller, curve: curve); |
| 96 | _onEnd = onEnd; |
| 97 | } |
| 98 | |
| 99 | /// When asserts are enabled, returns the animation controller that is used |
| 100 | /// to drive the resizing. |
| 101 | /// |
| 102 | /// Otherwise, returns null. |
| 103 | /// |
| 104 | /// This getter is intended for use in framework unit tests. Applications must |
| 105 | /// not depend on its value. |
| 106 | @visibleForTesting |
| 107 | AnimationController? get debugController { |
| 108 | AnimationController? controller; |
| 109 | assert(() { |
| 110 | controller = _controller; |
| 111 | return true; |
| 112 | }()); |
| 113 | return controller; |
| 114 | } |
| 115 | |
| 116 | /// When asserts are enabled, returns the animation that drives the resizing. |
| 117 | /// |
| 118 | /// Otherwise, returns null. |
| 119 | /// |
| 120 | /// This getter is intended for use in framework unit tests. Applications must |
| 121 | /// not depend on its value. |
| 122 | @visibleForTesting |
| 123 | CurvedAnimation? get debugAnimation { |
| 124 | CurvedAnimation? animation; |
| 125 | assert(() { |
| 126 | animation = _animation; |
| 127 | return true; |
| 128 | }()); |
| 129 | return animation; |
| 130 | } |
| 131 | |
| 132 | late final AnimationController _controller; |
| 133 | late final CurvedAnimation _animation; |
| 134 | |
| 135 | final SizeTween _sizeTween = SizeTween(); |
| 136 | late bool _hasVisualOverflow; |
| 137 | double? _lastValue; |
| 138 | |
| 139 | /// The state this size animation is in. |
| 140 | /// |
| 141 | /// See [RenderAnimatedSizeState] for possible states. |
| 142 | @visibleForTesting |
| 143 | RenderAnimatedSizeState get state => _state; |
| 144 | RenderAnimatedSizeState _state = RenderAnimatedSizeState.start; |
| 145 | |
| 146 | /// The duration of the animation. |
| 147 | Duration get duration => _controller.duration!; |
| 148 | set duration(Duration value) { |
| 149 | if (value == _controller.duration) { |
| 150 | return; |
| 151 | } |
| 152 | _controller.duration = value; |
| 153 | } |
| 154 | |
| 155 | /// The duration of the animation when running in reverse. |
| 156 | Duration? get reverseDuration => _controller.reverseDuration; |
| 157 | set reverseDuration(Duration? value) { |
| 158 | if (value == _controller.reverseDuration) { |
| 159 | return; |
| 160 | } |
| 161 | _controller.reverseDuration = value; |
| 162 | } |
| 163 | |
| 164 | /// The curve of the animation. |
| 165 | Curve get curve => _animation.curve; |
| 166 | set curve(Curve value) { |
| 167 | if (value == _animation.curve) { |
| 168 | return; |
| 169 | } |
| 170 | _animation.curve = value; |
| 171 | } |
| 172 | |
| 173 | /// {@macro flutter.material.Material.clipBehavior} |
| 174 | /// |
| 175 | /// Defaults to [Clip.hardEdge]. |
| 176 | Clip get clipBehavior => _clipBehavior; |
| 177 | Clip _clipBehavior = Clip.hardEdge; |
| 178 | set clipBehavior(Clip value) { |
| 179 | if (value != _clipBehavior) { |
| 180 | _clipBehavior = value; |
| 181 | markNeedsPaint(); |
| 182 | markNeedsSemanticsUpdate(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /// Whether the size is being currently animated towards the child's size. |
| 187 | /// |
| 188 | /// See [RenderAnimatedSizeState] for situations when we may not be animating |
| 189 | /// the size. |
| 190 | bool get isAnimating => _controller.isAnimating; |
| 191 | |
| 192 | /// The [TickerProvider] for the [AnimationController] that runs the animation. |
| 193 | TickerProvider get vsync => _vsync; |
| 194 | TickerProvider _vsync; |
| 195 | set vsync(TickerProvider value) { |
| 196 | if (value == _vsync) { |
| 197 | return; |
| 198 | } |
| 199 | _vsync = value; |
| 200 | _controller.resync(vsync); |
| 201 | } |
| 202 | |
| 203 | /// Called every time an animation completes. |
| 204 | /// |
| 205 | /// This can be useful to trigger additional actions (e.g. another animation) |
| 206 | /// at the end of the current animation. |
| 207 | VoidCallback? get onEnd => _onEnd; |
| 208 | VoidCallback? _onEnd; |
| 209 | set onEnd(VoidCallback? value) { |
| 210 | if (value == _onEnd) { |
| 211 | return; |
| 212 | } |
| 213 | _onEnd = value; |
| 214 | } |
| 215 | |
| 216 | @override |
| 217 | void attach(PipelineOwner owner) { |
| 218 | super.attach(owner); |
| 219 | switch (state) { |
| 220 | case RenderAnimatedSizeState.start: |
| 221 | case RenderAnimatedSizeState.stable: |
| 222 | break; |
| 223 | case RenderAnimatedSizeState.changed: |
| 224 | case RenderAnimatedSizeState.unstable: |
| 225 | // Call markNeedsLayout in case the RenderObject isn't marked dirty |
| 226 | // already, to resume interrupted resizing animation. |
| 227 | markNeedsLayout(); |
| 228 | } |
| 229 | _controller.addStatusListener(_animationStatusListener); |
| 230 | } |
| 231 | |
| 232 | @override |
| 233 | void detach() { |
| 234 | _controller.stop(); |
| 235 | _controller.removeStatusListener(_animationStatusListener); |
| 236 | super.detach(); |
| 237 | } |
| 238 | |
| 239 | Size? get _animatedSize { |
| 240 | return _sizeTween.evaluate(_animation); |
| 241 | } |
| 242 | |
| 243 | late Size _currentSize; |
| 244 | |
| 245 | @override |
| 246 | void performLayout() { |
| 247 | _lastValue = _controller.value; |
| 248 | _hasVisualOverflow = false; |
| 249 | final BoxConstraints constraints = this.constraints; |
| 250 | if (child == null || constraints.isTight) { |
| 251 | _controller.stop(); |
| 252 | size = _currentSize = _sizeTween.begin = _sizeTween.end = constraints.smallest; |
| 253 | _state = RenderAnimatedSizeState.start; |
| 254 | child?.layout(constraints); |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | child!.layout(constraints, parentUsesSize: true); |
| 259 | |
| 260 | switch (_state) { |
| 261 | case RenderAnimatedSizeState.start: |
| 262 | _layoutStart(); |
| 263 | case RenderAnimatedSizeState.stable: |
| 264 | _layoutStable(); |
| 265 | case RenderAnimatedSizeState.changed: |
| 266 | _layoutChanged(); |
| 267 | case RenderAnimatedSizeState.unstable: |
| 268 | _layoutUnstable(); |
| 269 | } |
| 270 | |
| 271 | size = _currentSize = constraints.constrain(_animatedSize!); |
| 272 | alignChild(); |
| 273 | |
| 274 | if (size.width < _sizeTween.end!.width || size.height < _sizeTween.end!.height) { |
| 275 | _hasVisualOverflow = true; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | @override |
| 280 | @protected |
| 281 | Size computeDryLayout(covariant BoxConstraints constraints) { |
| 282 | if (child == null || constraints.isTight) { |
| 283 | return constraints.smallest; |
| 284 | } |
| 285 | |
| 286 | // This simplified version of performLayout only calculates the current |
| 287 | // size without modifying global state. See performLayout for comments |
| 288 | // explaining the rational behind the implementation. |
| 289 | final Size childSize = child!.getDryLayout(constraints); |
| 290 | switch (_state) { |
| 291 | case RenderAnimatedSizeState.start: |
| 292 | return constraints.constrain(childSize); |
| 293 | case RenderAnimatedSizeState.stable: |
| 294 | if (_sizeTween.end != childSize) { |
| 295 | return constraints.constrain(_currentSize); |
| 296 | } else if (_controller.value == _controller.upperBound) { |
| 297 | return constraints.constrain(childSize); |
| 298 | } |
| 299 | case RenderAnimatedSizeState.unstable: |
| 300 | case RenderAnimatedSizeState.changed: |
| 301 | if (_sizeTween.end != childSize) { |
| 302 | return constraints.constrain(childSize); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | return constraints.constrain(_animatedSize!); |
| 307 | } |
| 308 | |
| 309 | void _restartAnimation() { |
| 310 | _lastValue = 0.0; |
| 311 | _controller.forward(from: 0.0); |
| 312 | } |
| 313 | |
| 314 | /// Laying out the child for the first time. |
| 315 | /// |
| 316 | /// We have the initial size to animate from, but we do not have the target |
| 317 | /// size to animate to, so we set both ends to child's size. |
| 318 | void _layoutStart() { |
| 319 | _sizeTween.begin = _sizeTween.end = debugAdoptSize(child!.size); |
| 320 | _state = RenderAnimatedSizeState.stable; |
| 321 | } |
| 322 | |
| 323 | /// At this state we're assuming the child size is stable and letting the |
| 324 | /// animation run its course. |
| 325 | /// |
| 326 | /// If during animation the size of the child changes we restart the |
| 327 | /// animation. |
| 328 | void _layoutStable() { |
| 329 | if (_sizeTween.end != child!.size) { |
| 330 | _sizeTween.begin = size; |
| 331 | _sizeTween.end = debugAdoptSize(child!.size); |
| 332 | _restartAnimation(); |
| 333 | _state = RenderAnimatedSizeState.changed; |
| 334 | } else if (_controller.value == _controller.upperBound) { |
| 335 | // Animation finished. Reset target sizes. |
| 336 | _sizeTween.begin = _sizeTween.end = debugAdoptSize(child!.size); |
| 337 | } else if (!_controller.isAnimating) { |
| 338 | _controller.forward(); // resume the animation after being detached |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /// This state indicates that the size of the child changed once after being |
| 343 | /// considered stable. |
| 344 | /// |
| 345 | /// If the child stabilizes immediately, we go back to stable state. If it |
| 346 | /// changes again, we match the child's size, restart animation and go to |
| 347 | /// unstable state. |
| 348 | void _layoutChanged() { |
| 349 | if (_sizeTween.end != child!.size) { |
| 350 | // Child size changed again. Match the child's size and restart animation. |
| 351 | _sizeTween.begin = _sizeTween.end = debugAdoptSize(child!.size); |
| 352 | _restartAnimation(); |
| 353 | _state = RenderAnimatedSizeState.unstable; |
| 354 | } else { |
| 355 | // Child size stabilized. |
| 356 | _state = RenderAnimatedSizeState.stable; |
| 357 | if (!_controller.isAnimating) { |
| 358 | // Resume the animation after being detached. |
| 359 | _controller.forward(); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /// The child's size is not stable. |
| 365 | /// |
| 366 | /// Continue tracking the child's size until is stabilizes. |
| 367 | void _layoutUnstable() { |
| 368 | if (_sizeTween.end != child!.size) { |
| 369 | // Still unstable. Continue tracking the child. |
| 370 | _sizeTween.begin = _sizeTween.end = debugAdoptSize(child!.size); |
| 371 | _restartAnimation(); |
| 372 | } else { |
| 373 | // Child size stabilized. |
| 374 | _controller.stop(); |
| 375 | _state = RenderAnimatedSizeState.stable; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | void _animationStatusListener(AnimationStatus status) { |
| 380 | if (status.isCompleted) { |
| 381 | _onEnd?.call(); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | @override |
| 386 | void paint(PaintingContext context, Offset offset) { |
| 387 | if (child != null && _hasVisualOverflow && clipBehavior != Clip.none) { |
| 388 | final Rect rect = Offset.zero & size; |
| 389 | _clipRectLayer.layer = context.pushClipRect( |
| 390 | needsCompositing, |
| 391 | offset, |
| 392 | rect, |
| 393 | super.paint, |
| 394 | clipBehavior: clipBehavior, |
| 395 | oldLayer: _clipRectLayer.layer, |
| 396 | ); |
| 397 | } else { |
| 398 | _clipRectLayer.layer = null; |
| 399 | super.paint(context, offset); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | final LayerHandle<ClipRectLayer> _clipRectLayer = LayerHandle<ClipRectLayer>(); |
| 404 | |
| 405 | @override |
| 406 | void dispose() { |
| 407 | _clipRectLayer.layer = null; |
| 408 | _controller.dispose(); |
| 409 | _animation.dispose(); |
| 410 | super.dispose(); |
| 411 | } |
| 412 | } |
| 413 | |