【问题标题】:How to achieve swipe right or left in flutter如何在颤动中实现向右或向左滑动
【发布时间】:2020-01-29 20:00:01
【问题描述】:

我正在制作一个待办事项列表应用程序。我想实现向右滑动删除和向左滑动标记,就像某些电子邮件应用程序一样。

我知道Dismissible Widget可以实现滑动删除,secondaryBackground可以做其他方式滑动。但是当我滑动到其他方式时我不怎么调用其他函数。

return Dismissible(
          // Each Dismissible must contain a Key. Keys allow Flutter to
          // uniquely identify widgets.
          key: Key(item),
          // Provide a function that tells the app
          // what to do after an item has been swiped away.
          onDismissed: (direction) {
            // Remove the item from the data source.
            setState(() {
              items.removeAt(index);
            });
            // Then show a snackbar.
            Scaffold.of(context)
                .showSnackBar(SnackBar(content: Text("$item dismissed")));
          },
          // Show a red background as the item is swiped away.
          background: Container(color: Colors.red,child: Icon(Icons.cancel),),
          secondaryBackground: Container(color: Colors.green,child: Icon(Icons.check),),
          child: ListTile(title: Text('$item')),
        );

【问题讨论】:

  • 你能解释一下“当我滑动到其他方式时如何调用其他函数”是什么意思。
  • 例如,在这段代码中,当我向左滑动(secondaryBackground)时,我想将此事件添加到日历(由另一个函数实现)

标签: flutter flutter-widget


【解决方案1】:

确定您滑动的方向

onDismissed: (direction) {

   if(direction == DismissDirection.startToEnd) { // Right Swipe

        setState(() {
          items.removeAt(index);
        });

        Scaffold.of(context).showSnackBar(SnackBar(content: Text("$item dismissed")));

   } else if(direction == DismissDirection.endToStart) {//Left Swipe
        //add event to Calendar 
   }
 },

【讨论】:

  • 我可以让 Dismissible 小部件在direction == DismissDirection.endToStart 时不会被删除
猜你喜欢
  • 1970-01-01
  • 2020-12-21
  • 2011-03-29
  • 1970-01-01
  • 1970-01-01
  • 2011-10-02
  • 2019-12-17
  • 1970-01-01
  • 2019-04-16
相关资源
最近更新 更多