我正在使用 GetX。我正在顯示兩個對話框。一個在下,一個在上。當我點擊頂部對話框上的按鈕時,我想關閉底部對話框,怎么辦?請查看以下代碼:
Get.dialog(
Container(
width: double.infinity,
height: double.infinity,
alignment: Alignment.center,
child: Text("Bottom Dialog")),
name: "dialog1");
Future.delayed(Duration(seconds: 1), () {
Get.dialog(
Container(
width: double.infinity,
height: double.infinity,
alignment: Alignment.topRight,
child: GestureDetector(
onTap: () {
print("close");
Get.removeRoute(GetPageRoute(routeName: "dialog1"));
// Get.key.currentState!.removeRoute(GetPageRoute(routeName: "dialog1"));
},
child: Text("Top Dialog"))),
navigatorKey: Get.key);
});
uj5u.com熱心網友回復:
您可以使用以下方法關閉按鈕:
Navigator.pop(context);
實作為:
showDialog(
context: context,
builder: (_) {
return AlertDialog(
title: Text('Wanna Exit?'),
actions: [
FlatButton(
onPressed: () => Navigator.pop(context, false), // passing false
child: Text('No'),
),
FlatButton(
onPressed: () => Navigator.pop(context, true), // passing true
child: Text('Yes'),
),
],
);
}).then((exit) {
if (exit == null) return;
if (exit) {
// user pressed Yes button
} else {
// user pressed No button
}
});
uj5u.com熱心網友回復:
同時顯示兩個對話并不是好的 UI/UX 體驗。相反,您可以在打開頂部對話框后立即關閉底部對話框。
要重新打開底部的對話再次在頂部的對話被關閉,那么你可以使用回傳值從上面的對話使用
bool showFirstDialogue = await Get.dialog(DialogueTwo());
if(showFirstDialogue){
// show first dialogue again here
}
在這種情況下,請確保在關閉時回傳bool值DialogueTwo。
你可以用
Get.back();
上面的代碼片段不需要任何背景關系。
確保匯入獲取庫
import 'package:get/get.dart';
要獲得更清晰的代碼版本只是為了關閉打開的對話框,那么您可以檢查以下條件:
if(Get.isDialogueOpen){
Get.back();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/388549.html
