我正在尝试显示位于另一个文件中的对话框StatefullWidget但是当我调用它的函数时什么也没有发生。
我想这样做的原因是因为我的代码中有太多代码嵌套,所以我想让事情保持简单和干净。
\n\n下面是dialog.dart文件。
\n\nimport \'package:flutter/material.dart\';\n\nclass PersonDetailsDialog extends StatefulWidget {\n PersonDetailsDialog({Key key}) : super(key: key);\n\n @override\n _PersonDetailsDialogState createState() {\n return _PersonDetailsDialogState();\n }\n}\n\nclass _PersonDetailsDialogState extends State<PersonDetailsDialog> {\n @override\n Widget build(BuildContext context) {\n Future<void> _neverSatisfied() async {\n return showDialog<void>(\n context: context,\n barrierDismissible: false, // user must tap button!\n builder: (BuildContext context) {\n return AlertDialog(\n title: Text(\'Rewind and remember\'),\n content: SingleChildScrollView(\n child: ListBody(\n children: <Widget>[\n Text(\'You will never be satisfied.\'),\n Text(\'You\\\xe2\x80\x99re like me. I\xe2\x80\x99m never satisfied.\'),\n ],\n ),\n ),\n actions: <Widget>[\n FlatButton(\n child: Text(\'Regret\'),\n onPressed: () {\n Navigator.of(context).pop();\n },\n ),\n ],\n );\n },\n );\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n下面是main.dart文件。
\n\nmport \'package:flutter/material.dart\';\nimport \'package:practical_0/homepage.dart\';\n\nvoid main() => runApp(MyApp());\n\nclass MyApp extends StatelessWidget {\n @override\n Widget build(BuildContext context) {\n return MaterialApp(\n title: \'Flutter Demo\',\n theme: ThemeData(\n primarySwatch: Colors.blue\n ),\n home: Homepage(),\n );\n }\n}\n\nRun Code Online (Sandbox Code Playgroud)\n\n下面是homepage.dart文件,我试图在用户单击时显示对话框,RaisedButton但没有任何反应。
import \'package:flutter/material.dart\';\n\nclass Homepage extends StatelessWidget {\n\n final double heightFactor = 600/896;\n\n @override\n Widget build(BuildContext context) {\n return Scaffold(\n body: Container(\n child: RaisedButton(\n onPressed: PersonDetailsDialog(), // show dialog\n ),\n ),\n );\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
您必须在要显示对话框的位置使用ShowDialog 。
\n\n我希望下面的例子能澄清你的想法。
\n\nclass Delete extends StatefulWidget {\n @override\n _DeleteState createState() => _DeleteState();\n}\n\nclass _DeleteState extends State<Delete> {\n BuildContext parent, child;\n\n @override\n Widget build(BuildContext context) => Scaffold(\n body: Container(\n child: Center(\n child: RaisedButton(\n onPressed: () {\n showDialog(\n context: context,\n barrierDismissible: false,\n child: PersonDetailsDialog());\n }, // show dialog\n ),\n ),\n ),\n );\n}\n\nclass PersonDetailsDialog extends StatefulWidget {\n PersonDetailsDialog({Key key}) : super(key: key);\n\n @override\n _PersonDetailsDialogState createState() {\n return _PersonDetailsDialogState();\n }\n}\n\nclass _PersonDetailsDialogState extends State<PersonDetailsDialog> {\n @override\n Widget build(BuildContext context) {\n return AlertDialog(\n title: Text(\'Rewind and remember\'),\n content: SingleChildScrollView(\n child: ListBody(\n children: <Widget>[\n Text(\'You will never be satisfied.\'),\n Text(\'You\\\xe2\x80\x99re like me. I\xe2\x80\x99m never satisfied.\'),\n ],\n ),\n ),\n actions: <Widget>[\n FlatButton(\n child: Text(\'Regret\'),\n onPressed: () {\n Navigator.of(context).pop();\n },\n ),\n ],\n );\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
小智 5
这是一个例子:
显示对话框是一个异步函数
child: RaisedButton(
onPressed: () async{
final result = await showDialog(
context: context,
builder: (_) => AlertWidget(),
);
return result;
},
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3592 次 |
| 最近记录: |