Added padding property to NavigationRail items (#69404)
This commit is contained in:
@@ -496,6 +496,7 @@ class _NavigationRailState extends State<NavigationRail> with TickerProviderStat
|
||||
labelType: labelType,
|
||||
iconTheme: widget.selectedIndex == i ? selectedIconTheme : unselectedIconTheme,
|
||||
labelTextStyle: widget.selectedIndex == i ? selectedLabelTextStyle : unselectedLabelTextStyle,
|
||||
padding: widget.destinations[i].padding,
|
||||
onTap: () {
|
||||
widget.onDestinationSelected!(i);
|
||||
},
|
||||
@@ -579,6 +580,7 @@ class _RailDestination extends StatelessWidget {
|
||||
required this.labelTextStyle,
|
||||
required this.onTap,
|
||||
required this.indexLabel,
|
||||
this.padding,
|
||||
}) : assert(minWidth != null),
|
||||
assert(minExtendedWidth != null),
|
||||
assert(icon != null),
|
||||
@@ -609,6 +611,7 @@ class _RailDestination extends StatelessWidget {
|
||||
final TextStyle labelTextStyle;
|
||||
final VoidCallback onTap;
|
||||
final String indexLabel;
|
||||
final EdgeInsetsGeometry? padding;
|
||||
|
||||
final Animation<double> _positionAnimation;
|
||||
|
||||
@@ -634,42 +637,48 @@ class _RailDestination extends StatelessWidget {
|
||||
),
|
||||
);
|
||||
if (extendedTransitionAnimation.value == 0) {
|
||||
content = Stack(
|
||||
children: <Widget>[
|
||||
iconPart,
|
||||
// For semantics when label is not showing,
|
||||
SizedBox(
|
||||
width: 0,
|
||||
height: 0,
|
||||
child: Opacity(
|
||||
alwaysIncludeSemantics: true,
|
||||
opacity: 0.0,
|
||||
child: label,
|
||||
content = Padding(
|
||||
padding: padding ?? EdgeInsets.zero,
|
||||
child: Stack(
|
||||
children: <Widget>[
|
||||
iconPart,
|
||||
// For semantics when label is not showing,
|
||||
SizedBox(
|
||||
width: 0,
|
||||
height: 0,
|
||||
child: Opacity(
|
||||
alwaysIncludeSemantics: true,
|
||||
opacity: 0.0,
|
||||
child: label,
|
||||
),
|
||||
),
|
||||
),
|
||||
]
|
||||
]
|
||||
),
|
||||
);
|
||||
} else {
|
||||
content = ConstrainedBox(
|
||||
constraints: BoxConstraints(
|
||||
minWidth: lerpDouble(minWidth, minExtendedWidth, extendedTransitionAnimation.value)!,
|
||||
),
|
||||
child: ClipRect(
|
||||
child: Row(
|
||||
children: <Widget>[
|
||||
iconPart,
|
||||
Align(
|
||||
heightFactor: 1.0,
|
||||
widthFactor: extendedTransitionAnimation.value,
|
||||
alignment: AlignmentDirectional.centerStart,
|
||||
child: Opacity(
|
||||
alwaysIncludeSemantics: true,
|
||||
opacity: _extendedLabelFadeValue(),
|
||||
child: styledLabel,
|
||||
content = Padding(
|
||||
padding: padding ?? EdgeInsets.zero,
|
||||
child: ConstrainedBox(
|
||||
constraints: BoxConstraints(
|
||||
minWidth: lerpDouble(minWidth, minExtendedWidth, extendedTransitionAnimation.value)!,
|
||||
),
|
||||
child: ClipRect(
|
||||
child: Row(
|
||||
children: <Widget>[
|
||||
iconPart,
|
||||
Align(
|
||||
heightFactor: 1.0,
|
||||
widthFactor: extendedTransitionAnimation.value,
|
||||
alignment: AlignmentDirectional.centerStart,
|
||||
child: Opacity(
|
||||
alwaysIncludeSemantics: true,
|
||||
opacity: _extendedLabelFadeValue(),
|
||||
child: styledLabel,
|
||||
),
|
||||
),
|
||||
),
|
||||
SizedBox(width: _horizontalDestinationPadding * extendedTransitionAnimation.value),
|
||||
],
|
||||
SizedBox(width: _horizontalDestinationPadding * extendedTransitionAnimation.value),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -683,7 +692,7 @@ class _RailDestination extends StatelessWidget {
|
||||
minWidth: minWidth,
|
||||
minHeight: minWidth,
|
||||
),
|
||||
padding: const EdgeInsets.symmetric(horizontal: _horizontalDestinationPadding),
|
||||
padding: padding ?? const EdgeInsets.symmetric(horizontal: _horizontalDestinationPadding),
|
||||
child: ClipRect(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
@@ -713,7 +722,7 @@ class _RailDestination extends StatelessWidget {
|
||||
minWidth: minWidth,
|
||||
minHeight: minWidth,
|
||||
),
|
||||
padding: const EdgeInsets.symmetric(horizontal: _horizontalDestinationPadding),
|
||||
padding: padding ?? const EdgeInsets.symmetric(horizontal: _horizontalDestinationPadding),
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
const SizedBox(height: _verticalDestinationPaddingWithLabel),
|
||||
@@ -811,6 +820,7 @@ class NavigationRailDestination {
|
||||
required this.icon,
|
||||
Widget? selectedIcon,
|
||||
this.label,
|
||||
this.padding,
|
||||
}) : selectedIcon = selectedIcon ?? icon,
|
||||
assert(icon != null);
|
||||
|
||||
@@ -848,6 +858,9 @@ class NavigationRailDestination {
|
||||
/// still used for semantics, and may still be used if
|
||||
/// [NavigationRail.extended] is true.
|
||||
final Widget? label;
|
||||
|
||||
/// The amount of space to inset the destination item.
|
||||
final EdgeInsetsGeometry? padding;
|
||||
}
|
||||
|
||||
class _ExtendedNavigationRailAnimation extends InheritedWidget {
|
||||
|
||||
@@ -2022,6 +2022,129 @@ void main() {
|
||||
|
||||
semantics.dispose();
|
||||
});
|
||||
|
||||
testWidgets('NavigationRailDestination padding properly applied - NavigationRailLabelType.all', (WidgetTester tester) async {
|
||||
const EdgeInsets defaultPadding = EdgeInsets.symmetric(horizontal: 8.0);
|
||||
const EdgeInsets secondItemPadding = EdgeInsets.symmetric(vertical: 30.0);
|
||||
const EdgeInsets thirdItemPadding = EdgeInsets.symmetric(horizontal: 10.0);
|
||||
|
||||
await _pumpNavigationRail(
|
||||
tester,
|
||||
navigationRail: NavigationRail(
|
||||
labelType: NavigationRailLabelType.all,
|
||||
selectedIndex: 0,
|
||||
destinations: const <NavigationRailDestination>[
|
||||
NavigationRailDestination(
|
||||
icon: Icon(Icons.favorite_border),
|
||||
selectedIcon: Icon(Icons.favorite),
|
||||
label: Text('Abc'),
|
||||
),
|
||||
NavigationRailDestination(
|
||||
icon: Icon(Icons.bookmark_border),
|
||||
selectedIcon: Icon(Icons.bookmark),
|
||||
label: Text('Def'),
|
||||
padding: secondItemPadding,
|
||||
),
|
||||
NavigationRailDestination(
|
||||
icon: Icon(Icons.star_border),
|
||||
selectedIcon: Icon(Icons.star),
|
||||
label: Text('Ghi'),
|
||||
padding: thirdItemPadding,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
final Padding firstItem = tester.widget<Padding>(find.widgetWithText(Padding, 'Abc'));
|
||||
final Padding secondItem = tester.widget<Padding>(find.widgetWithText(Padding, 'Def'));
|
||||
final Padding thirdItem = tester.widget<Padding>(find.widgetWithText(Padding, 'Ghi'));
|
||||
|
||||
expect(firstItem.padding, defaultPadding);
|
||||
expect(secondItem.padding, secondItemPadding);
|
||||
expect(thirdItem.padding, thirdItemPadding);
|
||||
});
|
||||
|
||||
testWidgets('NavigationRailDestination padding properly applied - NavigationRailLabelType.selected', (WidgetTester tester) async {
|
||||
const EdgeInsets defaultPadding = EdgeInsets.symmetric(horizontal: 8.0);
|
||||
const EdgeInsets secondItemPadding = EdgeInsets.symmetric(vertical: 30.0);
|
||||
const EdgeInsets thirdItemPadding = EdgeInsets.symmetric(horizontal: 10.0);
|
||||
|
||||
await _pumpNavigationRail(
|
||||
tester,
|
||||
navigationRail: NavigationRail(
|
||||
labelType: NavigationRailLabelType.selected,
|
||||
selectedIndex: 0,
|
||||
destinations: const <NavigationRailDestination>[
|
||||
NavigationRailDestination(
|
||||
icon: Icon(Icons.favorite_border),
|
||||
selectedIcon: Icon(Icons.favorite),
|
||||
label: Text('Abc'),
|
||||
),
|
||||
NavigationRailDestination(
|
||||
icon: Icon(Icons.bookmark_border),
|
||||
selectedIcon: Icon(Icons.bookmark),
|
||||
label: Text('Def'),
|
||||
padding: secondItemPadding,
|
||||
),
|
||||
NavigationRailDestination(
|
||||
icon: Icon(Icons.star_border),
|
||||
selectedIcon: Icon(Icons.star),
|
||||
label: Text('Ghi'),
|
||||
padding: thirdItemPadding,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
final Padding firstItem = tester.widget<Padding>(find.widgetWithText(Padding, 'Abc'));
|
||||
final Padding secondItem = tester.widget<Padding>(find.widgetWithText(Padding, 'Def'));
|
||||
final Padding thirdItem = tester.widget<Padding>(find.widgetWithText(Padding, 'Ghi'));
|
||||
|
||||
expect(firstItem.padding, defaultPadding);
|
||||
expect(secondItem.padding, secondItemPadding);
|
||||
expect(thirdItem.padding, thirdItemPadding);
|
||||
});
|
||||
|
||||
testWidgets('NavigationRailDestination padding properly applied - NavigationRailLabelType.none', (WidgetTester tester) async {
|
||||
const EdgeInsets defaultPadding = EdgeInsets.zero;
|
||||
const EdgeInsets secondItemPadding = EdgeInsets.symmetric(vertical: 30.0);
|
||||
const EdgeInsets thirdItemPadding = EdgeInsets.symmetric(horizontal: 10.0);
|
||||
|
||||
await _pumpNavigationRail(
|
||||
tester,
|
||||
navigationRail: NavigationRail(
|
||||
labelType: NavigationRailLabelType.none,
|
||||
selectedIndex: 0,
|
||||
destinations: const <NavigationRailDestination>[
|
||||
NavigationRailDestination(
|
||||
icon: Icon(Icons.favorite_border),
|
||||
selectedIcon: Icon(Icons.favorite),
|
||||
label: Text('Abc'),
|
||||
),
|
||||
NavigationRailDestination(
|
||||
icon: Icon(Icons.bookmark_border),
|
||||
selectedIcon: Icon(Icons.bookmark),
|
||||
label: Text('Def'),
|
||||
padding: secondItemPadding,
|
||||
),
|
||||
NavigationRailDestination(
|
||||
icon: Icon(Icons.star_border),
|
||||
selectedIcon: Icon(Icons.star),
|
||||
label: Text('Ghi'),
|
||||
padding: thirdItemPadding,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
final Padding firstItem = tester.widget<Padding>(find.widgetWithText(Padding, 'Abc'));
|
||||
final Padding secondItem = tester.widget<Padding>(find.widgetWithText(Padding, 'Def'));
|
||||
final Padding thirdItem = tester.widget<Padding>(find.widgetWithText(Padding, 'Ghi'));
|
||||
|
||||
expect(firstItem.padding, defaultPadding);
|
||||
expect(secondItem.padding, secondItemPadding);
|
||||
expect(thirdItem.padding, thirdItemPadding);
|
||||
});
|
||||
}
|
||||
|
||||
TestSemantics _expectedSemantics() {
|
||||
|
||||
Reference in New Issue
Block a user