@@ -45,18 +45,15 @@ class GFTabBarView extends StatefulWidget {
45
45
_GFTabBarViewState createState () => _GFTabBarViewState ();
46
46
}
47
47
48
- final PageScrollPhysics _kGFTabBarViewPhysics =
49
- const PageScrollPhysics ().applyTo (const ClampingScrollPhysics ());
50
-
51
48
class _GFTabBarViewState extends State <GFTabBarView > {
52
49
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;
56
53
int ? _currentIndex;
57
54
int _warpUnderwayCount = 0 ;
58
55
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
60
57
// dispose the old one. In that case the old controller's animation will be
61
58
// null and should not be accessed.
62
59
bool get _controllerIsValid => _controller? .animation != null ;
@@ -74,15 +71,17 @@ class _GFTabBarViewState extends State<GFTabBarView> {
74
71
}
75
72
return true ;
76
73
}());
74
+
77
75
if (newController == _controller) {
78
76
return ;
79
77
}
78
+
80
79
if (_controllerIsValid) {
81
- _controller? .animation? .removeListener (_handleTabControllerAnimationTick);
80
+ _controller! .animation! .removeListener (_handleTabControllerAnimationTick);
82
81
}
83
82
_controller = newController;
84
83
if (_controller != null ) {
85
- _controller? .animation? .addListener (_handleTabControllerAnimationTick);
84
+ _controller! .animation! .addListener (_handleTabControllerAnimationTick);
86
85
}
87
86
}
88
87
@@ -114,7 +113,7 @@ class _GFTabBarViewState extends State<GFTabBarView> {
114
113
@override
115
114
void dispose () {
116
115
if (_controllerIsValid) {
117
- _controller? .animation? .removeListener (_handleTabControllerAnimationTick);
116
+ _controller! .animation! .removeListener (_handleTabControllerAnimationTick);
118
117
}
119
118
_controller = null ;
120
119
// We don't own the _controller Animation, so it's not disposed here.
@@ -127,47 +126,50 @@ class _GFTabBarViewState extends State<GFTabBarView> {
127
126
}
128
127
129
128
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 ();
138
136
}
139
137
}
140
138
141
139
Future <void > _warpToCurrentIndex () async {
142
- if (! mounted || _pageController == null || _currentIndex == null ) {
140
+ if (! mounted) {
143
141
return Future <void >.value ();
144
142
}
145
143
146
- if (_pageController! .page == _currentIndex! .toDouble ()) {
144
+ if (_pageController.page == _currentIndex! .toDouble ()) {
147
145
return Future <void >.value ();
148
146
}
149
147
150
148
final int previousIndex = _controller! .previousIndex;
151
149
if ((_currentIndex! - previousIndex).abs () == 1 ) {
152
- return _pageController? .animateToPage (_currentIndex! ,
150
+ _warpUnderwayCount += 1 ;
151
+ await _pageController.animateToPage (_currentIndex! ,
153
152
duration: kTabScrollDuration, curve: Curves .ease);
153
+ _warpUnderwayCount -= 1 ;
154
+ return Future <void >.value ();
154
155
}
155
156
156
157
assert ((_currentIndex! - previousIndex).abs () > 1 );
157
158
final int initialPage = _currentIndex! > previousIndex
158
159
? _currentIndex! - 1
159
160
: _currentIndex! + 1 ;
160
- final List <Widget >? originalChildren = _childrenWithKey;
161
+ final List <Widget > originalChildren = _childrenWithKey;
161
162
setState (() {
162
163
_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;
167
169
});
168
- _pageController? .jumpToPage (initialPage);
170
+ _pageController.jumpToPage (initialPage);
169
171
170
- await _pageController? .animateToPage (_currentIndex! ,
172
+ await _pageController.animateToPage (_currentIndex! ,
171
173
duration: kTabScrollDuration, curve: Curves .ease);
172
174
if (! mounted) {
173
175
return Future <void >.value ();
@@ -187,30 +189,30 @@ class _GFTabBarViewState extends State<GFTabBarView> {
187
189
if (_warpUnderwayCount > 0 ) {
188
190
return false ;
189
191
}
192
+
190
193
if (notification.depth != 0 ) {
191
194
return false ;
192
195
}
193
- if (_controller == null ||
194
- _pageController == null ||
195
- _pageController? .page != null ||
196
- _controller? .index == null ) {
197
- return false ;
198
- }
199
196
200
197
_warpUnderwayCount += 1 ;
201
198
if (notification is ScrollUpdateNotification &&
202
199
! _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 ();
205
202
_currentIndex = _controller! .index;
206
203
}
207
204
_controller! .offset =
208
- (_pageController! .page! - _controller! .index).clamp (- 1.0 , 1.0 );
205
+ (_pageController.page! - _controller! .index).clamp (- 1.0 , 1.0 );
209
206
} else if (notification is ScrollEndNotification ) {
210
- _controller! .index = _pageController! .page! .round ();
207
+ _controller! .index = _pageController.page! .round ();
211
208
_currentIndex = _controller! .index;
209
+ if (! _controller! .indexIsChanging) {
210
+ _controller! .offset =
211
+ (_pageController.page! - _controller! .index).clamp (- 1.0 , 1.0 );
212
+ }
212
213
}
213
214
_warpUnderwayCount -= 1 ;
215
+
214
216
return false ;
215
217
}
216
218
@@ -231,10 +233,13 @@ class _GFTabBarViewState extends State<GFTabBarView> {
231
233
child: PageView (
232
234
dragStartBehavior: widget.dragStartBehavior,
233
235
controller: _pageController,
236
+ // physics: widget.physics == null
237
+ // ? _kGFTabBarViewPhysics
238
+ // : _kGFTabBarViewPhysics.applyTo(widget.physics),
234
239
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,
238
243
),
239
244
),
240
245
);
0 commit comments