From 81e390c62123d93dc720cbf435a5662e2035db56 Mon Sep 17 00:00:00 2001 From: hangyu Date: Thu, 7 Dec 2023 12:51:20 -0800 Subject: [PATCH] Add background color to NavigationDrawerDestination (#139154) fixes:https://github.com/flutter/flutter/issues/138105 ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [Features we expect every widget to implement]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat --------- Co-authored-by: Qun Cheng <36861262+QuncCccccc@users.noreply.github.com> --- .../lib/src/material/navigation_drawer.dart | 16 +++++-- .../test/material/navigation_drawer_test.dart | 46 +++++++++++++++++-- 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/packages/flutter/lib/src/material/navigation_drawer.dart b/packages/flutter/lib/src/material/navigation_drawer.dart index 3c4edbe172..8ce07e90d5 100644 --- a/packages/flutter/lib/src/material/navigation_drawer.dart +++ b/packages/flutter/lib/src/material/navigation_drawer.dart @@ -196,11 +196,9 @@ class NavigationDrawerDestination extends StatelessWidget { this.enabled = true, }); - /// Sets the color of the [Material] that holds all of the [Drawer]'s - /// contents. + /// Sets the color of the destination. /// - /// If this is null, then [DrawerThemeData.backgroundColor] is used. If that - /// is also null, then it falls back to [Material]'s default. + /// If this is null, then [NavigationDrawerThemeData.backgroundColor]. final Color? backgroundColor; /// The [Widget] (usually an [Icon]) that's displayed for this @@ -286,6 +284,7 @@ class NavigationDrawerDestination extends StatelessWidget { ); }, enabled: enabled, + backgroundColor: backgroundColor, ); } } @@ -308,6 +307,7 @@ class _NavigationDestinationBuilder extends StatelessWidget { required this.buildIcon, required this.buildLabel, this.enabled = true, + this.backgroundColor, }); /// Builds the icon for a destination in a [NavigationDrawer]. @@ -339,6 +339,11 @@ class _NavigationDestinationBuilder extends StatelessWidget { /// Defaults to true. final bool enabled; + /// Sets the color of navigation destination. + /// + /// If this is null, then [NavigationDrawerTheme.backgroundColor] is used. + final Color? backgroundColor; + @override Widget build(BuildContext context) { final _NavigationDrawerDestinationInfo info = _NavigationDrawerDestinationInfo.of(context); @@ -354,8 +359,9 @@ class _NavigationDestinationBuilder extends StatelessWidget { ], ); - return Padding( + return Container( padding: info.tilePadding, + color: backgroundColor ?? navigationDrawerTheme.backgroundColor, child: _NavigationDestinationSemantics( child: SizedBox( height: navigationDrawerTheme.tileHeight ?? defaults.tileHeight, diff --git a/packages/flutter/test/material/navigation_drawer_test.dart b/packages/flutter/test/material/navigation_drawer_test.dart index 6df86c5a29..bf6ab6132b 100644 --- a/packages/flutter/test/material/navigation_drawer_test.dart +++ b/packages/flutter/test/material/navigation_drawer_test.dart @@ -11,7 +11,7 @@ void main() { (WidgetTester tester) async { int mutatedIndex = -1; final GlobalKey scaffoldKey = GlobalKey(); - final ThemeData theme= ThemeData.from(colorScheme: const ColorScheme.light()); + final ThemeData theme = ThemeData(); widgetSetup(tester, 3000, viewHeight: 3000); final Widget widget = _buildWidget( scaffoldKey, @@ -54,7 +54,7 @@ void main() { (WidgetTester tester) async { const Color color = Colors.yellow; final GlobalKey scaffoldKey = GlobalKey(); - final ThemeData theme= ThemeData.from(colorScheme: const ColorScheme.light()); + final ThemeData theme = ThemeData(); await tester.pumpWidget( _buildWidget( @@ -83,11 +83,49 @@ void main() { expect(_getMaterial(tester).color, equals(color)); }); + testWidgetsWithLeakTracking('NavigationDrawer can update destination background color', + (WidgetTester tester) async { + const Color color = Colors.yellow; + final GlobalKey scaffoldKey = GlobalKey(); + final ThemeData theme = ThemeData(); + + await tester.pumpWidget( + _buildWidget( + scaffoldKey, + NavigationDrawer( + children: [ + Text('Headline', style: theme.textTheme.bodyLarge), + NavigationDrawerDestination( + icon: Icon(Icons.ac_unit, color: theme.iconTheme.color), + label: Text('AC', style: theme.textTheme.bodySmall), + backgroundColor: color, + ), + NavigationDrawerDestination( + icon: Icon(Icons.access_alarm, color: theme.iconTheme.color), + label: Text('Alarm', style: theme.textTheme.bodySmall), + backgroundColor: color, + ), + ], + onDestinationSelected: (int i) {}, + ), + ), + ); + + scaffoldKey.currentState!.openDrawer(); + await tester.pump(const Duration(seconds: 1)); // animation done + final Container destinationColor = tester.firstWidget( + find.descendant( + of: find.byType(NavigationDrawerDestination), matching: find.byType(Container)), + ); + + expect(destinationColor.color, equals(color)); + }); + testWidgetsWithLeakTracking('NavigationDrawer can update elevation', (WidgetTester tester) async { const double elevation = 42.0; final GlobalKey scaffoldKey = GlobalKey(); - final ThemeData theme= ThemeData.from(colorScheme: const ColorScheme.light()); + final ThemeData theme = ThemeData(); final NavigationDrawer drawer = NavigationDrawer( elevation: elevation, children: [ @@ -254,7 +292,7 @@ void main() { testWidgetsWithLeakTracking('Navigation drawer semantics', (WidgetTester tester) async { final GlobalKey scaffoldKey = GlobalKey(); - final ThemeData theme= ThemeData.from(colorScheme: const ColorScheme.light()); + final ThemeData theme = ThemeData(); Widget widget({int selectedIndex = 0}) { return _buildWidget( scaffoldKey,