Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/components/bottom_sheet/gf_bottom_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class GFBottomSheetController extends ValueNotifier<bool> {

// ignore: unnecessary_getters_setters
double? get height => _height;
bool? get isBottomSheetOpened => value;
bool get isBottomSheetOpened => value;
void hideBottomSheet() => value = false;
void showBottomSheet() => value = true;
}
75 changes: 37 additions & 38 deletions lib/components/carousel/gf_carousel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class GFCarousel extends StatefulWidget {
final num viewportFraction;

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

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

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

/// The actual index of the [PageView].
int realPage = 10000;
int? currentSlide;

@override
void initState() {
super.initState();
realPage = widget.enableInfiniteScroll
// ignore: avoid_as
? realPage + (widget.initialPage as int)
? realPage + widget.initialPage
// ignore: avoid_as
: widget.initialPage as int;
: widget.initialPage;
pageController = PageController(
// ignore: avoid_as
viewportFraction: widget.viewportFraction as double,
initialPage: widget.enableInfiniteScroll
// ignore: avoid_as
? realPage + (widget.initialPage as int)
? realPage + widget.initialPage
// ignore: avoid_as
: widget.initialPage as int,
: widget.initialPage,
);
timer = getPlayTimer();
}
Expand Down Expand Up @@ -189,8 +190,6 @@ class _GFCarouselState extends State<GFCarousel> with TickerProviderStateMixin {
setState(() => currentSlide = index);
}

int? currentSlide;

@override
Widget build(BuildContext context) => Stack(
children: <Widget>[
Expand All @@ -206,25 +205,23 @@ class _GFCarouselState extends State<GFCarousel> with TickerProviderStateMixin {
: widget.items.length,
onPageChanged: (int index) {
int currentPage;
// ignore: avoid_as
currentPage = _getRealIndex(index + (widget.initialPage as int),
realPage, widget.items.length);
currentPage = _getRealIndex(
index + widget.initialPage, realPage, widget.items.length);
if (widget.onPageChanged != null) {
widget.onPageChanged!(currentPage);
}
if (widget.pagination == true && widget.onPageChanged == null) {
if (widget.pagination == true) {
onPageSlide(currentPage);
}
},
itemBuilder: (BuildContext context, int i) {
final int index = _getRealIndex(
// ignore: avoid_as
i + (widget.initialPage as int),
int index;
index = _getRealIndex(
i + widget.initialPage,
realPage,
widget.items.length,
);

currentSlide = index;
currentSlide = widget.initialPage;
return AnimatedBuilder(
animation: pageController,
child: widget.items[index],
Expand Down Expand Up @@ -283,27 +280,29 @@ class _GFCarouselState extends State<GFCarousel> with TickerProviderStateMixin {
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: widget.map<Widget>(
widget.items,
(pagerIndex, url) => Container(
width:
widget.pagerSize == null ? 8.0 : widget.pagerSize,
height:
widget.pagerSize == null ? 8.0 : widget.pagerSize,
margin: const EdgeInsets.symmetric(
vertical: 10, horizontal: 2),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: currentSlide == pagerIndex
? widget.activeIndicator == null
? const Color.fromRGBO(0, 0, 0, 0.9)
: widget.activeIndicator!
: widget.passiveIndicator == null
? const Color.fromRGBO(0, 0, 0, 0.4)
: widget.passiveIndicator!,
widget.items,
(pagerIndex, url) => Container(
width: widget.pagerSize == null
? 8.0
: widget.pagerSize,
height: widget.pagerSize == null
? 8.0
: widget.pagerSize,
margin: const EdgeInsets.symmetric(
vertical: 10, horizontal: 2),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: currentSlide == pagerIndex
? widget.activeIndicator == null
? const Color.fromRGBO(0, 0, 0, 0.9)
: widget.activeIndicator!
: widget.passiveIndicator == null
? const Color.fromRGBO(0, 0, 0, 0.4)
: widget.passiveIndicator!,
),
)
// ignore: avoid_as
),
),
// ignore: avoid_as
) as List<Widget>,
),
),
)
Expand Down
14 changes: 7 additions & 7 deletions lib/components/progress_bar/gf_progress_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,12 @@ class _GFProgressBarState extends State<GFProgressBar>
@override
Widget build(BuildContext context) {
super.build(context);
final item = <Widget?>[];

final item = List<Widget>.empty(growable: true);
if (widget.leading != null) {
item.add(widget.leading);
item.add(widget.leading!);
}

final hasSetWidth = widget.width != null;
final containerWidget = Container(
margin: const EdgeInsets.only(left: 10, right: 10),
Expand Down Expand Up @@ -271,7 +273,7 @@ class _GFProgressBarState extends State<GFProgressBar>
));
}
if (widget.trailing != null) {
item.add(widget.trailing);
item.add(widget.trailing!);
}
return widget.type == GFProgressType.linear
? Material(
Expand All @@ -281,8 +283,7 @@ class _GFProgressBarState extends State<GFProgressBar>
child: Row(
mainAxisAlignment: widget.alignment,
crossAxisAlignment: CrossAxisAlignment.center,
// ignore: avoid_as
children: item as List<Widget>,
children: item,
)),
)
: Material(
Expand All @@ -292,8 +293,7 @@ class _GFProgressBarState extends State<GFProgressBar>
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
// ignore: avoid_as
children: item as List<Widget>,
children: item,
)),
);
}
Expand Down
87 changes: 46 additions & 41 deletions lib/components/tabs/gf_tabbar_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,15 @@ class GFTabBarView extends StatefulWidget {
_GFTabBarViewState createState() => _GFTabBarViewState();
}

