dart 如何从另一个文件调用alertdialog?使用flutter

lsmepo6l  于 2024-01-03  发布在  Flutter
关注(0)|答案(2)|浏览(130)

如何从另一个dart文件调用alert对话框,当用户单击 addstudents.dart 中的按钮时,我想在另一个文件中创建alert对话框,以防万一它可以重用?在我的addstudents.dart中我有这个容器,请看下面的代码,谢谢。
addstudents.dart

  1. Container(
  2. padding: EdgeInsets.only(top: 10),
  3. width: (globals.screenWidth * 0.48),
  4. height: (globals.screenHeight * 0.10),
  5. decoration:
  6. BoxDecoration(borderRadius: BorderRadius.circular(10)),
  7. child: RaisedButton(
  8. shape: new RoundedRectangleBorder(
  9. borderRadius: new BorderRadius.circular(10.0),
  10. ),
  11. onPressed: () {
  12. // calling another file dart
  13. },
  14. color: Colors.green,
  15. child: RichText(
  16. text: TextSpan(
  17. text: "Confirmed!",
  18. style: TextStyle(
  19. fontWeight: FontWeight.bold,
  20. fontFamily: "Nunito",
  21. color: Colors.white,
  22. fontSize: globals.fontsize_19)),
  23. ),
  24. ),
  25. ),

字符串
这是我的alertdialog.dart

  1. void main() => runApp(MyApp());
  2. class MyApp extends StatelessWidget {
  3. @override
  4. Widget build(BuildContext context) {
  5. return MaterialApp(
  6. title: 'Flutter',
  7. home: Scaffold(
  8. appBar: AppBar(
  9. title: Text('Flutter'),
  10. ),
  11. body: MyLayout()),
  12. );
  13. }
  14. }
  15. class MyLayout extends StatelessWidget {
  16. @override
  17. Widget build(BuildContext context) {
  18. return Padding(
  19. padding: const EdgeInsets.all(8.0),
  20. child: RaisedButton(
  21. child: Text('Show alert'),
  22. onPressed: () {
  23. showAlertDialog(context);
  24. },
  25. ),
  26. );
  27. }
  28. }
  29. showAlertDialog(BuildContext context) {
  30. Widget okButton = FlatButton(
  31. child: Text("OK"),
  32. onPressed: () {},
  33. );
  34. AlertDialog alert = AlertDialog(
  35. title: Text("My title"),
  36. content: Text("This is my message."),
  37. actions: [
  38. okButton,
  39. ],
  40. );
  41. showDialog(
  42. context: context,
  43. builder: (BuildContext context) {
  44. return alert;
  45. },
  46. );
  47. }

pn9klfpd

pn9klfpd1#

不知道这是不是最好的做法,但我正在做的是

  1. class Alerts {
  2. static showAlertDialog(BuildContext context) {
  3. Widget okButton = FlatButton(
  4. child: Text("OK"),
  5. onPressed: () {},
  6. );
  7. AlertDialog alert = AlertDialog(
  8. title: Text("My title"),
  9. content: Text("This is my message."),
  10. actions: [
  11. okButton,
  12. ],
  13. );
  14. // show the dialog
  15. showDialog(
  16. context: context,
  17. builder: (BuildContext context) {
  18. return alert;
  19. },
  20. );
  21. }
  22. }

字符串
我使用**Alerts.showAlertDialog(context)**调用它们;

展开查看全部
tzxcd3kk

tzxcd3kk2#

Dean给出的答案是我见过的最简单的,也是最容易理解的。我用可变的标题和消息编辑了Dean的代码,以防其他像我这样的新手需要帮助。

  1. import 'package:flutter/material.dart';
  2. class Alerts{
  3. static showSimpleAlert(BuildContext context, title, message) {
  4. Widget okButton = TextButton(
  5. child: Text("OK"),
  6. onPressed: () {
  7. Navigator.of(context).pop();
  8. },
  9. );
  10. AlertDialog alert = AlertDialog(
  11. title: Text(title),
  12. content: Text(message),
  13. actions: [
  14. okButton,
  15. ],
  16. );
  17. // show the dialog
  18. showDialog(
  19. context: context,
  20. builder: (BuildContext context) {
  21. return alert;
  22. },
  23. );
  24. }
  25. }

字符串
然后打电话给它,

  1. Alerts.showSimpleAlert(context, "Sign-in Failed", "Please check your credentials used");

展开查看全部

相关问题