@@ -462,33 +462,25 @@ class _NavigationDestinationBuilder extends StatelessWidget {
462
462
final _NavigationDestinationInfo info = _NavigationDestinationInfo .of (context);
463
463
final NavigationBarThemeData navigationBarTheme = NavigationBarTheme .of (context);
464
464
final NavigationBarThemeData defaults = _defaultsFor (context);
465
+ final GlobalKey labelKey = GlobalKey ();
465
466
466
467
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
- }
479
468
return _NavigationBarDestinationSemantics (
480
469
child: _NavigationBarDestinationTooltip (
481
470
message: tooltip ?? label,
482
471
child: _IndicatorInkWell (
483
472
key: UniqueKey (),
484
- labelPadding: labelPadding,
473
+ labelKey: labelKey,
474
+ labelBehavior: info.labelBehavior,
475
+ selected: selected,
485
476
customBorder: navigationBarTheme.indicatorShape ?? defaults.indicatorShape,
486
477
onTap: info.onTap,
487
478
child: Row (
488
479
children: < Widget > [
489
480
Expanded (
490
481
child: _NavigationBarDestinationLayout (
491
482
icon: buildIcon (context),
483
+ labelKey: labelKey,
492
484
label: buildLabel (context),
493
485
),
494
486
),
@@ -503,7 +495,9 @@ class _NavigationDestinationBuilder extends StatelessWidget {
503
495
class _IndicatorInkWell extends InkResponse {
504
496
const _IndicatorInkWell ({
505
497
super .key,
506
- required this .labelPadding,
498
+ required this .labelKey,
499
+ required this .labelBehavior,
500
+ required this .selected,
507
501
super .customBorder,
508
502
super .onTap,
509
503
super .child,
@@ -512,10 +506,26 @@ class _IndicatorInkWell extends InkResponse {
512
506
highlightColor: Colors .transparent,
513
507
);
514
508
515
- final double labelPadding;
509
+ final GlobalKey labelKey;
510
+ final NavigationDestinationLabelBehavior labelBehavior;
511
+ final bool selected;
516
512
517
513
@override
518
514
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
+ }
519
529
final double indicatorOffsetX = referenceBox.size.width / 2 ;
520
530
final double indicatorOffsetY = referenceBox.size.height / 2 - labelPadding;
521
531
@@ -765,6 +775,7 @@ class _NavigationBarDestinationLayout extends StatelessWidget {
765
775
/// 3 [NavigationBar] .
766
776
const _NavigationBarDestinationLayout ({
767
777
required this .icon,
778
+ required this .labelKey,
768
779
required this .label,
769
780
});
770
781
@@ -773,6 +784,11 @@ class _NavigationBarDestinationLayout extends StatelessWidget {
773
784
/// See [NavigationDestination.icon] .
774
785
final Widget icon;
775
786
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
+
776
792
/// The label widget that sits below the icon.
777
793
///
778
794
/// This widget will sometimes be faded out, depending on
@@ -782,7 +798,6 @@ class _NavigationBarDestinationLayout extends StatelessWidget {
782
798
final Widget label;
783
799
784
800
static final Key _iconKey = UniqueKey ();
785
- static final Key _labelKey = UniqueKey ();
786
801
787
802
@override
788
803
Widget build (BuildContext context) {
@@ -806,7 +821,7 @@ class _NavigationBarDestinationLayout extends StatelessWidget {
806
821
alwaysIncludeSemantics: true ,
807
822
opacity: animation,
808
823
child: RepaintBoundary (
809
- key: _labelKey ,
824
+ key: labelKey ,
810
825
child: label,
811
826
),
812
827
),
0 commit comments