Skip to content

Commit

Permalink
[NavigationDrawer] adds padding property in NavigationDrawer Widget (…
Browse files Browse the repository at this point in the history
…#123961)

Adds `tilePadding` property to `NavigationDrawer` Widget.

Fixes: #121662

| Without adding `tilePadding` in NavigationDrawer | With `tilePadding: EdgeInsets.all(16)` in NavigationDrawer |
| --- | --- |
| ![photo_2023-04-02_15-03-56](https://user-images.githubusercontent.com/13456345/229344718-9070c0c0-9c71-4436-9953-d258367951e9.jpg) | ![photo_2023-04-02_15-04-00](https://user-images.githubusercontent.com/13456345/229344717-0768f9d5-6a55-4c2f-918e-53f2423bb959.jpg) |
  • Loading branch information
piedcipher authored May 5, 2023
1 parent 04e284a commit 91a84de
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/flutter/lib/src/material/navigation_drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class NavigationDrawer extends StatelessWidget {
this.indicatorShape,
this.onDestinationSelected,
this.selectedIndex = 0,
this.tilePadding = const EdgeInsets.symmetric(horizontal: 12.0),
});

/// The background color of the [Material] that holds the [NavigationDrawer]'s
Expand Down Expand Up @@ -124,6 +125,11 @@ class NavigationDrawer extends StatelessWidget {
/// Upon updating [selectedIndex], the [NavigationDrawer] will be rebuilt.
final ValueChanged<int>? onDestinationSelected;

/// Defines the padding for [NavigationDrawerDestination] widgets (Drawer items).
///
/// Defaults to `EdgeInsets.symmetric(horizontal: 12.0)`.
final EdgeInsetsGeometry tilePadding;

@override
Widget build(BuildContext context) {
final int totalNumberOfDestinations =
Expand All @@ -141,6 +147,7 @@ class NavigationDrawer extends StatelessWidget {
selectedAnimation: animation,
indicatorColor: indicatorColor,
indicatorShape: indicatorShape,
tilePadding: tilePadding,
onTap: () {
if (onDestinationSelected != null) {
onDestinationSelected!(index);
Expand Down Expand Up @@ -321,7 +328,7 @@ class _NavigationDestinationBuilder extends StatelessWidget {
final NavigationDrawerThemeData defaults = _NavigationDrawerDefaultsM3(context);

return Padding(
padding: const EdgeInsets.symmetric(horizontal: 12.0),
padding: info.tilePadding,
child: _NavigationDestinationSemantics(
child: SizedBox(
height: navigationDrawerTheme.tileHeight ?? defaults.tileHeight,
Expand Down Expand Up @@ -455,6 +462,7 @@ class _NavigationDrawerDestinationInfo extends InheritedWidget {
required this.indicatorShape,
required this.onTap,
required super.child,
required this.tilePadding,
});

/// Which destination index is this in the navigation drawer.
Expand Down Expand Up @@ -514,6 +522,11 @@ class _NavigationDrawerDestinationInfo extends InheritedWidget {
/// with [index] passed in.
final VoidCallback onTap;

/// Defines the padding for [NavigationDrawerDestination] widgets (Drawer items).
///
/// Defaults to `EdgeInsets.symmetric(horizontal: 12.0)`.
final EdgeInsetsGeometry tilePadding;

/// Returns a non null [_NavigationDrawerDestinationInfo].
///
/// This will return an error if called with no [_NavigationDrawerDestinationInfo]
Expand Down
23 changes: 23 additions & 0 deletions packages/flutter/test/material/navigation_drawer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,29 @@ void main() {
// Test that InkWell for hover, focus and pressed use custom shape.
expect(_getInkWell(tester)?.customBorder, shape);
});

testWidgets('NavigationDrawer.tilePadding defaults to EdgeInsets.symmetric(horizontal: 12.0)', (WidgetTester tester) async {
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
widgetSetup(tester, 3000, viewHeight: 3000);
final Widget widget = _buildWidget(
scaffoldKey,
NavigationDrawer(
children: const <Widget>[
NavigationDrawerDestination(
icon: Icon(Icons.ac_unit),
label: Text('AC'),
),
],
onDestinationSelected: (int i) {},
),
);

await tester.pumpWidget(widget);
scaffoldKey.currentState?.openDrawer();
await tester.pump();
final NavigationDrawer drawer = tester.widget(find.byType(NavigationDrawer));
expect(drawer.tilePadding, const EdgeInsets.symmetric(horizontal: 12.0));
});
}

Widget _buildWidget(GlobalKey<ScaffoldState> scaffoldKey, Widget child) {
Expand Down

0 comments on commit 91a84de

Please sign in to comment.