【问题标题】:How to check whether a user swiped left or right(dismissible) flutter如何检查用户是否向左或向右滑动(可关闭)颤动
【发布时间】:2019-12-17 18:58:06
【问题描述】:

我在 Flutter 中使用了可关闭小部件。
我知道 ondismiss() 可以为我们提供有关用户向左或向右滑动的位置的信息,但该信息仅在元素从屏幕上关闭后才提供。我可以检测到用户是要向左还是向右滑动? 也许,我可以检查一个背景是否可见,我说的是背景和第二个背景

【问题讨论】:

标签: flutter dart


【解决方案1】:

您可以使用confirmDismiss 在关闭之前执行任何逻辑,如果您返回true,则必须返回true 或false 值,然后元素将被删除并在onDismissed 中执行操作。

Dismissible(
          key: ValueKey(id),
          background: Container(
            color: Theme.of(context).errorColor,
            child: Icon(
              Icons.delete,
              color: Colors.white,
              size: 40,
            ),
            alignment: Alignment.centerRight,
            padding: EdgeInsets.only(right: 20),
            margin: EdgeInsets.symmetric(
              horizontal: 15,
              vertical: 4,
            ),
          ),
          direction: DismissDirection.endToStart,
          confirmDismiss: (direction) {
            return showDialog(
              context: context,
              builder: (ctx) => AlertDialog(
                    title: Text('Are you sure?'),
                    content: Text(
                      'Do you want to remove the item from the cart?',
                    ),
                    actions: <Widget>[
                      FlatButton(
                        child: Text('No'),
                        onPressed: () {
                          Navigator.of(ctx).pop(false);
                        },
                      ),
                      FlatButton(
                        child: Text('Yes'),
                        onPressed: () {
                          Navigator.of(ctx).pop(true);
                        },
                      ),
                    ],
                  ),
            );
          },
          onDismissed: (direction) {
            Provider.of<Cart>(context, listen: false).removeItem(productId);
          },
          child: Card(
            margin: EdgeInsets.symmetric(
              horizontal: 15,
              vertical: 4,
            ),
            child: Padding(
              padding: EdgeInsets.all(8),
              child: ListTile(
                leading: CircleAvatar(
                  child: Padding(
                    padding: EdgeInsets.all(5),
                    child: FittedBox(
                      child: Text('\$$price'),
                    ),
                  ),
                ),
                title: Text(title),
                subtitle: Text('Total: \$${(price * quantity)}'),
                trailing: Text('$quantity x'),
              ),
            ),
          ),
        );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    • 1970-01-01
    • 2011-03-29
    • 2011-10-02
    • 1970-01-01
    • 2011-07-23
    • 2013-07-05
    相关资源
    最近更新 更多