@@ -45,18 +45,15 @@ class GFTabBarView extends StatefulWidget {
4545 _GFTabBarViewState createState () => _GFTabBarViewState ();
4646}
4747
48- final PageScrollPhysics _kGFTabBarViewPhysics =
49- const PageScrollPhysics ().applyTo (const ClampingScrollPhysics ());
50-
5148class _GFTabBarViewState extends State <GFTabBarView > {
5249 TabController ? _controller;
53- PageController ? _pageController;
54- List <Widget >? _children;
55- List <Widget >? _childrenWithKey;
50+ late PageController _pageController;
51+ late List <Widget > _children;
52+ late List <Widget > _childrenWithKey;
5653 int ? _currentIndex;
5754 int _warpUnderwayCount = 0 ;
5855
59- // If the GFTabBarView is rebuilt with a new tab controller, the caller should
56+ // If the TabBarView is rebuilt with a new tab controller, the caller should
6057 // dispose the old one. In that case the old controller's animation will be
6158 // null and should not be accessed.
6259 bool get _controllerIsValid => _controller? .animation != null ;
@@ -74,15 +71,17 @@ class _GFTabBarViewState extends State<GFTabBarView> {
7471 }
7572 return true ;
7673 }());
74+
7775 if (newController == _controller) {
7876 return ;
7977 }
78+
8079 if (_controllerIsValid) {
81- _controller? .animation? .removeListener (_handleTabControllerAnimationTick);
80+ _controller! .animation! .removeListener (_handleTabControllerAnimationTick);
8281 }
8382 _controller = newController;
8483 if (_controller != null ) {
85- _controller? .animation? .addListener (_handleTabControllerAnimationTick);
84+ _controller! .animation! .addListener (_handleTabControllerAnimationTick);
8685 }
8786 }
8887
@@ -114,7 +113,7 @@ class _GFTabBarViewState extends State<GFTabBarView> {
114113 @override
115114 void dispose () {
116115 if (_controllerIsValid) {
117- _controller? .animation? .removeListener (_handleTabControllerAnimationTick);
116+ _controller! .animation! .removeListener (_handleTabControllerAnimationTick);
118117 }
119118 _controller = null ;
120119 // We don't own the _controller Animation, so it's not disposed here.
@@ -127,47 +126,50 @@ class _GFTabBarViewState extends State<GFTabBarView> {
127126 }
128127
129128 void _handleTabControllerAnimationTick () {
130- if (_controller != null ) {
131- if (_warpUnderwayCount > 0 || ! _controller! .indexIsChanging) {
132- return ;
133- } // This widget is driving the controller's animation.
134- if (_controller! .index != _currentIndex) {
135- _currentIndex = _controller! .index;
136- _warpToCurrentIndex ();
137- }
129+ if (_warpUnderwayCount > 0 || ! _controller! .indexIsChanging) {
130+ return ;
131+ } // This widget is driving the controller's animation.
132+
133+ if (_controller! .index != _currentIndex) {
134+ _currentIndex = _controller! .index;
135+ _warpToCurrentIndex ();
138136 }
139137 }
140138
141139 Future <void > _warpToCurrentIndex () async {
142- if (! mounted || _pageController == null || _currentIndex == null ) {
140+ if (! mounted) {
143141 return Future <void >.value ();
144142 }
145143
146- if (_pageController! .page == _currentIndex! .toDouble ()) {
144+ if (_pageController.page == _currentIndex! .toDouble ()) {
147145 return Future <void >.value ();
148146 }
149147
150148 final int previousIndex = _controller! .previousIndex;
151149 if ((_currentIndex! - previousIndex).abs () == 1 ) {
152- return _pageController? .animateToPage (_currentIndex! ,
150+ _warpUnderwayCount += 1 ;
151+ await _pageController.animateToPage (_currentIndex! ,
153152 duration: kTabScrollDuration, curve: Curves .ease);
153+ _warpUnderwayCount -= 1 ;
154+ return Future <void >.value ();
154155 }
155156
156157 assert ((_currentIndex! - previousIndex).abs () > 1 );
157158 final int initialPage = _currentIndex! > previousIndex
158159 ? _currentIndex! - 1
159160 : _currentIndex! + 1 ;
160- final List <Widget >? originalChildren = _childrenWithKey;
161+ final List <Widget > originalChildren = _childrenWithKey;
161162 setState (() {
162163 _warpUnderwayCount += 1 ;
163- _childrenWithKey = List <Widget >.from (_childrenWithKey! , growable: false );
164- final Widget temp = _childrenWithKey! [initialPage];
165- _childrenWithKey! [initialPage] = _childrenWithKey! [previousIndex];
166- _childrenWithKey! [previousIndex] = temp;
164+
165+ _childrenWithKey = List <Widget >.from (_childrenWithKey, growable: false );
166+ final Widget temp = _childrenWithKey[initialPage];
167+ _childrenWithKey[initialPage] = _childrenWithKey[previousIndex];
168+ _childrenWithKey[previousIndex] = temp;
167169 });
168- _pageController? .jumpToPage (initialPage);
170+ _pageController.jumpToPage (initialPage);
169171
170- await _pageController? .animateToPage (_currentIndex! ,
172+ await _pageController.animateToPage (_currentIndex! ,
171173 duration: kTabScrollDuration, curve: Curves .ease);
172174 if (! mounted) {
173175 return Future <void >.value ();
@@ -187,30 +189,30 @@ class _GFTabBarViewState extends State<GFTabBarView> {
187189 if (_warpUnderwayCount > 0 ) {
188190 return false ;
189191 }
192+
190193 if (notification.depth != 0 ) {
191194 return false ;
192195 }
193- if (_controller == null ||
194- _pageController == null ||
195- _pageController? .page != null ||
196- _controller? .index == null ) {
197- return false ;
198- }
199196
200197 _warpUnderwayCount += 1 ;
201198 if (notification is ScrollUpdateNotification &&
202199 ! _controller! .indexIsChanging) {
203- if ((_pageController! .page! - _controller! .index).abs () > 1.0 ) {
204- _controller! .index = _pageController! .page! .floor ();
200+ if ((_pageController.page! - _controller! .index).abs () > 1.0 ) {
201+ _controller! .index = _pageController.page! .floor ();
205202 _currentIndex = _controller! .index;
206203 }
207204 _controller! .offset =
208- (_pageController! .page! - _controller! .index).clamp (- 1.0 , 1.0 );
205+ (_pageController.page! - _controller! .index).clamp (- 1.0 , 1.0 );
209206 } else if (notification is ScrollEndNotification ) {
210- _controller! .index = _pageController! .page! .round ();
207+ _controller! .index = _pageController.page! .round ();
211208 _currentIndex = _controller! .index;
209+ if (! _controller! .indexIsChanging) {
210+ _controller! .offset =
211+ (_pageController.page! - _controller! .index).clamp (- 1.0 , 1.0 );
212+ }
212213 }
213214 _warpUnderwayCount -= 1 ;
215+
214216 return false ;
215217 }
216218
@@ -231,10 +233,13 @@ class _GFTabBarViewState extends State<GFTabBarView> {
231233 child: PageView (
232234 dragStartBehavior: widget.dragStartBehavior,
233235 controller: _pageController,
236+ // physics: widget.physics == null
237+ // ? _kGFTabBarViewPhysics
238+ // : _kGFTabBarViewPhysics.applyTo(widget.physics),
234239 physics: widget.physics == null
235- ? _kGFTabBarViewPhysics
236- : _kGFTabBarViewPhysics .applyTo (widget.physics),
237- children: _childrenWithKey! ,
240+ ? const PageScrollPhysics (). applyTo ( const ClampingScrollPhysics ())
241+ : const PageScrollPhysics () .applyTo (widget.physics),
242+ children: _childrenWithKey,
238243 ),
239244 ),
240245 );
0 commit comments