animated_size.dart 3.42 KB
Newer Older
1 2 3 4 5
// Copyright 2016 The Chromium Authors. All rights reserved.
// 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
///
/// See also:
///
///  * [SizeTransition], which changes its size based on an [Animation].
17 18 19 20
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.
21
  const AnimatedSize({
22 23
    Key key,
    Widget child,
24 25
    this.alignment = Alignment.center,
    this.curve = Curves.linear,
26
    @required this.duration,
27
    this.reverseDuration,
28
    @required this.vsync,
29 30 31 32 33 34
  }) : super(key: key, child: child);

  /// 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
35
  /// alignment, respectively. An x value of -1.0 means that the left edge of
36 37 38
  /// 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.
39
  /// For example, a value of 0.0 means that the center of the child is aligned
40
  /// with the center of the parent.
41 42 43 44 45 46 47 48 49
  ///
  /// 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.
50
  final AlignmentGeometry alignment;
51 52 53 54 55 56 57 58 59

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

60 61 62 63 64 65
  /// The duration when transitioning this widget's size to match the child's
  /// size when going in reverse.
  ///
  /// If not specified, defaults to [duration].
  final Duration reverseDuration;

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

69 70
  @override
  RenderAnimatedSize createRenderObject(BuildContext context) {
71
    return RenderAnimatedSize(
72 73
      alignment: alignment,
      duration: duration,
74
      reverseDuration: reverseDuration,
75 76
      curve: curve,
      vsync: vsync,
77
      textDirection: Directionality.of(context),
78 79 80 81
    );
  }

  @override
82
  void updateRenderObject(BuildContext context, RenderAnimatedSize renderObject) {
83 84 85
    renderObject
      ..alignment = alignment
      ..duration = duration
86
      ..reverseDuration = reverseDuration
87
      ..curve = curve
88 89
      ..vsync = vsync
      ..textDirection = Directionality.of(context);
90
  }
91 92 93 94 95 96 97 98

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