From 34a6e48ac82c4fe53e2bb816761118f06a55f488 Mon Sep 17 00:00:00 2001 From: xster Date: Wed, 22 Feb 2017 14:53:53 -0800 Subject: [PATCH] =?UTF-8?q?16=20dp=20title=20margin=20when=20there?= =?UTF-8?q?=E2=80=99s=20no=20leading=20button=20(#8344)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flutter/lib/src/material/app_bar.dart | 13 +-- .../flutter/test/material/app_bar_test.dart | 85 ++++++++++++------- 2 files changed, 60 insertions(+), 38 deletions(-) diff --git a/packages/flutter/lib/src/material/app_bar.dart b/packages/flutter/lib/src/material/app_bar.dart index 3b29f5c49a..bd11d96501 100644 --- a/packages/flutter/lib/src/material/app_bar.dart +++ b/packages/flutter/lib/src/material/app_bar.dart @@ -47,7 +47,8 @@ class _ToolbarLayout extends MultiChildLayoutDelegate { final bool centerTitle; static const double kLeadingWidth = 56.0; // So it's square with kToolbarHeight. - static const double kTitleLeft = 72.0; // As per https://material.io/guidelines/layout/metrics-keylines.html#metrics-keylines-keylines-spacing. + static const double kTitleLeftWithLeading = 72.0; // As per https://material.io/guidelines/layout/metrics-keylines.html#metrics-keylines-keylines-spacing. + static const double kTitleLeftWithoutLeading = 16.0; @override void performLayout(Size size) { @@ -69,11 +70,13 @@ class _ToolbarLayout extends MultiChildLayoutDelegate { } if (hasChild(_ToolbarSlot.title)) { - final double maxWidth = math.max(size.width - kTitleLeft - actionsWidth, 0.0); + final double titleLeftMargin = + hasChild(_ToolbarSlot.leading) ? kTitleLeftWithLeading : kTitleLeftWithoutLeading; + final double maxWidth = math.max(size.width - titleLeftMargin - actionsWidth, 0.0); final BoxConstraints constraints = new BoxConstraints.loose(size).copyWith(maxWidth: maxWidth); final Size titleSize = layoutChild(_ToolbarSlot.title, constraints); final double titleY = (size.height - titleSize.height) / 2.0; - double titleX = kTitleLeft; + double titleX = titleLeftMargin; // If the centered title will not fit between the leading and actions // widgets, then align its left or right edge with the adjacent boundary. @@ -81,8 +84,8 @@ class _ToolbarLayout extends MultiChildLayoutDelegate { titleX = (size.width - titleSize.width) / 2.0; if (titleX + titleSize.width > size.width - actionsWidth) titleX = size.width - actionsWidth - titleSize.width; - else if (titleX < kTitleLeft) - titleX = kTitleLeft; + else if (titleX < titleLeftMargin) + titleX = titleLeftMargin; } positionChild(_ToolbarSlot.title, new Offset(titleX, titleY)); diff --git a/packages/flutter/test/material/app_bar_test.dart b/packages/flutter/test/material/app_bar_test.dart index 0d99a92b84..800f016fd0 100644 --- a/packages/flutter/test/material/app_bar_test.dart +++ b/packages/flutter/test/material/app_bar_test.dart @@ -13,10 +13,10 @@ void main() { theme: new ThemeData(platform: TargetPlatform.android), home: new Scaffold( appBar: new AppBar( - title: new Text('X') - ) - ) - ) + title: new Text('X'), + ), + ), + ), ); Finder title = find.text('X'); @@ -32,10 +32,10 @@ void main() { theme: new ThemeData(platform: TargetPlatform.iOS), home: new Scaffold( appBar: new AppBar( - title: new Text('X') - ) - ) - ) + title: new Text('X'), + ), + ), + ), ); center = tester.getCenter(title); @@ -51,7 +51,7 @@ void main() { home: new Scaffold( appBar: new AppBar( centerTitle: true, - title: new Text('X') + title: new Text('X'), ) ) ) @@ -65,16 +65,35 @@ void main() { expect(center.x, lessThan(400 + size.width / 2.0)); }); - testWidgets('AppBar centerTitle:false title left edge is 72.0 ', (WidgetTester tester) async { + testWidgets('AppBar centerTitle:false title left edge is 16.0 ', (WidgetTester tester) async { await tester.pumpWidget( new MaterialApp( home: new Scaffold( appBar: new AppBar( centerTitle: false, - title: new Text('X') - ) - ) - ) + title: new Text('X'), + ), + ), + ), + ); + + expect(tester.getTopLeft(find.text('X')).x, 16.0); + }); + + testWidgets( + 'AppBar centerTitle:false leading button title left edge is 72.0 ', + (WidgetTester tester) async { + await tester.pumpWidget( + new MaterialApp( + home: new Scaffold( + appBar: new AppBar( + centerTitle: false, + title: new Text('X'), + ), + // A drawer causes a leading hamburger. + drawer: new Drawer(), + ), + ), ); expect(tester.getTopLeft(find.text('X')).x, 72.0); @@ -85,7 +104,7 @@ void main() { // between the leading and actions widgets. Key titleKey = new UniqueKey(); - Widget leading; + Widget leading = new Container(); List actions; Widget buildApp() { @@ -96,11 +115,11 @@ void main() { centerTitle: false, title: new Container( key: titleKey, - constraints: new BoxConstraints.loose(const Size(1000.0, 1000.0)) + constraints: new BoxConstraints.loose(const Size(1000.0, 1000.0)), ), - actions: actions - ) - ) + actions: actions, + ), + ), ); } @@ -146,11 +165,11 @@ void main() { centerTitle: true, title: new Container( key: titleKey, - constraints: new BoxConstraints.loose(new Size(titleWidth, 1000.0)) + constraints: new BoxConstraints.loose(new Size(titleWidth, 1000.0)), ), - actions: actions - ) - ) + actions: actions, + ), + ), ); } @@ -188,11 +207,11 @@ void main() { width: 0.0, child: new Scaffold( appBar: new AppBar( - title: new Text('X') - ) - ) - ) - ) + title: new Text('X'), + ), + ), + ), + ), ); Finder title = find.text('X'); @@ -219,7 +238,7 @@ void main() { ], ), ), - ) + ), ); // The vertical center of the widget with key, in global coordinates. @@ -240,8 +259,8 @@ void main() { title: new Text('X'), ), drawer: new Column(), // Doesn't really matter. Triggers a hamburger regardless. - ) - ) + ), + ), ); Finder hamburger = find.byTooltip('Open navigation menu'); @@ -271,8 +290,8 @@ void main() { ), ], ), - ) - ) + ), + ), ); Finder addButton = find.byTooltip('Add');