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,