container.dart 13.7 KB
Newer Older
1 2 3 4
// 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.

5
import 'package:flutter/foundation.dart';
6 7 8 9 10 11 12 13 14 15 16 17 18
import 'package:flutter/painting.dart';
import 'package:flutter/rendering.dart';

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

/// A widget that paints a [Decoration] either before or after its child paints.
///
/// [Container] insets its child by the widths of the borders; this widget does
/// not.
///
/// Commonly used with [BoxDecoration].
19 20 21 22 23 24 25 26 27
///
/// ## Sample code
///
/// This sample shows a radial gradient that draws a moon on a night sky:
///
/// ```dart
/// new DecoratedBox(
///   decoration: new BoxDecoration(
///     gradient: new RadialGradient(
28
///       center: const Alignment(-0.5, -0.6),
29 30 31 32 33 34 35 36
///       radius: 0.15,
///       colors: <Color>[
///         const Color(0xFFEEEEEE),
///         const Color(0xFF111133),
///       ],
///       stops: <double>[0.9, 1.0],
///     ),
///   ),
37
/// )
38 39
/// ```
///
40 41
/// See also:
///
42 43 44 45 46
///  * [DecoratedBoxTransition], the version of this class that animates on the
///    [decoration] property.
///  * [Decoration], which you can extend to provide other effects with
///    [DecoratedBox].
///  * [CustomPaint], another way to draw custom effects from the widget layer.
47 48 49 50 51
class DecoratedBox extends SingleChildRenderObjectWidget {
  /// Creates a widget that paints a [Decoration].
  ///
  /// The [decoration] and [position] arguments must not be null. By default the
  /// decoration paints behind the child.
52
  const DecoratedBox({
53 54 55 56
    Key key,
    @required this.decoration,
    this.position: DecorationPosition.background,
    Widget child
57 58 59
  }) : assert(decoration != null),
       assert(position != null),
       super(key: key, child: child);
60 61 62 63 64 65 66 67 68 69 70 71 72 73

  /// What decoration to paint.
  ///
  /// Commonly a [BoxDecoration].
  final Decoration decoration;

  /// Whether to paint the box decoration behind or in front of the child.
  final DecorationPosition position;

  @override
  RenderDecoratedBox createRenderObject(BuildContext context) {
    return new RenderDecoratedBox(
      decoration: decoration,
      position: position,
Ian Hickson's avatar
Ian Hickson committed
74
      configuration: createLocalImageConfiguration(context),
75 76 77 78 79 80 81 82 83 84
    );
  }

  @override
  void updateRenderObject(BuildContext context, RenderDecoratedBox renderObject) {
    renderObject
      ..decoration = decoration
      ..configuration = createLocalImageConfiguration(context)
      ..position = position;
  }
85 86

  @override
87
  void debugFillProperties(DiagnosticPropertiesBuilder description) {
88
    super.debugFillProperties(description);
89 90 91 92 93 94 95 96 97 98 99 100 101
    String label;
    if (position != null) {
      switch (position) {
        case DecorationPosition.background:
          label = 'bg';
          break;
        case DecorationPosition.foreground:
          label = 'fg';
          break;
      }
    } else {
      label = 'decoration';
    }
102
    description.add(new EnumProperty<DecorationPosition>('position', position, level: position != null ? DiagnosticLevel.hidden : DiagnosticLevel.info));
103 104 105 106 107 108
    description.add(new DiagnosticsProperty<Decoration>(
      label,
      decoration,
      ifNull: 'no decoration',
      showName: decoration != null,
    ));
109
  }
110 111 112 113 114 115 116
}

