Unverified Commit 81e390c6 authored by hangyu's avatar hangyu Committed by GitHub

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].

<!-- 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: 's avatarQun Cheng <36861262+QuncCccccc@users.noreply.github.com>
parent 91ea024c
......@@ -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,
......
......@@ -11,7 +11,7 @@ void main() {
(WidgetTester tester) async {
int mutatedIndex = -1;
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
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<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
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<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
final ThemeData theme = ThemeData();
await tester.pumpWidget(
_buildWidget(
scaffoldKey,
NavigationDrawer(
children: <Widget>[
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<Container>(
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<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
final ThemeData theme= ThemeData.from(colorScheme: const ColorScheme.light());
final ThemeData theme = ThemeData();
final NavigationDrawer drawer = NavigationDrawer(
elevation: elevation,
children: <Widget>[
......@@ -254,7 +292,7 @@ void main() {
testWidgetsWithLeakTracking('Navigation drawer semantics', (WidgetTester tester) async {
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
final ThemeData theme= ThemeData.from(colorScheme: const ColorScheme.light());
final ThemeData theme = ThemeData();
Widget widget({int selectedIndex = 0}) {
return _buildWidget(
scaffoldKey,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment