Dismissible widget:只在点击按钮时才会关闭widget



我试图使用disdismissible小部件,仅在按下按钮时才被解雇,而不是通过滑动。是否有任何方法来实现这一点,或者是不可能做到与Dismissible小部件(或者也许我将不得不实现我自己的自定义动画或类似的东西)?

对于如何实现这一点有什么想法吗?

我尝试将方向设置更改为解散方向。无,以防止滑动。这是可行的,但我不知道如何在按下按钮时让它消失。我希望Dismissible有某种类型的控制器参数,但是它没有。

class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);

@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Dismissible(
key: UniqueKey(),
direction: DismissDirection.none, // This prevents it from being swiped
child: Container(
width: 300.0,
height: 100.0,
color: Colors.blue,
)
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {}, // I want this to somehow dismiss the Dismissible widget when this is pressed
),
);
}
}

(我在这里看到了一个类似的问题,但答案并没有解决我的问题,因为我并没有试图实现一个类似于屏幕上显示的通知的小部件。)我试图驳回一个小部件,是存在于我的应用程序的身体。)

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),
),
direction: DismissDirection.endToStart,
confirmDismiss: (direction) => showDialog(
context: context,
builder: ((context) => AlertDialog(
title: Text('Are you sure?'),
content: Text('Do you want to remove item from the cart'),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(false),
child: Text('No')),
TextButton(
onPressed: () => Navigator.of(context).pop(true),
child: Text('Yes'))
],
)),
),
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'),
),
),
),
);

this might help, there's a on dismissed method which you need to apply,and inside the action list, you will get two button i.e yes or no, where you can perform your action.

相关内容

最新更新