/// A convenience widget that combines common painting, positioning, and sizing
/// widgets.
///
/// A container first surrounds the child with [padding] (inflated by any
/// borders present in the [decoration]) and then applies additional
117
/// [constraints] to the padded extent (incorporating the `width` and `height`
118 119 120 121 122 123 124
/// as constraints, if either is non-null). The container is then surrounded by
/// additional empty space described from the [margin].
///
/// During painting, the container first applies the given [transform], then
/// paints the [decoration] to fill the padded extent, then it paints the child,
/// and finally paints the [foregroundDecoration], also filling the padded
/// extent.
125 126 127 128 129 130
///
/// Containers with no children try to be as big as possible unless the incoming
/// constraints are unbounded, in which case they try to be as small as
/// possible. Containers with children size themselves to their children. The
/// `width`, `height`, and [constraints] arguments to the constructor override
/// this.
Ian Hickson's avatar
Ian Hickson committed
131
///
132 133 134 135 136
/// ## Layout behavior
///
/// _See [BoxConstraints] for an introduction to box layout models._
///
/// Since [Container] combines a number of other widgets each with their own
Ian Hickson's avatar
Ian Hickson committed
137
/// layout behavior, [Container]'s layout behavior is somewhat complicated.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
///
/// tl;dr: [Container] tries, in order: to honor [alignment], to size itself to
/// the [child], to honor the `width`, `height`, and [constraints], to expand to
/// fit the parent, to be as small as possible.
///
/// More specifically:
///
/// If the widget has no child, no `height`, no `width`, no [constraints],
/// and the parent provides unbounded constraints, then [Container] tries to
/// size as small as possible.
///
/// If the widget has no child and no [alignment], but a `height`, `width`, or
/// [constraints] are provided, then the [Container] tries to be as small as
/// possible given the combination of those constraints and the parent's
/// constraints.
///
/// If the widget has no child, no `height`, no `width`, no [constraints], and
/// no [alignment], but the parent provides bounded constraints, then
/// [Container] expands to fit the constraints provided by the parent.
///
/// If the widget has an [alignment], and the parent provides unbounded
/// constraints, then the [Container] tries to size itself around the child.
///
/// If the widget has an [alignment], and the parent provides bounded
/// constraints, then the [Container] tries to expand to fit the parent, and
/// then positions the child within itself as per the [alignment].
///
/// Otherwise, the widget has a [child] but no `height`, no `width`, no
/// [constraints], and no [alignment], and the [Container] passes the
/// constraints from the parent to the child and sizes itself to match the
/// child.
///
/// The [margin] and [padding] properties also affect the layout, as described
/// in the documentation for those properties. (Their effects merely augment the
/// rules described above.) The [decoration] can implicitly increase the
/// [padding] (e.g. borders in a [BoxDecoration] contribute to the [padding]);
/// see [Decoration.padding].
///
Ian Hickson's avatar
Ian Hickson committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
/// ## Sample code
///
/// This example shows a 48x48 green square (placed inside a [Center] widget in
/// case the parent widget has its own opinions regarding the size that the
/// [Container] should take), with a margin so that it stays away from
/// neighboring widgets:
///
/// ```dart
/// new Center(
///   child: new Container(
///     margin: const EdgeInsets.all(10.0),
///     color: const Color(0xFF00FF00),
///     width: 48.0,
///     height: 48.0,
///   ),
/// )
/// ```
///
/// This example shows how to use many of the features of [Container] at once.
/// The [constraints] are set to fit the font size plus ample headroom
/// vertically, while expanding horizontally to fit the parent. The [padding] is
/// used to make sure there is space between the contents and the text. The
198
/// `color` makes the box teal. The [alignment] causes the [child] to be
Ian Hickson's avatar
Ian Hickson committed
199 200 201 202 203 204 205 206 207 208 209
/// centered in the box. The [foregroundDecoration] overlays a nine-patch image
/// onto the text. Finally, the [transform] applies a slight rotation to the
/// entire contraption to complete the effect.
///
/// ```dart
/// new Container(
///   constraints: new BoxConstraints.expand(
///     height: Theme.of(context).textTheme.display1.fontSize * 1.1 + 200.0,
///   ),
///   padding: const EdgeInsets.all(8.0),
///   color: Colors.teal.shade700,
210
///   alignment: Alignment.center,
Ian Hickson's avatar
Ian Hickson committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
///   child: new Text('Hello World', style: Theme.of(context).textTheme.display1.copyWith(color: Colors.white)),
///   foregroundDecoration: new BoxDecoration(
///     image: new DecorationImage(
///       image: new NetworkImage('https://www.example.com/images/frame.png'),
///       centerSlice: new Rect.fromLTRB(270.0, 180.0, 1360.0, 730.0),
///     ),
///   ),
///   transform: new Matrix4.rotationZ(0.1),
/// )
/// ```
///
/// See also:
///
///  * [AnimatedContainer], a variant that smoothly animates the properties when
///    they change.
///  * [Border], which has a sample which uses [Container] heavily.
227 228
class Container extends StatelessWidget {
  /// Creates a widget that combines common painting, positioning, and sizing widgets.
229 230
  ///
  /// The `height` and `width` values include the padding.
231
  ///
Ian Hickson's avatar
Ian Hickson committed
232 233 234 235 236
  /// The `color` argument is a shorthand for `decoration: new
  /// BoxDecoration(color: color)`, which means you cannot supply both a `color`
  /// and a `decoration` argument. If you want to have both a `color` and a
  /// `decoration`, you can pass the color as the `color` argument to the
  /// `BoxDecoration`.
237 238
  Container({
    Key key,
239
    this.alignment,
240
    this.padding,
241 242
    Color color,
    Decoration decoration,
243 244 245 246 247 248
    this.foregroundDecoration,
    double width,
    double height,
    BoxConstraints constraints,
    this.margin,
    this.transform,
249
    this.child,
250 251 252 253 254 255 256 257 258
  }) : assert(margin == null || margin.isNonNegative),
       assert(padding == null || padding.isNonNegative),
       assert(decoration == null || decoration.debugAssertIsValid()),
       assert(constraints == null || constraints.debugAssertIsValid()),
       assert(color == null || decoration == null,
         'Cannot provide both a color and a decoration\n'
         'The color argument is just a shorthand for "decoration: new BoxDecoration(color: color)".'
       ),
       decoration = decoration ?? (color != null ? new BoxDecoration(color: color) : null),
259
       constraints =
260 261 262 263
        (width != null || height != null)
          ? constraints?.tighten(width: width, height: height)
            ?? new BoxConstraints.tightFor(width: width, height: height)
          : constraints,
264
       super(key: key);
265

266
  /// The [child] contained by the container.
267
  ///
268 269 270 271
  /// If null, and if the [constraints] are unbounded or also null, the
  /// container will expand to fill all available space in its parent, unless
  /// the parent provides unbounded constraints, in which case the container
  /// will attempt to be as small as possible.
272 273
  final Widget child;

274
  /// Align the [child] within the container.
275 276
  ///
  /// If non-null, the container will expand to fill its parent and position its
277 278 279 280
  /// child within itself according to the given value. If the incoming
  /// constraints are unbounded, then the child will be shrink-wrapped instead.
  ///
  /// Ignored if [child] is null.
281
  final AlignmentGeometry alignment;
282

283 284
  /// Empty space to inscribe inside the [decoration]. The [child], if any, is
  /// placed inside this padding.
285
  final EdgeInsetsGeometry padding;
286

287
  /// The decoration to paint behind the [child].
288 289 290 291
  ///
  /// A shorthand for specifying just a solid color is available in the
  /// constructor: set the `color` argument instead of the `decoration`
  /// argument.
292 293
  final Decoration decoration;

294
  /// The decoration to paint in front of the [child].
295 296 297
  final Decoration foregroundDecoration;

