From c5f5601c4a704238de9acc89c9e568f76dafab5d Mon Sep 17 00:00:00 2001 From: Ricardo Dalarme Date: Mon, 16 Jan 2023 12:24:04 -0300 Subject: [PATCH] feat(type-safe): make all callbacks type-safe --- CHANGELOG.md | 4 ++++ lib/src/card_swiper.dart | 23 ++++++++++------------- lib/src/typedefs.dart | 6 ++++++ 3 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 lib/src/typedefs.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d3806f..c14c872 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [1.0.2] + +- Make all callbacks type-safe + ## [1.0.1] - Fix problem with the back card not being rendered diff --git a/lib/src/card_swiper.dart b/lib/src/card_swiper.dart index 30ef09e..6b8f946 100644 --- a/lib/src/card_swiper.dart +++ b/lib/src/card_swiper.dart @@ -3,6 +3,7 @@ import 'dart:math'; import 'package:flutter/material.dart'; import 'package:flutter_card_swiper/src/card_swiper_controller.dart'; import 'package:flutter_card_swiper/src/enums.dart'; +import 'package:flutter_card_swiper/src/typedefs.dart'; class CardSwiper extends StatefulWidget { /// list of widgets for the swiper @@ -27,13 +28,13 @@ class CardSwiper extends StatefulWidget { final bool isDisabled; /// function that gets called with the new index and detected swipe direction when the user swiped or swipe is triggered by controller - final Function onSwipe; + final CardSwiperOnSwipe? onSwipe; /// function that gets called when there is no widget left to be swiped away - final Function onEnd; + final CardSwiperOnEnd? onEnd; /// function that gets triggered when the swiper is disabled - final Function onTapDisabled; + final CardSwiperOnTapDisabled? onTapDisabled; /// direction in which the card gets swiped when triggered by controller, default set to right final CardSwiperDirection direction; @@ -47,9 +48,9 @@ class CardSwiper extends StatefulWidget { this.maxAngle = 30, this.threshold = 50, this.isDisabled = false, - this.onTapDisabled = emptyFunction, - this.onSwipe = emptyFunctionIndex, - this.onEnd = emptyFunction, + this.onTapDisabled, + this.onSwipe, + this.onEnd, this.direction = CardSwiperDirection.right, }) : assert(maxAngle >= 0 && maxAngle <= 360), assert(threshold >= 1 && threshold <= 100), @@ -161,10 +162,10 @@ class _CardSwiperState extends State if (status == AnimationStatus.completed) { setState(() { if (_swipeTyp == SwipeType.swipe) { - widget.onSwipe(_currentIndex, detectedDirection); + widget.onSwipe?.call(_currentIndex, detectedDirection); if (_isLastCard) { - widget.onEnd(); + widget.onEnd?.call(); _currentIndex = 0; } else { _currentIndex++; @@ -225,7 +226,7 @@ class _CardSwiperState extends State ), onTap: () { if (widget.isDisabled) { - widget.onTapDisabled(); + widget.onTapDisabled?.call(); } }, onPanStart: (tapInfo) { @@ -397,7 +398,3 @@ class _CardSwiperState extends State }); } } - -//for null safety -void emptyFunction() {} -void emptyFunctionIndex(int index, CardSwiperDirection direction) {} diff --git a/lib/src/typedefs.dart b/lib/src/typedefs.dart new file mode 100644 index 0000000..d022e77 --- /dev/null +++ b/lib/src/typedefs.dart @@ -0,0 +1,6 @@ +import 'package:flutter_card_swiper/src/enums.dart'; + +typedef CardSwiperOnSwipe = void Function( + int index, CardSwiperDirection direction); +typedef CardSwiperOnEnd = void Function(); +typedef CardSwiperOnTapDisabled = void Function();