简体   繁体   English

如何在 Flutter 的 Dismissible 小部件中禁用向左或向右滑动?

[英]How to disable swiping left or right in Dismissible widget in Flutter?

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.解决方案:我只是在 Dismissible 小部件中使用了 confirmDismiss。

confirmDismiss: (direction) {
  if(data.length > 0 && direction==..)
  // do stuff
  else if(...)
}

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).虽然 Sergio 的解决方案在您有一个简单场景时有效,但在我的情况下,我在 Dismissible 小部件下方有一个复杂的树(它需要接受其他类型的交互)。

Peeking @ flutter's source for Dismissible , I noticed we can set the property direction to DismissDirection.none to prevent swipes.查看 @flutter 的 Dismissible ,我注意到我们可以将属性direction设置为DismissDirection.none以防止滑动。 eg:例如:

Dismissible(
   key: UniqueKey(),
   direction: !_canSwipe ? DismissDirection.none : DismissDirection.horizontal,
   (...)

Hope it helps!希望能帮助到你!

一个迟到的答案,但您可以将 Dismissible 的direction属性设置为方向: Dismissible direction: DismissDirection.endToStart,direction: DismissDirection.startToEnd,

You can use direction property您可以使用方向属性

Set it to DismissDirection.endToStart or DismissDirection.startToEnd将其设置为DismissDirection.endToStartDismissDirection.startToEnd

Dismissible(
    direction: DismissDirection.endToStart,
    )

Wrap the dismissible widget with AbsorbPointer .AbsorbPointer包裹可关闭的小部件。 Then, whenever you don't want item to be dismissed, set absorbing to true.然后,当您不想关闭项目时,将absorbing设置为 true。 Check this for more info link检查此以获取更多信息链接

In case anyone wants to disable dismiss swiping altogether (left or right) you can use DismissDirection.none如果有人想完全禁用关闭滑动(向左或向右),您可以使用 DismissDirection.none

*Note this parameter is currently only available on the beta channel *注意此参数目前仅在 beta 通道可用

Dismissible(
  direction: data.length > 1 ? DismissDirection.endToStart : DismissDirection.none,
)

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:按照 tomblue 的想法,我想出了另一个想法,它不仅没有任何错误(编译和运行时),而且还允许开发人员防止向右或向左滑动:

I used the ternary with the properties in dismissThresholds set to impossible values.我使用了三元组,将 dismissThresholds 中的属性设置为不可能的值。 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())
               (...)

This is a newly proposed api change, and until then unfortunately we have to reuse the code.这是一个新提议的 api 更改,不幸的是,在那之前我们必须重用代码。

return isDisabled ?
      CardWidgetAndCode(
        ...
      )
    : Dismissible(
      child: CardWidgetAndCode( //  <-- duplicate code and/or function call
        ...
      )
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM