Flutter:更改AlertDialog的宽度 [英] Flutter: change the width of an AlertDialog
问题描述
我不知道如何更改AlertDialog的默认宽度,我仅成功更改了边框半径:
I wonder how to change the default width of an AlertDialog, I only succeeded to change the border radius :
这是我的代码:
showDialog(
context: context,
builder: (_) =>
new AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0))
),
content: ProductPreviewScreen(),
)
);
预期结果:
有什么想法吗?
Any idea?
推荐答案
我认为另一个答案是有问题的,因为: / p>
I think the other answer is problematic because:
- 快速更新可能会破坏您的应用程序。
- 您可能会破坏应用程序内已经存在的对话框。
- 通过gitlab / github / etc与其他开发人员共享代码将是有问题的。
是我所做的:
第1步。
从 dialog.dart并将其粘贴到新文件中,例如:lib> custom_flutter> custom_dialog.dart。
Copy the code from 'dialog.dart' and paste this into a new file, e.g.: lib>custom_flutter>custom_dialog.dart.
第2步。
这会产生一些导入错误,删除那些导入,并添加以下行:
This will give some import errors, remove those imports, and add this line:
import 'package:flutter/material.dart';
第3步。
在新文件 custom_dialog.dart中找到对话框类。并替换:
Find the Dialog class, inside of your new file 'custom_dialog.dart'. And replace:
padding: MediaQuery.of(context).viewInsets + const EdgeInsets.symmetric(horizontal: 40.0, vertical: 24.0),
与此:
padding: MediaQuery.of(context).viewInsets + const EdgeInsets.symmetric(horizontal: 0.0, vertical: 24.0),
第4步。
现在无论您想在哪里使用此对话框,请添加以下导入声明:
Now wherever you want to use this dialog, add the following import statement:
import 'package:my_app_name/custom_flutter/custom_dialog.dart' as customDialog;
第5步。
如果要使用无限制对话框,请使用:
If you want to use the unrestricted dialog use:
customDialog.AlertDialog() //Unrestricted dialog
如果没有,只需继续使用常规值:
if not, just keep using the regular one:
AlertDialog() //Restricted dialog
这篇关于Flutter:更改AlertDialog的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!