animated_size.dart 5.85 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/rendering.dart';

import 'basic.dart';
import 'framework.dart';
9
import 'ticker_provider.dart';
10 11 12

/// Animated widget that automatically transitions its size over a given
/// duration whenever the given child's size changes.
Ian Hickson's avatar
Ian Hickson committed
13
///
14
/// {@tool dartpad --template=stateful_widget_scaffold_center_freeform_state}
15 16 17 18
/// This example makes a [Container] react to being touched, causing the child
/// of the [AnimatedSize] widget, here a [FlutterLogo], to animate.
///
/// ```dart
19
/// class _MyStatefulWidgetState extends State<MyStatefulWidget> {
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
///   double _size = 50.0;
///   bool _large = false;
///
///   void _updateSize() {
///     setState(() {
///       _size = _large ? 250.0 : 100.0;
///       _large = !_large;
///     });
///   }
///
///   @override
///   Widget build(BuildContext context) {
///     return GestureDetector(
///       onTap: () => _updateSize(),
///       child: Container(
///         color: Colors.amberAccent,
///         child: AnimatedSize(
///           curve: Curves.easeIn,
38
///           duration: const Duration(seconds: 1),
39 40 41 42 43 44 45 46 47
///           child: FlutterLogo(size: _size),
///         ),
///       ),
///     );
///   }
/// }
/// ```
/// {@end-tool}
///
Ian Hickson's avatar
Ian Hickson committed
48 49 50
/// See also:
///
///  * [SizeTransition], which changes its size based on an [Animation].
51
class AnimatedSize extends StatefulWidget {
52 53 54
  /// Creates a widget that animates its size to match that of its child.
  ///
  /// The [curve] and [duration] arguments must not be null.
55
  const AnimatedSize({
56
    Key? key,
57
    this.child,
58 59
    this.alignment = Alignment.center,
    this.curve = Curves.linear,
60
    required this.duration,
61
    this.reverseDuration,
62 63 64 65 66
    @Deprecated(
      'This field is now ignored. '
      'This feature was deprecated after v2.2.0-10.1.pre.'
    )
    TickerProvider? vsync,
67 68
    this.clipBehavior = Clip.hardEdge,
  }) : assert(clipBehavior != null),
69 70 71 72 73 74
       super(key: key);

  /// The widget below this widget in the tree.
  ///
  /// {@macro flutter.widgets.ProxyWidget.child}
  final Widget? child;
75 76 77 78 79

  /// The alignment of the child within the parent when the parent is not yet
  /// the same size as the child.
  ///
  /// The x and y values of the alignment control the horizontal and vertical
80
  /// alignment, respectively. An x value of -1.0 means that the left edge of
81 82 83
  /// the child is aligned with the left edge of the parent whereas an x value
  /// of 1.0 means that the right edge of the child is aligned with the right
  /// edge of the parent. Other values interpolate (and extrapolate) linearly.
84
  /// For example, a value of 0.0 means that the center of the child is aligned
85
  /// with the center of the parent.
86 87 88 89 90 91 92 93 94
  ///
  /// Defaults to [Alignment.center].
  ///
  /// See also:
  ///
  ///  * [Alignment], a class with convenient constants typically used to
  ///    specify an [AlignmentGeometry].
  ///  * [AlignmentDirectional], like [Alignment] for specifying alignments
  ///    relative to text direction.
95
  final AlignmentGeometry alignment;
96 97 98 99 100 101 102 103 104

  /// The animation curve when transitioning this widget's size to match the
  /// child's size.
  final Curve curve;

  /// The duration when transitioning this widget's size to match the child's
  /// size.
  final Duration duration;

105 106 107 108
  /// The duration when transitioning this widget's size to match the child's
  /// size when going in reverse.
  ///
  /// If not specified, defaults to [duration].
109
  final Duration? reverseDuration;
110

111
  /// {@macro flutter.material.Material.clipBehavior}
112 113 114 115
  ///
  /// Defaults to [Clip.hardEdge], and must not be null.
  final Clip clipBehavior;

116
  @override
117
  State<AnimatedSize> createState() => _AnimatedSizeState();
118 119 120 121 122 123 124 125 126 127 128 129 130
}

class _AnimatedSizeState
    extends State<AnimatedSize> with SingleTickerProviderStateMixin {
  @override
  Widget build(BuildContext context) {
    return _AnimatedSize(
      alignment: widget.alignment,
      curve: widget.curve,
      duration: widget.duration,
      reverseDuration: widget.reverseDuration,
      vsync: this,
      clipBehavior: widget.clipBehavior,
131
      child: widget.child,
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    );
  }
}

class _AnimatedSize extends SingleChildRenderObjectWidget {
  const _AnimatedSize({
    Key? key,
    Widget? child,
    this.alignment = Alignment.center,
    this.curve = Curves.linear,
    required this.duration,
    this.reverseDuration,
    required this.vsync,
    this.clipBehavior = Clip.hardEdge,
  }) : assert(clipBehavior != null),
       super(key: key, child: child);

  final AlignmentGeometry alignment;
  final Curve curve;
  final Duration duration;
  final Duration? reverseDuration;

  /// The [TickerProvider] for this widget.
  final TickerProvider vsync;

  final Clip clipBehavior;

159 160
  @override
  RenderAnimatedSize createRenderObject(BuildContext context) {
161
    return RenderAnimatedSize(
162 163
      alignment: alignment,
      duration: duration,
164
      reverseDuration: reverseDuration,
165 166
      curve: curve,
      vsync: vsync,
167
      textDirection: Directionality.maybeOf(context),
168
      clipBehavior: clipBehavior,
169 170 171 172
    );
  }

  @override
173
  void updateRenderObject(BuildContext context, RenderAnimatedSize renderObject) {
174 175 176
    renderObject
      ..alignment = alignment
      ..duration = duration
177
      ..reverseDuration = reverseDuration
178
      ..curve = curve
179
      ..vsync = vsync
180
      ..textDirection = Directionality.maybeOf(context)
181
      ..clipBehavior = clipBehavior;
182
  }
183 184 185 186 187 188 189 190

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(DiagnosticsProperty<AlignmentGeometry>('alignment', alignment, defaultValue: Alignment.topCenter));
    properties.add(IntProperty('duration', duration.inMilliseconds, unit: 'ms'));
    properties.add(IntProperty('reverseDuration', reverseDuration?.inMilliseconds, unit: 'ms', defaultValue: null));
  }
191
}