From f1c8723a10d8bd70b33f9bf7cf826692a8da95d5 Mon Sep 17 00:00:00 2001 From: Matheus Kirchesch Date: Tue, 30 May 2023 12:38:05 -0300 Subject: [PATCH] Added option to disable [NavigationRailDestination]s (#127113) --- .../lib/src/material/navigation_rail.dart | 17 +++++-- .../test/material/navigation_rail_test.dart | 44 +++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/material/navigation_rail.dart b/packages/flutter/lib/src/material/navigation_rail.dart index 276d70d87f..646e6eaefb 100644 --- a/packages/flutter/lib/src/material/navigation_rail.dart +++ b/packages/flutter/lib/src/material/navigation_rail.dart @@ -467,6 +467,7 @@ class _NavigationRailState extends State with TickerProviderStat tabIndex: i + 1, tabCount: widget.destinations.length, ), + disabled: widget.destinations[i].disabled, ), if (widget.trailing != null) widget.trailing!, @@ -545,6 +546,7 @@ class _RailDestination extends StatelessWidget { required this.useIndicator, this.indicatorColor, this.indicatorShape, + this.disabled = false, }) : _positionAnimation = CurvedAnimation( parent: ReverseAnimation(destinationAnimation), curve: Curves.easeInOut, @@ -567,6 +569,7 @@ class _RailDestination extends StatelessWidget { final bool useIndicator; final Color? indicatorColor; final ShapeBorder? indicatorShape; + final bool disabled; final Animation _positionAnimation; @@ -577,12 +580,16 @@ class _RailDestination extends StatelessWidget { '[NavigationRail.indicatorColor] does not have an effect when [NavigationRail.useIndicator] is false', ); - final bool material3 = Theme.of(context).useMaterial3; + final ThemeData theme = Theme.of(context); + + final bool material3 = theme.useMaterial3; final EdgeInsets destinationPadding = (padding ?? EdgeInsets.zero).resolve(Directionality.of(context)); Offset indicatorOffset; final Widget themedIcon = IconTheme( - data: iconTheme, + data: disabled + ? iconTheme.copyWith(color: theme.colorScheme.onSurface.withOpacity(0.38)) + : iconTheme, child: icon, ); final Widget styledLabel = DefaultTextStyle( @@ -761,7 +768,7 @@ class _RailDestination extends StatelessWidget { Material( type: MaterialType.transparency, child: _IndicatorInkWell( - onTap: onTap, + onTap: disabled ? null : onTap, borderRadius: BorderRadius.all(Radius.circular(minWidth / 2.0)), customBorder: indicatorShape, splashColor: colors.primary.withOpacity(0.12), @@ -909,6 +916,7 @@ class NavigationRailDestination { this.indicatorShape, required this.label, this.padding, + this.disabled = false, }) : selectedIcon = selectedIcon ?? icon; /// The icon of the destination. @@ -954,6 +962,9 @@ class NavigationRailDestination { /// The amount of space to inset the destination item. final EdgeInsetsGeometry? padding; + + /// Indicates that this destination is inaccessible. + final bool disabled; } class _ExtendedNavigationRailAnimation extends InheritedWidget { diff --git a/packages/flutter/test/material/navigation_rail_test.dart b/packages/flutter/test/material/navigation_rail_test.dart index 7590c8ea39..ccfaa9947a 100644 --- a/packages/flutter/test/material/navigation_rail_test.dart +++ b/packages/flutter/test/material/navigation_rail_test.dart @@ -3136,6 +3136,50 @@ void main() { expect(_getIndicatorDecoration(tester)?.shape, shape); }); + testWidgets("Destination's respect their disabled state", (WidgetTester tester) async { + late int selectedIndex; + await _pumpNavigationRail( + tester, + navigationRail: NavigationRail( + selectedIndex: 0, + destinations: const [ + NavigationRailDestination( + icon: Icon(Icons.favorite_border), + selectedIcon: Icon(Icons.favorite), + label: Text('Abc'), + ), + NavigationRailDestination( + icon: Icon(Icons.star_border), + selectedIcon: Icon(Icons.star), + label: Text('Bcd'), + ), + NavigationRailDestination( + icon: Icon(Icons.bookmark_border), + selectedIcon: Icon(Icons.bookmark), + label: Text('Cde'), + disabled: true, + ), + ], + onDestinationSelected: (int index) { + selectedIndex = index; + }, + labelType: NavigationRailLabelType.all, + ), + ); + + await tester.tap(find.text('Abc')); + expect(selectedIndex, 0); + + await tester.tap(find.text('Bcd')); + expect(selectedIndex, 1); + + await tester.tap(find.text('Cde')); + expect(selectedIndex, 1); + + // Wait for any pending shader compilation. + tester.pumpAndSettle(); + }); + group('Material 2', () { // These tests are only relevant for Material 2. Once Material 2 // support is deprecated and the APIs are removed, these tests