我想洗掉云 Firestore 中的檔案,而不是直接洗掉它我想首先使用顫振中的對話框顯示確認,該對話框將確認或取消檔案的洗掉。
通過單擊洗掉圖示,將顯示對話框以便用戶選擇命令,因為在我當前的代碼中,它將直接洗掉 firebase 中的檔案。
class _TabPage3State extends State<TabPage3> {
final Stream<QuerySnapshot> driver =
FirebaseFirestore.instance.collection('driversInfo').snapshots();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Center(
child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
Expanded(
child: StreamBuilder(
stream: driver,
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Somethinng went wrong!');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text('loading...');
}
final data = snapshot.requireData;
return ListView.builder(
itemCount: data.size,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(10.0),
child: Container(
padding: EdgeInsets.all(15),
decoration: BoxDecoration(
color: bgColor,
borderRadius: BorderRadius.circular(12),
),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Column(
children: [
// Instead of direct deleting the document in firebase, I want to show a dialog box first to confirm and cancel
IconButton(
onPressed: () {
FirebaseFirestore.instance
.collection("driversInfo")
.doc(
'${data.docs[index]['plate number']}')
.delete();
},
icon: Icon(
Icons.delete,
color: red,
),
),
],
),
],
),
],
),
),
);
},
);
})),
]),
),
),
);
}
}
uj5u.com熱心網友回復:
只需將您的 onPressed 更新為:
onPressed: () async {
final result = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Are you sure?'),
content: const Text('This action will permanently delete this data'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text('Cancel'),
),
TextButton(
onPressed: () => Navigator.pop(context, true),
child: const Text('Delete'),
),
],
),
);
if (result == null || !result) {
return;
}
FirebaseFirestore.instance
.collection('driversInfo')
.doc(
'${data.docs[index]['plate number']}',
)
.delete();
}
這將顯示一個 AlertDialog 供用戶確認,如果用戶按下“洗掉”,它將回傳 true ( Navigator.pop(context, true))。如果用戶按下背景或“取消”,它將回傳 null(背景)或 false(取消)。
uj5u.com熱心網友回復:
您可以將洗掉方法放在與AlertDialog連接的onPressed按鈕內。
showAlertDialog(BuildContext 背景關系) {
// set up the button
Widget okButton = TextButton(
child: Text("OK"),
onPressed: () { },
);
// set up the AlertDialog
AlertDialog alert = AlertDialog(
title: Text("My title"),
content: Text("This is my message."),
actions: [
okButton,
],
);
// show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/512886.html
標籤:扑镖
下一篇:如何在顫動中制作自定義微調器?
