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 { ...@@ -196,11 +196,9 @@ class NavigationDrawerDestination extends StatelessWidget {
this.enabled = true, this.enabled = true,
}); });
/// Sets the color of the [Material] that holds all of the [Drawer]'s /// Sets the color of the destination.
/// contents.
/// ///
/// If this is null, then [DrawerThemeData.backgroundColor] is used. If that /// If this is null, then [NavigationDrawerThemeData.backgroundColor].
/// is also null, then it falls back to [Material]'s default.
final Color? backgroundColor; final Color? backgroundColor;
/// The [Widget] (usually an [Icon]) that's displayed for this /// The [Widget] (usually an [Icon]) that's displayed for this
...@@ -286,6 +284,7 @@ class NavigationDrawerDestination extends StatelessWidget { ...@@ -286,6 +284,7 @@ class NavigationDrawerDestination extends StatelessWidget {
); );
}, },
enabled: enabled, enabled: enabled,
backgroundColor: backgroundColor,
); );
} }
} }
...@@ -308,6 +307,7 @@ class _NavigationDestinationBuilder extends StatelessWidget { ...@@ -308,6 +307,7 @@ class _NavigationDestinationBuilder extends StatelessWidget {
required this.buildIcon, required this.buildIcon,
required this.buildLabel, required this.buildLabel,
this.enabled = true, this.enabled = true,
this.backgroundColor,
}); });
/// Builds the icon for a destination in a [NavigationDrawer]. /// Builds the icon for a destination in a [NavigationDrawer].
...@@ -339,6 +339,11 @@ class _NavigationDestinationBuilder extends StatelessWidget { ...@@ -339,6 +339,11 @@ class _NavigationDestinationBuilder extends StatelessWidget {
/// Defaults to true. /// Defaults to true.
final bool enabled; final bool enabled;
/// Sets the color of navigation destination.
///
/// If this is null, then [NavigationDrawerTheme.backgroundColor] is used.
final Color? backgroundColor;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final _NavigationDrawerDestinationInfo info = _NavigationDrawerDestinationInfo.of(context); final _NavigationDrawerDestinationInfo info = _NavigationDrawerDestinationInfo.of(context);
...@@ -354,8 +359,9 @@ class _NavigationDestinationBuilder extends StatelessWidget { ...@@ -354,8 +359,9 @@ class _NavigationDestinationBuilder extends StatelessWidget {
], ],
); );
return Padding( return Container(
padding: info.tilePadding, padding: info.tilePadding,
color: backgroundColor ?? navigationDrawerTheme.backgroundColor,
child: _NavigationDestinationSemantics( child: _NavigationDestinationSemantics(
child: SizedBox( child: SizedBox(
height: navigationDrawerTheme.tileHeight ?? defaults.tileHeight, height: navigationDrawerTheme.tileHeight ?? defaults.tileHeight,
......
...@@ -11,7 +11,7 @@ void main() { ...@@ -11,7 +11,7 @@ void main() {
(WidgetTester tester) async { (WidgetTester tester) async {
int mutatedIndex = -1; int mutatedIndex = -1;
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>(); final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
final ThemeData theme= ThemeData.from(colorScheme: const ColorScheme.light()); final ThemeData theme = ThemeData();
widgetSetup(tester, 3000, viewHeight: 3000); widgetSetup(tester, 3000, viewHeight: 3000);
final Widget widget = _buildWidget( final Widget widget = _buildWidget(
scaffoldKey, scaffoldKey,
...@@ -54,7 +54,7 @@ void main() { ...@@ -54,7 +54,7 @@ void main() {
(WidgetTester tester) async { (WidgetTester tester) async {
const Color color = Colors.yellow; const Color color = Colors.yellow;
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>(); final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
final ThemeData theme= ThemeData.from(colorScheme: const ColorScheme.light()); final ThemeData theme = ThemeData();
await tester.pumpWidget( await tester.pumpWidget(
_buildWidget( _buildWidget(
...@@ -83,11 +83,49 @@ void main() { ...@@ -83,11 +83,49 @@ void main() {
expect(_getMaterial(tester).color, equals(color)); 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', testWidgetsWithLeakTracking('NavigationDrawer can update elevation',
(WidgetTester tester) async { (WidgetTester tester) async {
const double elevation = 42.0; const double elevation = 42.0;
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>(); final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
final ThemeData theme= ThemeData.from(colorScheme: const ColorScheme.light()); final ThemeData theme = ThemeData();
final NavigationDrawer drawer = NavigationDrawer( final NavigationDrawer drawer = NavigationDrawer(
elevation: elevation, elevation: elevation,
children: <Widget>[ children: <Widget>[
...@@ -254,7 +292,7 @@ void main() { ...@@ -254,7 +292,7 @@ void main() {
testWidgetsWithLeakTracking('Navigation drawer semantics', (WidgetTester tester) async { testWidgetsWithLeakTracking('Navigation drawer semantics', (WidgetTester tester) async {
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>(); final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
final ThemeData theme= ThemeData.from(colorScheme: const ColorScheme.light()); final ThemeData theme = ThemeData();
Widget widget({int selectedIndex = 0}) { Widget widget({int selectedIndex = 0}) {
return _buildWidget( return _buildWidget(
scaffoldKey, 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