@@ -462,33 +462,25 @@ class _NavigationDestinationBuilder extends StatelessWidget {
462462 final _NavigationDestinationInfo info = _NavigationDestinationInfo .of (context);
463463 final NavigationBarThemeData navigationBarTheme = NavigationBarTheme .of (context);
464464 final NavigationBarThemeData defaults = _defaultsFor (context);
465+ final GlobalKey labelKey = GlobalKey ();
465466
466467 final bool selected = info.selectedIndex == info.index;
467- final double labelPadding;
468- switch (info.labelBehavior) {
469- case NavigationDestinationLabelBehavior .alwaysShow:
470- labelPadding = 8 ;
471- break ;
472- case NavigationDestinationLabelBehavior .onlyShowSelected:
473- labelPadding = selected ? 8 : 0 ;
474- break ;
475- case NavigationDestinationLabelBehavior .alwaysHide:
476- labelPadding = 0 ;
477- break ;
478- }
479468 return _NavigationBarDestinationSemantics (
480469 child: _NavigationBarDestinationTooltip (
481470 message: tooltip ?? label,
482471 child: _IndicatorInkWell (
483472 key: UniqueKey (),
484- labelPadding: labelPadding,
473+ labelKey: labelKey,
474+ labelBehavior: info.labelBehavior,
475+ selected: selected,
485476 customBorder: navigationBarTheme.indicatorShape ?? defaults.indicatorShape,
486477 onTap: info.onTap,
487478 child: Row (
488479 children: < Widget > [
489480 Expanded (
490481 child: _NavigationBarDestinationLayout (
491482 icon: buildIcon (context),
483+ labelKey: labelKey,
492484 label: buildLabel (context),
493485 ),
494486 ),
@@ -503,7 +495,9 @@ class _NavigationDestinationBuilder extends StatelessWidget {
503495class _IndicatorInkWell extends InkResponse {
504496 const _IndicatorInkWell ({
505497 super .key,
506- required this .labelPadding,
498+ required this .labelKey,
499+ required this .labelBehavior,
500+ required this .selected,
507501 super .customBorder,
508502 super .onTap,
509503 super .child,
@@ -512,10 +506,26 @@ class _IndicatorInkWell extends InkResponse {
512506 highlightColor: Colors .transparent,
513507 );
514508
515- final double labelPadding;
509+ final GlobalKey labelKey;
510+ final NavigationDestinationLabelBehavior labelBehavior;
511+ final bool selected;
516512
517513 @override
518514 RectCallback ? getRectCallback (RenderBox referenceBox) {
515+ final RenderBox labelBox = labelKey.currentContext! .findRenderObject ()! as RenderBox ;
516+ final Rect labelRect = labelBox.localToGlobal (Offset .zero) & labelBox.size;
517+ final double labelPadding;
518+ switch (labelBehavior) {
519+ case NavigationDestinationLabelBehavior .alwaysShow:
520+ labelPadding = labelRect.height / 2 ;
521+ break ;
522+ case NavigationDestinationLabelBehavior .onlyShowSelected:
523+ labelPadding = selected ? labelRect.height / 2 : 0 ;
524+ break ;
525+ case NavigationDestinationLabelBehavior .alwaysHide:
526+ labelPadding = 0 ;
527+ break ;
528+ }
519529 final double indicatorOffsetX = referenceBox.size.width / 2 ;
520530 final double indicatorOffsetY = referenceBox.size.height / 2 - labelPadding;
521531
@@ -765,6 +775,7 @@ class _NavigationBarDestinationLayout extends StatelessWidget {
765775 /// 3 [NavigationBar] .
766776 const _NavigationBarDestinationLayout ({
767777 required this .icon,
778+ required this .labelKey,
768779 required this .label,
769780 });
770781
@@ -773,6 +784,11 @@ class _NavigationBarDestinationLayout extends StatelessWidget {
773784 /// See [NavigationDestination.icon] .
774785 final Widget icon;
775786
787+ /// The global key for the label of this destination.
788+ ///
789+ /// This is used to determine the position of the label relative to the icon.
790+ final GlobalKey labelKey;
791+
776792 /// The label widget that sits below the icon.
777793 ///
778794 /// This widget will sometimes be faded out, depending on
@@ -782,7 +798,6 @@ class _NavigationBarDestinationLayout extends StatelessWidget {
782798 final Widget label;
783799
784800 static final Key _iconKey = UniqueKey ();
785- static final Key _labelKey = UniqueKey ();
786801
787802 @override
788803 Widget build (BuildContext context) {
@@ -806,7 +821,7 @@ class _NavigationBarDestinationLayout extends StatelessWidget {
806821 alwaysIncludeSemantics: true ,
807822 opacity: animation,
808823 child: RepaintBoundary (
809- key: _labelKey ,
824+ key: labelKey ,
810825 child: label,
811826 ),
812827 ),
0 commit comments