feat(type-safe): make all callbacks type-safe

This commit is contained in:
Ricardo Dalarme 2023-01-16 12:24:04 -03:00
parent 941b68ad7d
commit c5f5601c4a
3 changed files with 20 additions and 13 deletions

View File

@ -1,3 +1,7 @@
## [1.0.2]
- Make all callbacks type-safe
## [1.0.1] ## [1.0.1]
- Fix problem with the back card not being rendered - Fix problem with the back card not being rendered

View File

@ -3,6 +3,7 @@ import 'dart:math';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_card_swiper/src/card_swiper_controller.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/enums.dart';
import 'package:flutter_card_swiper/src/typedefs.dart';
class CardSwiper extends StatefulWidget { class CardSwiper extends StatefulWidget {
/// list of widgets for the swiper /// list of widgets for the swiper
@ -27,13 +28,13 @@ class CardSwiper extends StatefulWidget {
final bool isDisabled; 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 /// 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 /// 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 /// 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 /// direction in which the card gets swiped when triggered by controller, default set to right
final CardSwiperDirection direction; final CardSwiperDirection direction;
@ -47,9 +48,9 @@ class CardSwiper extends StatefulWidget {
this.maxAngle = 30, this.maxAngle = 30,
this.threshold = 50, this.threshold = 50,
this.isDisabled = false, this.isDisabled = false,
this.onTapDisabled = emptyFunction, this.onTapDisabled,
this.onSwipe = emptyFunctionIndex, this.onSwipe,
this.onEnd = emptyFunction, this.onEnd,
this.direction = CardSwiperDirection.right, this.direction = CardSwiperDirection.right,
}) : assert(maxAngle >= 0 && maxAngle <= 360), }) : assert(maxAngle >= 0 && maxAngle <= 360),
assert(threshold >= 1 && threshold <= 100), assert(threshold >= 1 && threshold <= 100),
@ -161,10 +162,10 @@ class _CardSwiperState extends State<CardSwiper>
if (status == AnimationStatus.completed) { if (status == AnimationStatus.completed) {
setState(() { setState(() {
if (_swipeTyp == SwipeType.swipe) { if (_swipeTyp == SwipeType.swipe) {
widget.onSwipe(_currentIndex, detectedDirection); widget.onSwipe?.call(_currentIndex, detectedDirection);
if (_isLastCard) { if (_isLastCard) {
widget.onEnd(); widget.onEnd?.call();
_currentIndex = 0; _currentIndex = 0;
} else { } else {
_currentIndex++; _currentIndex++;
@ -225,7 +226,7 @@ class _CardSwiperState extends State<CardSwiper>
), ),
onTap: () { onTap: () {
if (widget.isDisabled) { if (widget.isDisabled) {
widget.onTapDisabled(); widget.onTapDisabled?.call();
} }
}, },
onPanStart: (tapInfo) { onPanStart: (tapInfo) {
@ -397,7 +398,3 @@ class _CardSwiperState extends State<CardSwiper>
}); });
} }
} }
//for null safety
void emptyFunction() {}
void emptyFunctionIndex(int index, CardSwiperDirection direction) {}

6
lib/src/typedefs.dart Normal file
View File

@ -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();