Skip to content

Commit 4bef5bf

Browse files
Merge pull request #227 from deepikahr/animation_test
Animation test
2 parents 11235ef + ca5407b commit 4bef5bf

File tree

5 files changed

+295
-291
lines changed

5 files changed

+295
-291
lines changed

lib/components/bottom_sheet/gf_bottom_sheet.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ class GFBottomSheetController extends ValueNotifier<bool> {
258258

259259
// ignore: unnecessary_getters_setters
260260
double? get height => _height;
261-
bool? get isBottomSheetOpened => value;
261+
bool get isBottomSheetOpened => value;
262262
void hideBottomSheet() => value = false;
263263
void showBottomSheet() => value = true;
264264
}

lib/components/carousel/gf_carousel.dart

+37-38
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class GFCarousel extends StatefulWidget {
5252
final num viewportFraction;
5353

5454
/// The initial page to show when first creating the [GFCarousel]. Defaults to 0.
55-
final num initialPage;
55+
final int initialPage;
5656

5757
/// Determines if slides should loop infinitely or be limited to item length. Defaults to true, i.e. infinite loop.
5858
final bool enableInfiniteScroll;
@@ -97,8 +97,8 @@ class GFCarousel extends StatefulWidget {
9797
/// Defaults to matching platform conventions.
9898
final ScrollPhysics? scrollPhysics;
9999

100-
List<T?> map<T>(List list, Function handler) {
101-
List<T?> result;
100+
List<T> map<T>(List list, Function handler) {
101+
List<T> result;
102102
result = [];
103103
for (var i = 0; i < list.length; i++) {
104104
result.add(handler(i, list[i]));
@@ -125,23 +125,24 @@ class _GFCarouselState extends State<GFCarousel> with TickerProviderStateMixin {
125125

126126
/// The actual index of the [PageView].
127127
int realPage = 10000;
128+
int? currentSlide;
128129

129130
@override
130131
void initState() {
131132
super.initState();
132133
realPage = widget.enableInfiniteScroll
133134
// ignore: avoid_as
134-
? realPage + (widget.initialPage as int)
135+
? realPage + widget.initialPage
135136
// ignore: avoid_as
136-
: widget.initialPage as int;
137+
: widget.initialPage;
137138
pageController = PageController(
138139
// ignore: avoid_as
139140
viewportFraction: widget.viewportFraction as double,
140141
initialPage: widget.enableInfiniteScroll
141142
// ignore: avoid_as
142-
? realPage + (widget.initialPage as int)
143+
? realPage + widget.initialPage
143144
// ignore: avoid_as
144-
: widget.initialPage as int,
145+
: widget.initialPage,
145146
);
146147
timer = getPlayTimer();
147148
}
@@ -189,8 +190,6 @@ class _GFCarouselState extends State<GFCarousel> with TickerProviderStateMixin {
189190
setState(() => currentSlide = index);
190191
}
191192

192-
int? currentSlide;
193-
194193
@override
195194
Widget build(BuildContext context) => Stack(
196195
children: <Widget>[
@@ -206,25 +205,23 @@ class _GFCarouselState extends State<GFCarousel> with TickerProviderStateMixin {
206205
: widget.items.length,
207206
onPageChanged: (int index) {
208207
int currentPage;
209-
// ignore: avoid_as
210-
currentPage = _getRealIndex(index + (widget.initialPage as int),
211-
realPage, widget.items.length);
208+
currentPage = _getRealIndex(
209+
index + widget.initialPage, realPage, widget.items.length);
212210
if (widget.onPageChanged != null) {
213211
widget.onPageChanged!(currentPage);
214212
}
215-
if (widget.pagination == true && widget.onPageChanged == null) {
213+
if (widget.pagination == true) {
216214
onPageSlide(currentPage);
217215
}
218216
},
219217
itemBuilder: (BuildContext context, int i) {
220-
final int index = _getRealIndex(
221-
// ignore: avoid_as
222-
i + (widget.initialPage as int),
218+
int index;
219+
index = _getRealIndex(
220+
i + widget.initialPage,
223221
realPage,
224222
widget.items.length,
225223
);
226-
227-
currentSlide = index;
224+
currentSlide = widget.initialPage;
228225
return AnimatedBuilder(
229226
animation: pageController,
230227
child: widget.items[index],
@@ -283,27 +280,29 @@ class _GFCarouselState extends State<GFCarousel> with TickerProviderStateMixin {
283280
child: Row(
284281
mainAxisAlignment: MainAxisAlignment.center,
285282
children: widget.map<Widget>(
286-
widget.items,
287-
(pagerIndex, url) => Container(
288-
width:
289-
widget.pagerSize == null ? 8.0 : widget.pagerSize,
290-
height:
291-
widget.pagerSize == null ? 8.0 : widget.pagerSize,
292-
margin: const EdgeInsets.symmetric(
293-
vertical: 10, horizontal: 2),
294-
decoration: BoxDecoration(
295-
shape: BoxShape.circle,
296-
color: currentSlide == pagerIndex
297-
? widget.activeIndicator == null
298-
? const Color.fromRGBO(0, 0, 0, 0.9)
299-
: widget.activeIndicator!
300-
: widget.passiveIndicator == null
301-
? const Color.fromRGBO(0, 0, 0, 0.4)
302-
: widget.passiveIndicator!,
283+
widget.items,
284+
(pagerIndex, url) => Container(
285+
width: widget.pagerSize == null
286+
? 8.0
287+
: widget.pagerSize,
288+
height: widget.pagerSize == null
289+
? 8.0
290+
: widget.pagerSize,
291+
margin: const EdgeInsets.symmetric(
292+
vertical: 10, horizontal: 2),
293+
decoration: BoxDecoration(
294+
shape: BoxShape.circle,
295+
color: currentSlide == pagerIndex
296+
? widget.activeIndicator == null
297+
? const Color.fromRGBO(0, 0, 0, 0.9)
298+
: widget.activeIndicator!
299+
: widget.passiveIndicator == null
300+
? const Color.fromRGBO(0, 0, 0, 0.4)
301+
: widget.passiveIndicator!,
302+
),
303+
)
304+
// ignore: avoid_as
303305
),
304-
),
305-
// ignore: avoid_as
306-
) as List<Widget>,
307306
),
308307
),
309308
)

lib/components/progress_bar/gf_progress_bar.dart

+7-7
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,12 @@ class _GFProgressBarState extends State<GFProgressBar>
220220
@override
221221
Widget build(BuildContext context) {
222222
super.build(context);
223-
final item = <Widget?>[];
223+
224+
final item = List<Widget>.empty(growable: true);
224225
if (widget.leading != null) {
225-
item.add(widget.leading);
226+
item.add(widget.leading!);
226227
}
228+
227229
final hasSetWidth = widget.width != null;
228230
final containerWidget = Container(
229231
margin: const EdgeInsets.only(left: 10, right: 10),
@@ -271,7 +273,7 @@ class _GFProgressBarState extends State<GFProgressBar>
271273
));
272274
}
273275
if (widget.trailing != null) {
274-
item.add(widget.trailing);
276+
item.add(widget.trailing!);
275277
}
276278
return widget.type == GFProgressType.linear
277279
? Material(
@@ -281,8 +283,7 @@ class _GFProgressBarState extends State<GFProgressBar>
281283
child: Row(
282284
mainAxisAlignment: widget.alignment,
283285
crossAxisAlignment: CrossAxisAlignment.center,
284-
// ignore: avoid_as
285-
children: item as List<Widget>,
286+
children: item,
286287
)),
287288
)
288289
: Material(
@@ -292,8 +293,7 @@ class _GFProgressBarState extends State<GFProgressBar>
292293
child: Column(
293294
crossAxisAlignment: CrossAxisAlignment.center,
294295
mainAxisAlignment: MainAxisAlignment.spaceBetween,
295-
// ignore: avoid_as
296-
children: item as List<Widget>,
296+
children: item,
297297
)),
298298
);
299299
}

lib/components/tabs/gf_tabbar_view.dart

+46-41
Original file line numberDiff line numberDiff line change
@@ -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-
5148
class _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

Comments
 (0)