  /// Additional constraints to apply to the child.
298
  ///
299 300 301
  /// The constructor `width` and `height` arguments are combined with the
  /// `constraints` argument to set this property.
  ///
302
  /// The [padding] goes inside the constraints.
303 304
  final BoxConstraints constraints;

305
  /// Empty space to surround the [decoration] and [child].
306
  final EdgeInsetsGeometry margin;
307 308 309 310

  /// The transformation matrix to apply before painting the container.
  final Matrix4 transform;

311
  EdgeInsetsGeometry get _paddingIncludingDecoration {
312 313
    if (decoration == null || decoration.padding == null)
      return padding;
314
    final EdgeInsetsGeometry decorationPadding = decoration.padding;
315 316
    if (padding == null)
      return decorationPadding;
317
    return padding.add(decorationPadding);
318 319 320 321 322 323
  }

  @override
  Widget build(BuildContext context) {
    Widget current = child;

324 325 326 327 328 329 330
    if (child == null && (constraints == null || !constraints.isTight)) {
      current = new LimitedBox(
        maxWidth: 0.0,
        maxHeight: 0.0,
        child: new ConstrainedBox(constraints: const BoxConstraints.expand())
      );
    }
331

332 333 334
    if (alignment != null)
      current = new Align(alignment: alignment, child: current);

335
    final EdgeInsetsGeometry effectivePadding = _paddingIncludingDecoration;
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
    if (effectivePadding != null)
      current = new Padding(padding: effectivePadding, child: current);

    if (decoration != null)
      current = new DecoratedBox(decoration: decoration, child: current);

    if (foregroundDecoration != null) {
      current = new DecoratedBox(
        decoration: foregroundDecoration,
        position: DecorationPosition.foreground,
        child: current
      );
    }

    if (constraints != null)
      current = new ConstrainedBox(constraints: constraints, child: current);

    if (margin != null)
      current = new Padding(padding: margin, child: current);

    if (transform != null)
      current = new Transform(transform: transform, child: current);

    return current;
  }

  @override
363
  void debugFillProperties(DiagnosticPropertiesBuilder description) {
364
    super.debugFillProperties(description);
365
    description.add(new DiagnosticsProperty<AlignmentGeometry>('alignment', alignment, showName: false, defaultValue: null));
366
    description.add(new DiagnosticsProperty<EdgeInsetsGeometry>('padding', padding, defaultValue: null));
367 368 369
    description.add(new DiagnosticsProperty<Decoration>('bg', decoration, defaultValue: null));
    description.add(new DiagnosticsProperty<Decoration>('fg', foregroundDecoration, defaultValue: null));
    description.add(new DiagnosticsProperty<BoxConstraints>('constraints', constraints, defaultValue: null));
370
    description.add(new DiagnosticsProperty<EdgeInsetsGeometry>('margin', margin, defaultValue: null));
371
    description.add(new ObjectFlagProperty<Matrix4>.has('transform', transform));
372 373
  }
}