final PageScrollPhysics _kGFTabBarViewPhysics =
const PageScrollPhysics().applyTo(const ClampingScrollPhysics());

class _GFTabBarViewState extends State<GFTabBarView> {
TabController? _controller;
PageController? _pageController;
List<Widget>? _children;
List<Widget>? _childrenWithKey;
late PageController _pageController;
late List<Widget> _children;
late List<Widget> _childrenWithKey;
int? _currentIndex;
int _warpUnderwayCount = 0;

// If the GFTabBarView is rebuilt with a new tab controller, the caller should
// If the TabBarView is rebuilt with a new tab controller, the caller should
// dispose the old one. In that case the old controller's animation will be
// null and should not be accessed.
bool get _controllerIsValid => _controller?.animation != null;
Expand All @@ -74,15 +71,17 @@ class _GFTabBarViewState extends State<GFTabBarView> {
}
return true;
}());

if (newController == _controller) {
return;
}

if (_controllerIsValid) {
_controller?.animation?.removeListener(_handleTabControllerAnimationTick);
_controller!.animation!.removeListener(_handleTabControllerAnimationTick);
}
_controller = newController;
if (_controller != null) {
_controller?.animation?.addListener(_handleTabControllerAnimationTick);
_controller!.animation!.addListener(_handleTabControllerAnimationTick);
}
}

