Skip to content

Commit eaf74bd

Browse files
authored
Fix NavigationDrawerDestination backgroundColor obscures interactions (flutter#160239)
## Description This PR fixes `NavigationDrawerDestination.backgroundColor` obscuring ink well splashes and overlay. Before this PR the destination background color was renderer in a `ColoredBox` which hides the ink well effects. This PR replaces the `ColorsBox` with an `Ink`. ## Related Issue Fixes [NavigationDrawerDestination backgroundColor obscures interaction states](flutter#160109) ## Tests Updates 1 test.
1 parent 26ea690 commit eaf74bd

File tree

2 files changed

+66
-45
lines changed

2 files changed

+66
-45
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:flutter/widgets.dart';
1010
import 'color_scheme.dart';
1111
import 'colors.dart';
1212
import 'drawer.dart';
13+
import 'ink_decoration.dart';
1314
import 'ink_well.dart';
1415
import 'material.dart';
1516
import 'material_localizations.dart';
@@ -196,9 +197,12 @@ class NavigationDrawerDestination extends StatelessWidget {
196197
this.enabled = true,
197198
});
198199

199-
/// Sets the color of the destination.
200+
/// The background color of the destination.
200201
///
201-
/// If this is null, then [NavigationDrawerThemeData.backgroundColor].
202+
/// If this is null, no background is set and [NavigationDrawer.backgroundColor] will be visible.
203+
///
204+
/// This is the background color of the whole rectangular area behind the drawer destination.
205+
/// To customize only the indicator color consider using [NavigationDrawer.indicatorColor].
202206
final Color? backgroundColor;
203207

204208
/// The [Widget] (usually an [Icon]) that's displayed for this
@@ -339,9 +343,6 @@ class _NavigationDestinationBuilder extends StatelessWidget {
339343
/// Defaults to true.
340344
final bool enabled;
341345

342-
/// Sets the color of navigation destination.
343-
///
344-
/// If this is null, then [NavigationDrawerThemeData.backgroundColor] is used.
345346
final Color? backgroundColor;
346347

347348
@override
@@ -386,9 +387,8 @@ class _NavigationDestinationBuilder extends StatelessWidget {
386387
),
387388
);
388389

389-
final Color? color = backgroundColor ?? navigationDrawerTheme.backgroundColor;
390-
if (color != null) {
391-
return ColoredBox(color: color, child: destination);
390+
if (backgroundColor != null) {
391+
return Ink(color: backgroundColor, child: destination);
392392
}
393393
return destination;
394394
}

packages/flutter/test/material/navigation_drawer_test.dart

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -82,45 +82,66 @@ void main() {
8282
expect(_getMaterial(tester).color, equals(color));
8383
});
8484

85-
testWidgets('NavigationDrawer can update destination background color',
86-
(WidgetTester tester) async {
87-
const Color color = Colors.yellow;
88-
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
89-
final ThemeData theme = ThemeData();
90-
91-
await tester.pumpWidget(
92-
_buildWidget(
93-
scaffoldKey,
94-
NavigationDrawer(
95-
children: <Widget>[
96-
Text('Headline', style: theme.textTheme.bodyLarge),
97-
NavigationDrawerDestination(
98-
icon: Icon(Icons.ac_unit, color: theme.iconTheme.color),
99-
label: Text('AC', style: theme.textTheme.bodySmall),
100-
backgroundColor: color,
101-
),
102-
NavigationDrawerDestination(
103-
icon: Icon(Icons.access_alarm, color: theme.iconTheme.color),
104-
label: Text('Alarm', style: theme.textTheme.bodySmall),
105-
backgroundColor: color,
106-
),
107-
],
108-
onDestinationSelected: (int i) {},
85+
testWidgets(
86+
'NavigationDestinationDrawer background color is customizable',
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+
),
102+
NavigationDrawerDestination(
103+
icon: Icon(Icons.access_alarm, color: theme.iconTheme.color),
104+
label: Text('Alarm', style: theme.textTheme.bodySmall),
105+
backgroundColor: color,
106+
),
107+
],
108+
onDestinationSelected: (int i) {},
109+
),
109110
),
110-
),
111-
);
112-
113-
scaffoldKey.currentState!.openDrawer();
114-
await tester.pump(const Duration(seconds: 1)); // animation done
115-
final ColoredBox destinationColor = tester.firstWidget<ColoredBox>(
116-
find.descendant(
117-
of: find.byType(NavigationDrawerDestination),
118-
matching: find.byType(ColoredBox),
119-
),
120-
);
111+
);
121112

122-
expect(destinationColor.color, equals(color));
123-
});
113+
Finder findDestinationInk(String label) {
114+
return find.descendant(
115+
of: find.ancestor(
116+
of: find.text(label),
117+
matching: find.byType(NavigationDrawerDestination),
118+
),
119+
matching: find.byType(Ink),
120+
);
121+
}
122+
123+
scaffoldKey.currentState!.openDrawer();
124+
await tester.pump();
125+
await tester.pump(const Duration(seconds: 1)); // Animation done.
126+
127+
// Destination with no custom background color.
128+
await tester.tap(find.text('AC'));
129+
await tester.pump();
130+
131+
expect(findDestinationInk('AC'), findsNothing);
132+
133+
// Destination with a custom background color.
134+
await tester.tap(find.byIcon(Icons.access_alarm));
135+
await tester.pump();
136+
137+
// A Material is added with the custom color.
138+
expect(findDestinationInk('Alarm'), findsOne);
139+
final BoxDecoration destinationDecoration = tester.firstWidget<Ink>(
140+
findDestinationInk('Alarm'),
141+
).decoration! as BoxDecoration;
142+
expect(destinationDecoration.color, color);
143+
},
144+
);
124145

125146
testWidgets('NavigationDrawer can update elevation',
126147
(WidgetTester tester) async {

0 commit comments

Comments
 (0)