@@ -519,26 +519,35 @@ class _TabBarScrollPosition extends ScrollPositionWithSingleContext {
519519
520520 final _TabBarState tabBar;
521521
522- bool ? _initialViewportDimensionWasZero;
522+ bool _viewportDimensionWasNonZero = false ;
523+
524+ // Position should be adjusted at least once.
525+ bool _needsPixelsCorrection = true ;
523526
524527 @override
525528 bool applyContentDimensions (double minScrollExtent, double maxScrollExtent) {
526529 bool result = true ;
527- if (_initialViewportDimensionWasZero != true ) {
528- // If the viewport never had a non-zero dimension, we just want to jump
529- // to the initial scroll position to avoid strange scrolling effects in
530- // release mode: In release mode, the viewport temporarily may have a
531- // dimension of zero before the actual dimension is calculated. In that
532- // scenario, setting the actual dimension would cause a strange scroll
533- // effect without this guard because the super call below would starts a
534- // ballistic scroll activity.
535- assert (viewportDimension != null );
536- _initialViewportDimensionWasZero = viewportDimension != 0.0 ;
530+ if (! _viewportDimensionWasNonZero) {
531+ _viewportDimensionWasNonZero = viewportDimension != 0.0 ;
532+ }
533+ // If the viewport never had a non-zero dimension, we just want to jump
534+ // to the initial scroll position to avoid strange scrolling effects in
535+ // release mode: In release mode, the viewport temporarily may have a
536+ // dimension of zero before the actual dimension is calculated. In that
537+ // scenario, setting the actual dimension would cause a strange scroll
538+ // effect without this guard because the super call below would starts a
539+ // ballistic scroll activity.
540+ if (! _viewportDimensionWasNonZero || _needsPixelsCorrection) {
541+ _needsPixelsCorrection = false ;
537542 correctPixels (tabBar._initialScrollOffset (viewportDimension, minScrollExtent, maxScrollExtent));
538543 result = false ;
539544 }
540545 return super .applyContentDimensions (minScrollExtent, maxScrollExtent) && result;
541546 }
547+
548+ void markNeedsPixelsCorrection () {
549+ _needsPixelsCorrection = true ;
550+ }
542551}
543552
544553// This class, and TabBarScrollPosition, only exist to handle the case
@@ -1027,6 +1036,13 @@ class _TabBarState extends State<TabBar> {
10271036 if (widget.controller != oldWidget.controller) {
10281037 _updateTabController ();
10291038 _initIndicatorPainter ();
1039+ // Adjust scroll position.
1040+ if (_scrollController != null ) {
1041+ final ScrollPosition position = _scrollController! .position;
1042+ if (position is _TabBarScrollPosition ) {
1043+ position.markNeedsPixelsCorrection ();
1044+ }
1045+ }
10301046 } else if (widget.indicatorColor != oldWidget.indicatorColor ||
10311047 widget.indicatorWeight != oldWidget.indicatorWeight ||
10321048 widget.indicatorSize != oldWidget.indicatorSize ||
0 commit comments