animated_size.dart 4.68 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// 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';
6
import 'package:flutter/scheduler.dart';
7 8 9 10 11 12

import 'basic.dart';
import 'framework.dart';

/// 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 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/// {@tool dartpad --template=stateful_widget_scaffold_center_freeform_state}
/// This example makes a [Container] react to being touched, causing the child
/// of the [AnimatedSize] widget, here a [FlutterLogo], to animate.
///
/// ```dart
/// class _MyStatefulWidgetState extends State<MyStatefulWidget> with SingleTickerProviderStateMixin {
///   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,
///           vsync: this,
///           duration: Duration(seconds: 1),
///           child: FlutterLogo(size: _size),
///         ),
///       ),
///     );
///   }
/// }
/// ```
/// {@end-tool}
///
Ian Hickson's avatar
Ian Hickson committed
49 50 51
/// See also:
///
///  * [SizeTransition], which changes its size based on an [Animation].
52 53 54 55
class AnimatedSize extends SingleChildRenderObjectWidget {
  /// Creates a widget that animates its size to match that of its child.
  ///
  /// The [curve] and [duration] arguments must not be null.
56
  const AnimatedSize({
57 58
    Key? key,
    Widget? child,
59 60
    this.alignment = Alignment.center,
    this.curve = Curves.linear,
61
    required this.duration,
62
    this.reverseDuration,
63
    required this.vsync,
64 65 66
    this.clipBehavior = Clip.hardEdge,
  }) : assert(clipBehavior != null),
       super(key: key, child: child);
67 68 69 70 71

  /// 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
72
  /// alignment, respectively. An x value of -1.0 means that the left edge of
73 74 75
  /// 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.
76
  /// For example, a value of 0.0 means that the center of the child is aligned
77
  /// with the center of the parent.
78 79 80 81 82 83 84 85 86
  ///
  /// 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.
87
  final AlignmentGeometry alignment;
88 89 90 91 92 93 94 95 96

  /// 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;

97 98 99 100
  /// The duration when transitioning this widget's size to match the child's
  /// size when going in reverse.
  ///
  /// If not specified, defaults to [duration].
101
  final Duration? reverseDuration;
102

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

106 107 108 109 110
  /// {@macro flutter.widgets.Clip}
  ///
  /// Defaults to [Clip.hardEdge], and must not be null.
  final Clip clipBehavior;

111 112
  @override
  RenderAnimatedSize createRenderObject(BuildContext context) {
113
    return RenderAnimatedSize(
114 115
      alignment: alignment,
      duration: duration,
116
      reverseDuration: reverseDuration,
117 118
      curve: curve,
      vsync: vsync,
119
      textDirection: Directionality.of(context),
120
      clipBehavior: clipBehavior,
121 122 123 124
    );
  }

  @override
125
  void updateRenderObject(BuildContext context, RenderAnimatedSize renderObject) {
126 127 128
    renderObject
      ..alignment = alignment
      ..duration = duration
129
      ..reverseDuration = reverseDuration
130
      ..curve = curve
131
      ..vsync = vsync
132 133
      ..textDirection = Directionality.of(context)
      ..clipBehavior = clipBehavior;
134
  }
135 136 137 138 139 140 141 142

  @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));
  }
143
}