【发布时间】:2020-08-29 17:04:42
【问题描述】:
我正在创建一个应用程序,它从存储库中获取人员,然后将它们显示在屏幕上。
用户应该能够通过向右滑动来从存储库中删除此人。当用户向右滑动confirmDismiss: 属性时,会弹出对话框询问用户是否确定。
对话框中的content: 是否可以是该人的姓名。因此,如果有人想从列表中删除 Nicolas Cage,它会说“您确定要删除 Nicolas Cage 吗?”。
这是 Person 类的实现方式:
class Person {
int _id;
String _fullName;
String _email;
String _mobile;
String _other;
Person(int id, String fullName, String email, String mobile, String other) {
this._id = id;
this._fullName = fullName;
this._email = email;
this._mobile = mobile;
this._other = other;
}
}
这就是 Dismissible confirmDissmis 的实现方式:
confirmDismiss: (direction) async {
bool response = await showDialog<bool>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: const Text('Are you sure you want to delete '),
actions: <Widget>[
FlatButton(
child: const Text('Yes'),
onPressed: () => Navigator.pop(context, true),
),
FlatButton(
child: const Text('No'),
onPressed: () => Navigator.pop(context, false),
),
],
);
},
);
},
我希望在 AlertDialog 内容中显示“您确定要删除 Nicolas Cage 吗?”如果我们假设用户在 Nicolas Cage List Tile 上向右滑动。我尝试过的这种方法不起作用。它说这个常量表达式的求值抛出一个异常,无效的常量值。
【问题讨论】:
-
并非如此。我知道如何使用confirmDismiss,我不知道的是一旦用户向右滑动,如何在警报对话框中显示人员全名字段。
-
只需将
people[index].name与content: const Text('Are you sure you want to delete '),附加为content: const Text('Are you sure you want to delete ' + people[index].fullName), -
我还必须删除 Text 小部件前面的 const,因为 people[index].fullName 不是 const。它对我有用!你解决了我的问题。非常感谢!
-
如果可能的话,请将我的解决方案标记为已接受✔ 并支持 ⇧。享受。我在下面添加了我的答案。