Commit 80d578d9 authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Make `new Container()` more cooperative. (#6474)

If a Container with no child is in an infinite space, make it shrink
instead of expanding. Still expand otherwise.
parent 0838ef7d
...@@ -70,6 +70,12 @@ class DecoratedBox extends SingleChildRenderObjectWidget { ...@@ -70,6 +70,12 @@ class DecoratedBox extends SingleChildRenderObjectWidget {
/// paints the [decoration] to fill the padded extent, then it paints the child, /// paints the [decoration] to fill the padded extent, then it paints the child,
/// and finally paints the [foregroundDecoration], also filling the padded /// and finally paints the [foregroundDecoration], also filling the padded
/// extent. /// extent.
///
/// 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.
class Container extends StatelessWidget { class Container extends StatelessWidget {
/// Creates a widget that combines common painting, positioning, and sizing widgets. /// Creates a widget that combines common painting, positioning, and sizing widgets.
/// ///
...@@ -98,32 +104,42 @@ class Container extends StatelessWidget { ...@@ -98,32 +104,42 @@ class Container extends StatelessWidget {
assert(constraints == null || constraints.debugAssertIsValid()); assert(constraints == null || constraints.debugAssertIsValid());
} }
/// The child contained by the container. /// The [child] contained by the container.
/// ///
/// If null, the container will expand to fill all available space in its parent. /// 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.
final Widget child; final Widget child;
/// Align the child within the container. /// Align the [child] within the container.
/// ///
/// If non-null, the container will expand to fill its parent and position its /// If non-null, the container will expand to fill its parent and position its
/// child within itself according to the given value. /// 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.
final FractionalOffset alignment; final FractionalOffset alignment;
/// Empty space to inscribe inside the decoration. /// Empty space to inscribe inside the [decoration]. The [child], if any, is
/// placed inside this padding.
final EdgeInsets padding; final EdgeInsets padding;
/// The decoration to paint behind the child. /// The decoration to paint behind the [child].
final Decoration decoration; final Decoration decoration;
/// The decoration to paint in front of the child. /// The decoration to paint in front of the [child].
final Decoration foregroundDecoration; final Decoration foregroundDecoration;
/// Additional constraints to apply to the child. /// Additional constraints to apply to the child.
/// ///
/// The constructor `width` and `height` arguments are combined with the
/// `constraints` argument to set this property.
///
/// The [padding] goes inside the constraints. /// The [padding] goes inside the constraints.
final BoxConstraints constraints; final BoxConstraints constraints;
/// Empty space to surround the decoration. /// Empty space to surround the [decoration] and [child].
final EdgeInsets margin; final EdgeInsets margin;
/// The transformation matrix to apply before painting the container. /// The transformation matrix to apply before painting the container.
...@@ -142,8 +158,13 @@ class Container extends StatelessWidget { ...@@ -142,8 +158,13 @@ class Container extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
Widget current = child; Widget current = child;
if (child == null && (constraints == null || !constraints.isTight)) if (child == null && (constraints == null || !constraints.isTight)) {
current = new ConstrainedBox(constraints: const BoxConstraints.expand()); current = new LimitedBox(
maxWidth: 0.0,
maxHeight: 0.0,
child: new ConstrainedBox(constraints: const BoxConstraints.expand())
);
}
if (alignment != null) if (alignment != null)
current = new Align(alignment: alignment, child: current); current = new Align(alignment: alignment, child: current);
...@@ -180,6 +201,8 @@ class Container extends StatelessWidget { ...@@ -180,6 +201,8 @@ class Container extends StatelessWidget {
super.debugFillDescription(description); super.debugFillDescription(description);
if (constraints != null) if (constraints != null)
description.add('$constraints'); description.add('$constraints');
if (alignment != null)
description.add('$alignment');
if (decoration != null) if (decoration != null)
description.add('bg: $decoration'); description.add('bg: $decoration');
if (foregroundDecoration != null) if (foregroundDecoration != null)
......
// Copyright 2015 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_test/flutter_test.dart';
import 'package:flutter/widgets.dart';
void main() {
testWidgets('Can be placed in an infinite box', (WidgetTester tester) async {
await tester.pumpWidget(new Block(children: <Widget>[new Container()]));
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment