diff --git a/packages/flutter/lib/src/widgets/basic.dart b/packages/flutter/lib/src/widgets/basic.dart index 98393fe37c..140f92f604 100644 --- a/packages/flutter/lib/src/widgets/basic.dart +++ b/packages/flutter/lib/src/widgets/basic.dart @@ -820,15 +820,24 @@ class CustomMultiChildLayout extends MultiChildRenderObjectWidget { /// sizes itself to fit the parent. It is equivalent to setting [width] and /// [height] to [double.INFINITY]. class SizedBox extends SingleChildRenderObjectWidget { - /// Creates a box of a specific size. + /// Creates a fixed size box. The [width] and [height] parameters can be null + /// to indicate that the size of the box should not be constrained in + /// the corresponding dimension. const SizedBox({ Key key, this.width, this.height, Widget child }) : super(key: key, child: child); + /// Creates a box that will become as large as its parent allows. const SizedBox.expand({ Key key, Widget child }) : width = double.INFINITY, height = double.INFINITY, super(key: key, child: child); + /// Creates a box with the specified size. + SizedBox.fromSize({ Key key, Widget child, Size size }) + : width = size?.width, + height = size?.height, + super(key: key, child: child); + /// If non-null, requires the child to have exactly this width. final double width; diff --git a/packages/flutter/test/widgets/sized_box_test.dart b/packages/flutter/test/widgets/sized_box_test.dart index d1b37075f9..82cc6d7db0 100644 --- a/packages/flutter/test/widgets/sized_box_test.dart +++ b/packages/flutter/test/widgets/sized_box_test.dart @@ -6,6 +6,32 @@ import 'package:flutter/widgets.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { + testWidgets('SizedBox constructors', (WidgetTester tester) async { + final SizedBox a = const SizedBox(); + expect(a.width, isNull); + expect(a.height, isNull); + + final SizedBox b = const SizedBox(width: 10.0); + expect(b.width, 10.0); + expect(b.height, isNull); + + final SizedBox c = const SizedBox(width: 10.0, height: 20.0); + expect(c.width, 10.0); + expect(c.height, 20.0); + + final SizedBox d = new SizedBox.fromSize(); + expect(d.width, isNull); + expect(d.height, isNull); + + final SizedBox e = new SizedBox.fromSize(size: const Size(1.0, 2.0)); + expect(e.width, 1.0); + expect(e.height, 2.0); + + final SizedBox f = const SizedBox.expand(); + expect(f.width, double.INFINITY); + expect(f.height, double.INFINITY); + }); + testWidgets('SizedBox - no child', (WidgetTester tester) async { GlobalKey patient = new GlobalKey();