英文:
Is there a better way than this to control dialog?
问题
我在屏幕加载时使用对话框。问题是,如果用户关闭对话框,然后触发对话框的弹出功能,它将弹出页面而不是对话框。(Navigator.pop(context))。所以我想到了一种使用全局键来解决的方法。现在函数会检测全局键是否已挂载,然后再弹出。我想知道是否有更好、更简洁的方法。
我在对话框弹出时使用Navigator.pop(context)。但如果用户在关闭对话框之前关闭它本身,那么它会弹出页面而不是对话框。
这是对话框的函数,我正在使用全局键,并且这是如何弹出它的方式:
void loadingDialog() {
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return BackdropFilter(
filter: ImageFilter.blur(sigmaX: 10, sigmaY: 10),
child: AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
key: dashboardLoadingDialogKey,
backgroundColor: const Color(0xFF15000D),
content: Container(
padding: const EdgeInsets.all(15),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
const CircularProgressIndicator(
color: Colors.amber,
),
const SizedBox(height: 20),
Text(
'LOADING',
textAlign: TextAlign.center,
style: dialogAmber,
),
],
),
),
),
);
},
);
}
if (response.statusCode == 200 && mounted) {
// 对话框弹出后的逻辑
if (dashboardLoadingDialogKey.currentContext != null &&
dashboardLoadingDialogKey.currentContext!.mounted) {
Navigator.of(dashboardLoadingDialogKey.currentContext!).pop();
}
}
这是关于对话框的函数,我正在使用全局键,并且这是如何弹出它的方式。
英文:
I am using dialog when screen loads. The problem is that if user closes the dialog and after that the pop function for dialog is triggered, then it will pop the page instead of dialog. (Navigator.pop(context). So I came up with a solution to use global key for dialog. Now the function detects if global key is mounted then it pops else not. I want to know if there is any better and shorter method.
I am using Navigator.pop(context) for dialog to pop. But if user closes the dialog box before closing itself, then it pops the page instead of dialog box.
void loadingDialog() {
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return BackdropFilter(
filter: ImageFilter.blur(sigmaX: 10, sigmaY: 10),
child: AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
key: dashboardLoadingDialogKey,
backgroundColor: const Color(0xFF15000D),
content: Container(
padding: const EdgeInsets.all(15),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
const CircularProgressIndicator(
color: Colors.amber,
),
const SizedBox(height: 20),
Text(
'LOADING',
textAlign: TextAlign.center,
style: dialogAmber,
),
],
),
),
),
);
},
);
}
T
if (response.statusCode == 200 && mounted) {
// dashboard loading dialog pop
if (dashboardLoadingDialogKey.currentContext != null &&
dashboardLoadingDialogKey.currentContext!.mounted) {
Navigator.of(dashboardLoadingDialogKey.currentContext!).pop();
}
}
This is the function for dialog and I am using global key, and this is how I am popping it
答案1
得分: 1
Loading dialog shouldn't be closed/popped by the user. So, I would wrap my builder
widget with WillPopScope
as the following so that it can be popped only programmatically:
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return WillPopScope(
onWillPop: () async => false,
child: AlertDialog(
// ...
),
);
},
);
英文:
Loading dialog shouldn't be closed/popped by the user. So, I would wrap my builder
widget with WillPopScope
as the following so that it can be popped only programmatically:
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return WillPopScope(
onWillPop: () async => false,
child: AlertDialog(
// ...
),
);
},
);
答案2
得分: 0
我建议您不要通过 Navigator
来实现这个,而是直接使用 Overlay
或 Stack
,并在那里使用一个 bool
变量来指示是否显示叠加层。这种方法也与大多数状态管理系统更兼容,而不是手动推送和弹出对话框。对于用户来说,如果按照设计方式进行,它看起来是一样的。
英文:
I would recommend that you do not do this via the Navigator
, but simply use the Overlay
or a Stack
and simply use a bool
there to indicate whether the overlay should be displayed.
This approach is also more compatible with most state management systems than manually pushing and popping dialogs. For the user, it would look identical if it was designed accordingly.
答案3
得分: 0
它将弹出除基本第一个路由以外的所有其他路由。如果您正在使用显示对话框,则会非常有帮助。如果您使用了Navigator.pop(context)
,有时会因用户交互而弹出所有路由。
Navigator.popUntil(context, (route) => route.isFirst);
英文:
It will pop all other route except base first route. It will help a lot when if you are using show dialog. If you used Navigator.pop(context)
, sometimes all route will be pop because of user interaction.
Navigator.popUntil(context, (route) => route.isFirst);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论