From 625d96a1c6f99526bf5382461afe3fad61374a67 Mon Sep 17 00:00:00 2001 From: Qun Cheng <36861262+QuncCccccc@users.noreply.github.com> Date: Thu, 11 Aug 2022 15:29:19 -0700 Subject: [PATCH] Revert "Set `IconButton.visualDensity` default to `VisualDensity.standard` (#109349)" (#109421) This reverts commit 3070baf17581b50b6b9327fd2b682a6742db04e0. --- .../lib/icon_button_template.dart | 2 +- .../material/icon_button/icon_button.3.dart | 4 ++ .../api/test/widgets/heroes/hero.0_test.dart | 20 +++--- .../api/test/widgets/heroes/hero.1_test.dart | 36 +++++----- .../flutter/lib/src/material/app_bar.dart | 4 +- .../flutter/lib/src/material/icon_button.dart | 12 +--- .../flutter/lib/src/material/theme_data.dart | 5 -- .../flutter/test/material/app_bar_test.dart | 12 ++-- .../test/material/icon_button_test.dart | 68 +------------------ .../test/material/popup_menu_test.dart | 7 +- 10 files changed, 49 insertions(+), 121 deletions(-) diff --git a/dev/tools/gen_defaults/lib/icon_button_template.dart b/dev/tools/gen_defaults/lib/icon_button_template.dart index 10a2009c9f..0e8a9b5dc8 100644 --- a/dev/tools/gen_defaults/lib/icon_button_template.dart +++ b/dev/tools/gen_defaults/lib/icon_button_template.dart @@ -108,7 +108,7 @@ class _${blockName}DefaultsM3 extends ButtonStyle { }); @override - VisualDensity? get visualDensity => VisualDensity.standard; + VisualDensity? get visualDensity => Theme.of(context).visualDensity; @override MaterialTapTargetSize? get tapTargetSize => Theme.of(context).materialTapTargetSize; diff --git a/examples/api/lib/material/icon_button/icon_button.3.dart b/examples/api/lib/material/icon_button/icon_button.3.dart index 1e819a4a71..230d29260e 100644 --- a/examples/api/lib/material/icon_button/icon_button.3.dart +++ b/examples/api/lib/material/icon_button/icon_button.3.dart @@ -19,6 +19,10 @@ class IconButtonToggleApp extends StatelessWidget { theme: ThemeData( colorSchemeSeed: const Color(0xff6750a4), useMaterial3: true, + // Desktop and web platforms have a compact visual density by default. + // To see buttons with circular background on desktop/web, the "visualDensity" + // needs to be set to "VisualDensity.standard". + visualDensity: VisualDensity.standard, ), title: 'Icon Button Types', home: const Scaffold( diff --git a/examples/api/test/widgets/heroes/hero.0_test.dart b/examples/api/test/widgets/heroes/hero.0_test.dart index 0cea769220..058436ddb0 100644 --- a/examples/api/test/widgets/heroes/hero.0_test.dart +++ b/examples/api/test/widgets/heroes/hero.0_test.dart @@ -13,32 +13,32 @@ void main() { ); expect(find.text('Hero Sample'), findsOneWidget); - await tester.tap(find.byType(example.BoxWidget)); + await tester.tap(find.byType(Container)); await tester.pump(); - Size heroSize = tester.getSize(find.byType(example.BoxWidget)); + Size heroSize = tester.getSize(find.byType(Container)); // Jump 25% into the transition (total length = 300ms) await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget)); + heroSize = tester.getSize(find.byType(Container)); expect(heroSize.width.roundToDouble(), 103.0); expect(heroSize.height.roundToDouble(), 60.0); // Jump to 50% into the transition. await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget)); + heroSize = tester.getSize(find.byType(Container)); expect(heroSize.width.roundToDouble(), 189.0); expect(heroSize.height.roundToDouble(), 146.0); // Jump to 75% into the transition. await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget)); + heroSize = tester.getSize(find.byType(Container)); expect(heroSize.width.roundToDouble(), 199.0); expect(heroSize.height.roundToDouble(), 190.0); // Jump to 100% into the transition. await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget)); + heroSize = tester.getSize(find.byType(Container)); expect(heroSize, const Size(200.0, 200.0)); expect(find.byIcon(Icons.arrow_back), findsOneWidget); @@ -47,25 +47,25 @@ void main() { // Jump 25% into the transition (total length = 300ms) await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget)); + heroSize = tester.getSize(find.byType(Container)); expect(heroSize.width.roundToDouble(), 199.0); expect(heroSize.height.roundToDouble(), 190.0); // Jump to 50% into the transition. await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget)); + heroSize = tester.getSize(find.byType(Container)); expect(heroSize.width.roundToDouble(), 189.0); expect(heroSize.height.roundToDouble(), 146.0); // Jump to 75% into the transition. await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget)); + heroSize = tester.getSize(find.byType(Container)); expect(heroSize.width.roundToDouble(), 103.0); expect(heroSize.height.roundToDouble(), 60.0); // Jump to 100% into the transition. await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget)); + heroSize = tester.getSize(find.byType(Container)); expect(heroSize, const Size(50.0, 50.0)); }); } diff --git a/examples/api/test/widgets/heroes/hero.1_test.dart b/examples/api/test/widgets/heroes/hero.1_test.dart index 861e23bde7..18cd6011e7 100644 --- a/examples/api/test/widgets/heroes/hero.1_test.dart +++ b/examples/api/test/widgets/heroes/hero.1_test.dart @@ -16,30 +16,30 @@ void main() { await tester.tap(find.byType(ElevatedButton)); await tester.pump(); - Size heroSize = tester.getSize(find.byType(example.BoxWidget).first); + Size heroSize = tester.getSize(find.byType(Container).first); expect(heroSize, const Size(50.0, 50.0)); // Jump 25% into the transition (total length = 300ms) await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget).first); + heroSize = tester.getSize(find.byType(Container).first); expect(heroSize.width.roundToDouble(), 171.0); expect(heroSize.height.roundToDouble(), 73.0); // Jump to 50% into the transition. await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget).first); + heroSize = tester.getSize(find.byType(Container).first); expect(heroSize.width.roundToDouble(), 371.0); expect(heroSize.height.roundToDouble(), 273.0); // Jump to 75% into the transition. await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget).first); + heroSize = tester.getSize(find.byType(Container).first); expect(heroSize.width.roundToDouble(), 398.0); expect(heroSize.height.roundToDouble(), 376.0); // Jump to 100% into the transition. await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget).first); + heroSize = tester.getSize(find.byType(Container).first); expect(heroSize, const Size(400.0, 400.0)); expect(find.byIcon(Icons.arrow_back), findsOneWidget); @@ -48,25 +48,25 @@ void main() { // Jump 25% into the transition (total length = 300ms) await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget).first); + heroSize = tester.getSize(find.byType(Container).first); expect(heroSize.width.roundToDouble(), 398.0); expect(heroSize.height.roundToDouble(), 376.0); // Jump to 50% into the transition. await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget).first); + heroSize = tester.getSize(find.byType(Container).first); expect(heroSize.width.roundToDouble(), 371.0); expect(heroSize.height.roundToDouble(), 273.0); // Jump to 75% into the transition. await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget).first); + heroSize = tester.getSize(find.byType(Container).first); expect(heroSize.width.roundToDouble(), 171.0); expect(heroSize.height.roundToDouble(), 73.0); // Jump to 100% into the transition. await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget).first); + heroSize = tester.getSize(find.byType(Container).first); expect(heroSize, const Size(50.0, 50.0)); }); @@ -79,30 +79,30 @@ void main() { await tester.tap(find.byType(ElevatedButton)); await tester.pump(); - Size heroSize = tester.getSize(find.byType(example.BoxWidget).last); + Size heroSize = tester.getSize(find.byType(Container).last); expect(heroSize, const Size(50.0, 50.0)); // Jump 25% into the transition (total length = 300ms) await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget).last); + heroSize = tester.getSize(find.byType(Container).last); expect(heroSize.width.roundToDouble(), 133.0); expect(heroSize.height.roundToDouble(), 133.0); // Jump to 50% into the transition. await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget).last); + heroSize = tester.getSize(find.byType(Container).last); expect(heroSize.width.roundToDouble(), 321.0); expect(heroSize.height.roundToDouble(), 321.0); // Jump to 75% into the transition. await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget).first); + heroSize = tester.getSize(find.byType(Container).first); expect(heroSize.width.roundToDouble(), 398.0); expect(heroSize.height.roundToDouble(), 376.0); // Jump to 100% into the transition. await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget).last); + heroSize = tester.getSize(find.byType(Container).last); expect(heroSize, const Size(400.0, 400.0)); expect(find.byIcon(Icons.arrow_back), findsOneWidget); @@ -111,25 +111,25 @@ void main() { // Jump 25% into the transition (total length = 300ms) await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget).last); + heroSize = tester.getSize(find.byType(Container).last); expect(heroSize.width.roundToDouble(), 386.0); expect(heroSize.height.roundToDouble(), 386.0); // Jump to 50% into the transition. await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget).last); + heroSize = tester.getSize(find.byType(Container).last); expect(heroSize.width.roundToDouble(), 321.0); expect(heroSize.height.roundToDouble(), 321.0); // Jump to 75% into the transition. await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget).last); + heroSize = tester.getSize(find.byType(Container).last); expect(heroSize.width.roundToDouble(), 133.0); expect(heroSize.height.roundToDouble(), 133.0); // Jump to 100% into the transition. await tester.pump(const Duration(milliseconds: 75)); // 25% of 300ms - heroSize = tester.getSize(find.byType(example.BoxWidget).last); + heroSize = tester.getSize(find.byType(Container).last); expect(heroSize, const Size(50.0, 50.0)); }); } diff --git a/packages/flutter/lib/src/material/app_bar.dart b/packages/flutter/lib/src/material/app_bar.dart index afd166f6f7..e031d93dc2 100644 --- a/packages/flutter/lib/src/material/app_bar.dart +++ b/packages/flutter/lib/src/material/app_bar.dart @@ -998,8 +998,7 @@ class _AppBarState extends State { } } if (leading != null) { - leading = Container( - alignment: Alignment.center, + leading = ConstrainedBox( constraints: BoxConstraints.tightFor(width: widget.leadingWidth ?? _kLeadingWidth), child: leading, ); @@ -1057,6 +1056,7 @@ class _AppBarState extends State { if (widget.actions != null && widget.actions!.isNotEmpty) { actions = Row( mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.stretch, children: widget.actions!, ); } else if (hasEndDrawer) { diff --git a/packages/flutter/lib/src/material/icon_button.dart b/packages/flutter/lib/src/material/icon_button.dart index 3c47720429..a1c8cbd0d0 100644 --- a/packages/flutter/lib/src/material/icon_button.dart +++ b/packages/flutter/lib/src/material/icon_button.dart @@ -132,11 +132,6 @@ const double _kMinButtonSize = kMinInteractiveDimension; /// precedence: widget property, [IconButtonTheme] property, [IconTheme] property and /// internal default property value. /// -/// In Material Design 3, the [IconButton.visualDensity] defaults to [VisualDensity.standard] -/// for all platforms because the button will have a rounded rectangle shape if -/// the [IconButton.visualDensity] is set to [VisualDensity.compact]. Users can -/// customize it by using [IconButtonTheme], [IconButton.style] or [IconButton.visualDensity]. -/// /// {@tool dartpad} /// This sample shows creation of [IconButton] widgets for standard, filled, /// filled tonal and outlined types, as described in: https://m3.material.io/components/icon-buttons/overview @@ -223,9 +218,6 @@ class IconButton extends StatelessWidget { /// /// {@macro flutter.material.themedata.visualDensity} /// - /// This property can be null. If null, it defaults to [VisualDensity.standard] - /// in Material Design 3 to make sure the button will be circular on all platforms. - /// /// See also: /// /// * [ThemeData.visualDensity], which specifies the [visualDensity] for all @@ -819,7 +811,7 @@ class _IconButtonM3 extends ButtonStyleButton { /// * `mouseCursor` /// * disabled - SystemMouseCursors.basic /// * others - SystemMouseCursors.click - /// * `visualDensity` - VisualDensity.standard + /// * `visualDensity` - theme.visualDensity /// * `tapTargetSize` - theme.materialTapTargetSize /// * `animationDuration` - kThemeChangeDuration /// * `enableFeedback` - true @@ -1061,7 +1053,7 @@ class _IconButtonDefaultsM3 extends ButtonStyle { }); @override - VisualDensity? get visualDensity => VisualDensity.standard; + VisualDensity? get visualDensity => Theme.of(context).visualDensity; @override MaterialTapTargetSize? get tapTargetSize => Theme.of(context).materialTapTargetSize; diff --git a/packages/flutter/lib/src/material/theme_data.dart b/packages/flutter/lib/src/material/theme_data.dart index b92f569029..c7c0c75249 100644 --- a/packages/flutter/lib/src/material/theme_data.dart +++ b/packages/flutter/lib/src/material/theme_data.dart @@ -1261,11 +1261,6 @@ class ThemeData with Diagnosticable { /// /// A larger value translates to a spacing increase (less dense), and a /// smaller value translates to a spacing decrease (more dense). - /// - /// In Material Design 3, the [visualDensity] does not override the value of - /// [IconButton.visualDensity] which defaults to [VisualDensity.standard] - /// for all platforms. To override the default value of [IconButton.visualDensity], - /// use [ThemeData.iconButtonTheme] instead. /// {@endtemplate} final VisualDensity visualDensity; diff --git a/packages/flutter/test/material/app_bar_test.dart b/packages/flutter/test/material/app_bar_test.dart index ab4475bbb9..620d647860 100644 --- a/packages/flutter/test/material/app_bar_test.dart +++ b/packages/flutter/test/material/app_bar_test.dart @@ -707,8 +707,8 @@ void main() { ); final Finder hamburger = find.byTooltip('Open navigation menu'); - expect(tester.getTopLeft(hamburger), const Offset(4.0, 4.0)); - expect(tester.getSize(hamburger), const Size(48.0, 48.0)); + expect(tester.getTopLeft(hamburger), Offset.zero); + expect(tester.getSize(hamburger), const Size(56.0, 56.0)); }); testWidgets('test action is 4dp from edge and 48dp min', (WidgetTester tester) async { @@ -737,14 +737,14 @@ void main() { ), ); - final Finder addButton = find.widgetWithIcon(IconButton, Icons.add); + final Finder addButton = find.byTooltip('Add'); expect(tester.getTopRight(addButton), const Offset(800.0, 0.0)); // It's still the size it was plus the 2 * 8dp padding from IconButton. expect(tester.getSize(addButton), const Size(60.0 + 2 * 8.0, 56.0)); - final Finder shareButton = find.widgetWithIcon(IconButton, Icons.share); + final Finder shareButton = find.byTooltip('Share'); // The 20dp icon is expanded to fill the IconButton's touch target to 48dp. - expect(tester.getSize(shareButton), const Size(48.0, 48.0)); + expect(tester.getSize(shareButton), const Size(48.0, 56.0)); }); testWidgets('SliverAppBar default configuration', (WidgetTester tester) async { @@ -1672,7 +1672,7 @@ void main() { ), ); await tester.tap(find.byKey(key)); - expect(painter, paints..save()..translate()..save()..translate()..circle(x: 24.0, y: 24.0)); + expect(painter, paints..save()..translate()..save()..translate()..circle(x: 24.0, y: 28.0)); }); testWidgets('AppBar handles loose children 0', (WidgetTester tester) async { diff --git a/packages/flutter/test/material/icon_button_test.dart b/packages/flutter/test/material/icon_button_test.dart index 09640ecc49..271db0f22e 100644 --- a/packages/flutter/test/material/icon_button_test.dart +++ b/packages/flutter/test/material/icon_button_test.dart @@ -275,23 +275,11 @@ void main() { testWidgets('Small icons comply with VisualDensity requirements', (WidgetTester tester) async { final bool material3 = theme.useMaterial3; - final ThemeData themeDataM2 = ThemeData( - useMaterial3: material3, - visualDensity: const VisualDensity(horizontal: 1, vertical: -1), - ); - final ThemeData themeDataM3 = ThemeData( - useMaterial3: material3, - iconButtonTheme: IconButtonThemeData( - style: IconButton.styleFrom( - visualDensity: const VisualDensity(horizontal: 1, vertical: -1) - ) - ), - ); await tester.pumpWidget( wrap( useMaterial3: material3, child: Theme( - data: material3 ? themeDataM3 : themeDataM2, + data: ThemeData(visualDensity: const VisualDensity(horizontal: 1, vertical: -1), useMaterial3: material3), child: IconButton( iconSize: 10.0, onPressed: mockOnPressedFunction.handler, @@ -464,9 +452,9 @@ void main() { ), ); + final RenderBox barBox = tester.renderObject(find.byType(AppBar)); final RenderBox iconBox = tester.renderObject(find.byType(IconButton)); - expect(iconBox.size.height, 48.0); - expect(tester.getCenter(find.byType(IconButton)).dy, 28); + expect(iconBox.size.height, equals(barBox.size.height)); }); // This test is very similar to the '...explicit splashColor and highlightColor' test @@ -1641,56 +1629,6 @@ void main() { expect(find.byIcon(Icons.ac_unit), findsOneWidget); }); - testWidgets('The visualDensity of M3 IconButton can be configured by IconButtonTheme, ' - 'but cannot be configured by ThemeData - M3' , (WidgetTester tester) async { - Future buildTest({VisualDensity? iconButtonThemeVisualDensity, VisualDensity? themeVisualDensity}) async { - return tester.pumpWidget( - MaterialApp( - theme: ThemeData.from(colorScheme: colorScheme, useMaterial3: true).copyWith( - iconButtonTheme: IconButtonThemeData( - style: IconButton.styleFrom(visualDensity: iconButtonThemeVisualDensity) - ), - visualDensity: themeVisualDensity - ), - home: Material( - child: Center( - child: IconButton( - onPressed: () {}, - icon: const Icon(Icons.play_arrow), - ), - ), - ), - ), - ); - } - - await buildTest(iconButtonThemeVisualDensity: VisualDensity.standard); - final RenderBox box = tester.renderObject(find.byType(IconButton)); - await tester.pumpAndSettle(); - expect(box.size, equals(const Size(48, 48))); - - await buildTest(iconButtonThemeVisualDensity: VisualDensity.compact); - await tester.pumpAndSettle(); - expect(box.size, equals(const Size(40, 40))); - - await buildTest(iconButtonThemeVisualDensity: const VisualDensity(horizontal: 3.0, vertical: 3.0)); - await tester.pumpAndSettle(); - expect(box.size, equals(const Size(64, 64))); - - // ThemeData.visualDensity will be ignored because useMaterial3 is true - await buildTest(themeVisualDensity: VisualDensity.standard); - await tester.pumpAndSettle(); - expect(box.size, equals(const Size(48, 48))); - - await buildTest(themeVisualDensity: VisualDensity.compact); - await tester.pumpAndSettle(); - expect(box.size, equals(const Size(48, 48))); - - await buildTest(themeVisualDensity: const VisualDensity(horizontal: 3.0, vertical: 3.0)); - await tester.pumpAndSettle(); - expect(box.size, equals(const Size(48, 48))); - }); - group('IconTheme tests in Material 3', () { testWidgets('IconTheme overrides default values in M3', (WidgetTester tester) async { // Theme's IconTheme diff --git a/packages/flutter/test/material/popup_menu_test.dart b/packages/flutter/test/material/popup_menu_test.dart index c356e35efb..80e1b86d48 100644 --- a/packages/flutter/test/material/popup_menu_test.dart +++ b/packages/flutter/test/material/popup_menu_test.dart @@ -2321,14 +2321,13 @@ void main() { await tester.pumpAndSettle(); final Offset button = tester.getTopRight(find.byKey(buttonKey)); - // The topPadding is 32 + (56 - 20) / 2, and the 56 pixels is the app bar height. - expect(button, const Offset(800.0, 50.0)); + expect(button, const Offset(800.0, 32.0)); // The topPadding is 32.0. final Offset popupMenu = tester.getTopRight(find.byType(SingleChildScrollView)); // The menu should be positioned directly next to the top of the button. // The 8.0 pixels is [_kMenuScreenPadding]. - expect(popupMenu, Offset(button.dx - 8.0, button.dy)); + expect(popupMenu, Offset(button.dx - 8.0, button.dy + 8.0)); }); // Regression test for https://github.com/flutter/flutter/issues/82874 @@ -2388,7 +2387,7 @@ void main() { await tester.pumpAndSettle(); final Offset button = tester.getTopRight(find.byKey(buttonKey)); - expect(button, Offset(800.0 - padding.right, 50.0)); // The topPadding is 32 + (56 - 20) / 2. + expect(button, Offset(800.0 - padding.right, padding.top)); // The topPadding is 32.0. final Offset popupMenuTopRight = tester.getTopRight(find.byType(SingleChildScrollView));