animated_size.dart 2.59 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 13 14 15 16

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

/// Animated widget that automatically transitions its size over a given
/// duration whenever the given child's size changes.
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.
17
  const AnimatedSize({
18 19
    Key key,
    Widget child,
20
    this.alignment: Alignment.center,
21
    this.curve: Curves.linear,
22 23
    @required this.duration,
    @required this.vsync,
24 25 26 27 28 29
  }) : 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
30
  /// alignment, respectively. An x value of -1.0 means that the left edge of
31 32 33
  /// 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.
34
  /// For example, a value of 0.0 means that the center of the child is aligned
35
  /// with the center of the parent.
36 37 38 39 40 41 42 43 44
  ///
  /// 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.
45
  final AlignmentGeometry alignment;
46 47 48 49 50 51 52 53 54

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

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

58 59 60 61 62
  @override
  RenderAnimatedSize createRenderObject(BuildContext context) {
    return new RenderAnimatedSize(
      alignment: alignment,
      duration: duration,
63 64
      curve: curve,
      vsync: vsync,
65
      textDirection: Directionality.of(context),
66 67 68 69
    );
  }

  @override
70
  void updateRenderObject(BuildContext context, RenderAnimatedSize renderObject) {
71 72 73
    renderObject
      ..alignment = alignment
      ..duration = duration
74
      ..curve = curve
75 76
      ..vsync = vsync
      ..textDirection = Directionality.of(context);
77 78
  }
}