Skip to content

Commit 81e390c

Browse files
Add background color to NavigationDrawerDestination (#139154)
fixes:#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]. <!-- Links --> [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 <[email protected]>
1 parent 91ea024 commit 81e390c

2 files changed

Lines changed: 53 additions & 9 deletions

File tree

‎packages/flutter/lib/src/material/navigation_drawer.dart‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,9 @@ class NavigationDrawerDestination extends StatelessWidget {
196196
this.enabled = true,
197197
});
198198

199-
/// Sets the color of the [Material] that holds all of the [Drawer]'s
200-
/// contents.
199+
/// Sets the color of the destination.
201200
///
202-
/// If this is null, then [DrawerThemeData.backgroundColor] is used. If that
203-
/// is also null, then it falls back to [Material]'s default.
201+
/// If this is null, then [NavigationDrawerThemeData.backgroundColor].
204202
final Color? backgroundColor;
205203

206204
/// The [Widget] (usually an [Icon]) that's displayed for this
@@ -286,6 +284,7 @@ class NavigationDrawerDestination extends StatelessWidget {
286284
);
287285
},
288286
enabled: enabled,
287+
backgroundColor: backgroundColor,
289288
);
290289
}
291290
}
@@ -308,6 +307,7 @@ class _NavigationDestinationBuilder extends StatelessWidget {
308307
required this.buildIcon,
309308
required this.buildLabel,
310309
this.enabled = true,
310+
this.backgroundColor,
311311
});
312312

313313
/// Builds the icon for a destination in a [NavigationDrawer].
@@ -339,6 +339,11 @@ class _NavigationDestinationBuilder extends StatelessWidget {
339339
/// Defaults to true.
340340
final bool enabled;
341341

342+
/// Sets the color of navigation destination.
343+
///
344+
/// If this is null, then [NavigationDrawerTheme.backgroundColor] is used.
345+
final Color? backgroundColor;
346+
342347
@override
343348
Widget build(BuildContext context) {
344349
final _NavigationDrawerDestinationInfo info = _NavigationDrawerDestinationInfo.of(context);
@@ -354,8 +359,9 @@ class _NavigationDestinationBuilder extends StatelessWidget {
354359
],
355360
);
356361

357-
return Padding(
362+
return Container(
358363
padding: info.tilePadding,
364+
color: backgroundColor ?? navigationDrawerTheme.backgroundColor,
359365
child: _NavigationDestinationSemantics(
360366
child: SizedBox(
361367
height: navigationDrawerTheme.tileHeight ?? defaults.tileHeight,

‎packages/flutter/test/material/navigation_drawer_test.dart‎

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ void main() {
1111
(WidgetTester tester) async {
1212
int mutatedIndex = -1;
1313
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
14-
final ThemeData theme= ThemeData.from(colorScheme: const ColorScheme.light());
14+
final ThemeData theme = ThemeData();
1515
widgetSetup(tester, 3000, viewHeight: 3000);
1616
final Widget widget = _buildWidget(
1717
scaffoldKey,
@@ -54,7 +54,7 @@ void main() {
5454
(WidgetTester tester) async {
5555
const Color color = Colors.yellow;
5656
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
57-
final ThemeData theme= ThemeData.from(colorScheme: const ColorScheme.light());
57+
final ThemeData theme = ThemeData();
5858

5959
await tester.pumpWidget(
6060
_buildWidget(
@@ -83,11 +83,49 @@ void main() {
8383
expect(_getMaterial(tester).color, equals(color));
8484
});
8585

86+
testWidgetsWithLeakTracking('NavigationDrawer can update destination background color',
87+
(WidgetTester tester) async {
88+
const Color color = Colors.yellow;
89+
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
90+
final ThemeData theme = ThemeData();
91+
92+
await tester.pumpWidget(
93+
_buildWidget(
94+
scaffoldKey,
95+
NavigationDrawer(
96+
children: <Widget>[
97+
Text('Headline', style: theme.textTheme.bodyLarge),
98+
NavigationDrawerDestination(
99+
icon: Icon(Icons.ac_unit, color: theme.iconTheme.color),
100+
label: Text('AC', style: theme.textTheme.bodySmall),
101+
backgroundColor: color,
102+
),
103+
NavigationDrawerDestination(
104+
icon: Icon(Icons.access_alarm, color: theme.iconTheme.color),
105+
label: Text('Alarm', style: theme.textTheme.bodySmall),
106+
backgroundColor: color,
107+
),
108+
],
109+
onDestinationSelected: (int i) {},
110+
),
111+
),
112+
);
113+
114+
scaffoldKey.currentState!.openDrawer();
115+
await tester.pump(const Duration(seconds: 1)); // animation done
116+
final Container destinationColor = tester.firstWidget<Container>(
117+
find.descendant(
118+
of: find.byType(NavigationDrawerDestination), matching: find.byType(Container)),
119+
);
120+
121+
expect(destinationColor.color, equals(color));
122+
});
123+
86124
testWidgetsWithLeakTracking('NavigationDrawer can update elevation',
87125
(WidgetTester tester) async {
88126
const double elevation = 42.0;
89127
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
90-
final ThemeData theme= ThemeData.from(colorScheme: const ColorScheme.light());
128+
final ThemeData theme = ThemeData();
91129
final NavigationDrawer drawer = NavigationDrawer(
92130
elevation: elevation,
93131
children: <Widget>[
@@ -254,7 +292,7 @@ void main() {
254292

255293
testWidgetsWithLeakTracking('Navigation drawer semantics', (WidgetTester tester) async {
256294
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
257-
final ThemeData theme= ThemeData.from(colorScheme: const ColorScheme.light());
295+
final ThemeData theme = ThemeData();
258296
Widget widget({int selectedIndex = 0}) {
259297
return _buildWidget(
260298
scaffoldKey,

0 commit comments

Comments
 (0)