Expand Down Expand Up @@ -114,7 +113,7 @@ class _GFTabBarViewState extends State<GFTabBarView> {
@override
void dispose() {
if (_controllerIsValid) {
_controller?.animation?.removeListener(_handleTabControllerAnimationTick);
_controller!.animation!.removeListener(_handleTabControllerAnimationTick);
}
_controller = null;
// We don't own the _controller Animation, so it's not disposed here.
Expand All @@ -127,47 +126,50 @@ class _GFTabBarViewState extends State<GFTabBarView> {
}

void _handleTabControllerAnimationTick() {
if (_controller != null) {
if (_warpUnderwayCount > 0 || !_controller!.indexIsChanging) {
return;
} // This widget is driving the controller's animation.
if (_controller!.index != _currentIndex) {
_currentIndex = _controller!.index;
_warpToCurrentIndex();
}
if (_warpUnderwayCount > 0 || !_controller!.indexIsChanging) {
return;
} // This widget is driving the controller's animation.

if (_controller!.index != _currentIndex) {
_currentIndex = _controller!.index;
_warpToCurrentIndex();
}
}

Future<void> _warpToCurrentIndex() async {
if (!mounted || _pageController == null || _currentIndex == null) {
if (!mounted) {
return Future<void>.value();
}

if (_pageController!.page == _currentIndex!.toDouble()) {
if (_pageController.page == _currentIndex!.toDouble()) {
return Future<void>.value();
}

final int previousIndex = _controller!.previousIndex;
if ((_currentIndex! - previousIndex).abs() == 1) {
return _pageController?.animateToPage(_currentIndex!,
_warpUnderwayCount += 1;
await _pageController.animateToPage(_currentIndex!,
duration: kTabScrollDuration, curve: Curves.ease);
_warpUnderwayCount -= 1;
return Future<void>.value();
}

assert((_currentIndex! - previousIndex).abs() > 1);
final int initialPage = _currentIndex! > previousIndex
? _currentIndex! - 1
: _currentIndex! + 1;
final List<Widget>? originalChildren = _childrenWithKey;
final List<Widget> originalChildren = _childrenWithKey;
setState(() {
_warpUnderwayCount += 1;
_childrenWithKey = List<Widget>.from(_childrenWithKey!, growable: false);
final Widget temp = _childrenWithKey![initialPage];
_childrenWithKey![initialPage] = _childrenWithKey![previousIndex];
_childrenWithKey![previousIndex] = temp;

_childrenWithKey = List<Widget>.from(_childrenWithKey, growable: false);
final Widget temp = _childrenWithKey[initialPage];
_childrenWithKey[initialPage] = _childrenWithKey[previousIndex];
_childrenWithKey[previousIndex] = temp;
});
_pageController?.jumpToPage(initialPage);
_pageController.jumpToPage(initialPage);

await _pageController?.animateToPage(_currentIndex!,
await _pageController.animateToPage(_currentIndex!,
duration: kTabScrollDuration, curve: Curves.ease);
if (!mounted) {
return Future<void>.value();
Expand All @@ -187,30 +189,30 @@ class _GFTabBarViewState extends State<GFTabBarView> {
if (_warpUnderwayCount > 0) {
return false;
}

if (notification.depth != 0) {
return false;
}
if (_controller == null ||
_pageController == null ||
_pageController?.page != null ||
_controller?.index == null) {
return false;
}

_warpUnderwayCount += 1;
if (notification is ScrollUpdateNotification &&
!_controller!.indexIsChanging) {
if ((_pageController!.page! - _controller!.index).abs() > 1.0) {
_controller!.index = _pageController!.page!.floor();
if ((_pageController.page! - _controller!.index).abs() > 1.0) {
_controller!.index = _pageController.page!.floor();
_currentIndex = _controller!.index;
}
_controller!.offset =
(_pageController!.page! - _controller!.index).clamp(-1.0, 1.0);
(_pageController.page! - _controller!.index).clamp(-1.0, 1.0);
} else if (notification is ScrollEndNotification) {
_controller!.index = _pageController!.page!.round();
_controller!.index = _pageController.page!.round();
_currentIndex = _controller!.index;
if (!_controller!.indexIsChanging) {
_controller!.offset =
(_pageController.page! - _controller!.index).clamp(-1.0, 1.0);
}
}
_warpUnderwayCount -= 1;

return false;
}

Expand All @@ -231,10 +233,13 @@ class _GFTabBarViewState extends State<GFTabBarView> {
child: PageView(
dragStartBehavior: widget.dragStartBehavior,
controller: _pageController,
// physics: widget.physics == null
// ? _kGFTabBarViewPhysics
// : _kGFTabBarViewPhysics.applyTo(widget.physics),
physics: widget.physics == null
? _kGFTabBarViewPhysics
: _kGFTabBarViewPhysics.applyTo(widget.physics),
children: _childrenWithKey!,
? const PageScrollPhysics().applyTo(const ClampingScrollPhysics())
: const PageScrollPhysics().applyTo(widget.physics),
children: _childrenWithKey,
),
),
);
Expand Down
Loading