Reputation: 1159
I want to disable swiping to the right once there are no data in DataBase.
if (data.length > 1) {
// I can swipe to the left or right
} else {
// I can't swipe to the right only left
}
Is it real to achieve this?
Perhaps, I can return an element to the center once one swiped it to the right
Solution: I just used confirmDismiss in Dismissible widget.
confirmDismiss: (direction) {
if(data.length > 0 && direction==..)
// do stuff
else if(...)
}
Upvotes: 12
Views: 16466
Reputation: 4060
Set the direction
property to DismissDirection.none
.
Example:
Dismissible(
direction: isEnabled
? DismissDirection.endToStart
: DismissDirection.none,
child: // Rest of code
}
Upvotes: 1
Reputation: 1169
Although Sergio's solution works when you have a simple scenario, in my case I had a complex tree below the Dismissible widget (which needed to accept other types of interactions).
Peeking @ flutter's source for Dismissible, I noticed we can set the property direction
to DismissDirection.none
to prevent swipes. e.g.:
Dismissible(
key: UniqueKey(),
direction: !_canSwipe ? DismissDirection.none : DismissDirection.horizontal,
(...)
Hope it helps!
Upvotes: 20
Reputation: 115
In case anyone wants to disable dismiss swiping altogether (left or right) you can use DismissDirection.none
*Note this parameter is currently only available on the beta channel
Dismissible(
direction: data.length > 1 ? DismissDirection.endToStart : DismissDirection.none,
)
Upvotes: 9
Reputation: 439
You can use direction property
Set it to DismissDirection.endToStart
or DismissDirection.startToEnd
Dismissible(
direction: DismissDirection.endToStart,
)
Upvotes: 14
Reputation: 57
This is a newly proposed api change, and until then unfortunately we have to reuse the code.
return isDisabled ?
CardWidgetAndCode(
...
)
: Dismissible(
child: CardWidgetAndCode( // <-- duplicate code and/or function call
...
)
)
Upvotes: -1
Reputation: 669
A late answer but you can set the direction
property of Dismissible
as direction: DismissDirection.endToStart,
or direction: DismissDirection.startToEnd,
Upvotes: 22
Reputation: 11219
Following tomblue's idea I came up with another idea which not only comes free of any error (both compilation and runtime) but also allows the developer to prevent swipe to the right or to the left:
I used the ternary with the properties in dismissThresholds set to impossible values. Full example below:
class SwiperState extends State<Swiper> {
bool _isNotFirstIndex = false;
bool _isNotLastIndex = true;
int _currentIndex = 0;
void nextOne() {
setState(() {
_isNotFirstIndex = !(_currentIndex - 1 < 0);
_isNotLastIndex = !(_currentIndex + 1 == count);
});
}
@override
Widget build(BuildContext context) {
return Dismissible(
dismissThresholds: {DismissDirection.startToEnd: _isNotFirstIndex ? 0.05 : 2.00,
DismissDirection.endToStart: _isNotLastIndex ? 0.05 : 2.00},
key: UniqueKey(),
onDismissed: (DismissDirection direction) {
if(direction == DismissDirection.endToStart) {
// dismissed to the left, next one
nextOne();
}
else (do the same with prevOne())
(...)
Upvotes: 2
Reputation: 2327
Wrap the dismissible widget with AbsorbPointer
. Then, whenever you don't want item to be dismissed, set absorbing
to true. Check this for more info link
Upvotes: 8