From e7d5c7d6b771ad5772f733335790ba7877402cb8 Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Thu, 3 Nov 2016 14:44:11 -0700 Subject: [PATCH] Change 'body' to be loosely constrained. (#6692) People get confused by the tight constraints. --- .../flutter/lib/src/material/scaffold.dart | 25 ++++++------ .../flutter/test/material/scaffold_test.dart | 38 +++++++++++++++++++ 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/packages/flutter/lib/src/material/scaffold.dart b/packages/flutter/lib/src/material/scaffold.dart index 02ff65bad4..7fefd48ca8 100644 --- a/packages/flutter/lib/src/material/scaffold.dart +++ b/packages/flutter/lib/src/material/scaffold.dart @@ -79,7 +79,7 @@ class _ScaffoldLayout extends MultiChildLayoutDelegate { // This part of the layout has the same effect as putting the app bar and // body in a column and making the body flexible. What's different is that - // in this case the app bar appears -after- the body in the stacking order, + // in this case the app bar appears _after_ the body in the stacking order, // so the app bar's shadow is drawn on top of the body. final BoxConstraints fullWidthConstraints = looseConstraints.tighten(width: size.width); @@ -108,7 +108,10 @@ class _ScaffoldLayout extends MultiChildLayoutDelegate { if (hasChild(_ScaffoldSlot.body)) { final double bodyHeight = contentBottom - contentTop; - final BoxConstraints bodyConstraints = fullWidthConstraints.tighten(height: bodyHeight); + final BoxConstraints bodyConstraints = new BoxConstraints( + maxWidth: fullWidthConstraints.maxWidth, + maxHeight: bodyHeight, + ); layoutChild(_ScaffoldSlot.body, bodyConstraints); positionChild(_ScaffoldSlot.body, new Offset(0.0, contentTop)); } @@ -325,22 +328,20 @@ class Scaffold extends StatefulWidget { /// [drawer]. To avoid the body being resized to avoid the window padding /// (e.g., from the onscreen keyboard), see [resizeToAvoidBottomPadding]. /// - /// The widget in the body of the scaffold will be forced to the size of the - /// available space, to cover the entire scaffold other than any app bars, - /// footer buttons, or navigation bars. - /// - /// To center this widget instead, consider putting it in a [Center] widget - /// and having that be the body. + /// The widget in the body of the scaffold is positioned at the top-left of + /// the available space between the app bar and the bottom of the scaffold. To + /// center this widget instead, consider putting it in a [Center] widget and + /// having that be the body. /// /// If you have a column of widgets that should normally fit on the screen, /// but may overflow and would in such cases need to scroll, consider using a /// [Block] as the body of the scaffold. /// - /// If you have a list of items, consider using a [LazyBlock] or - /// [LazyScrollableList] as the body of the scaffold. + /// If you have a list of items, consider using a [LazyBlock], + /// [LazyScrollableList], or [MaterialList] as the body of the scaffold. final Widget body; - /// A button displayed on top of the body. + /// A button displayed on top of the [body]. /// /// Typically a [FloatingActionButton]. final Widget floatingActionButton; @@ -357,7 +358,7 @@ class Scaffold extends StatefulWidget { /// * final List persistentFooterButtons; - /// A panel displayed to the side of the body, often hidden on mobile devices. + /// A panel displayed to the side of the [body], often hidden on mobile devices. /// /// Typically a [Drawer]. final Widget drawer; diff --git a/packages/flutter/test/material/scaffold_test.dart b/packages/flutter/test/material/scaffold_test.dart index 34fbc8848d..cc31c6d4e8 100644 --- a/packages/flutter/test/material/scaffold_test.dart +++ b/packages/flutter/test/material/scaffold_test.dart @@ -305,4 +305,42 @@ void main() { await expectBackIcon(tester, TargetPlatform.iOS, Icons.arrow_back_ios); }); }); + + group('body size', () { + testWidgets('body size with container', (WidgetTester tester) async { + Key testKey = new UniqueKey(); + await tester.pumpWidget( + new Scaffold(body: new Container(key: testKey)) + ); + expect(tester.element(find.byKey(testKey)).size, const Size(800.0, 600.0)); + expect(tester.renderObject/**/(find.byKey(testKey)).localToGlobal(Point.origin), const Point(0.0, 0.0)); + }); + + testWidgets('body size with sized container', (WidgetTester tester) async { + Key testKey = new UniqueKey(); + await tester.pumpWidget( + new Scaffold(body: new Container(key: testKey, height: 100.0)) + ); + expect(tester.element(find.byKey(testKey)).size, const Size(800.0, 100.0)); + expect(tester.renderObject/**/(find.byKey(testKey)).localToGlobal(Point.origin), const Point(0.0, 0.0)); + }); + + testWidgets('body size with centered container', (WidgetTester tester) async { + Key testKey = new UniqueKey(); + await tester.pumpWidget( + new Scaffold(body: new Center(child: new Container(key: testKey))) + ); + expect(tester.element(find.byKey(testKey)).size, const Size(800.0, 600.0)); + expect(tester.renderObject/**/(find.byKey(testKey)).localToGlobal(Point.origin), const Point(0.0, 0.0)); + }); + + testWidgets('body size with button', (WidgetTester tester) async { + Key testKey = new UniqueKey(); + await tester.pumpWidget( + new Scaffold(body: new FlatButton(key: testKey, onPressed: () { }, child: new Text(''))) + ); + expect(tester.element(find.byKey(testKey)).size, const Size(88.0, 36.0)); + expect(tester.renderObject/**/(find.byKey(testKey)).localToGlobal(Point.origin), const Point(0.0, 0.0)); + }); + }); }