1

我试图在另一个文件中显示一个对话框,StatefullWidget但是当我调用它的函数时什么都没有发生。

我想这样做的原因是因为我的代码中有太多的代码嵌套,所以我想让事情保持简单和干净。

下面是dialog.dart文件。

import 'package:flutter/material.dart';

class PersonDetailsDialog extends StatefulWidget {
  PersonDetailsDialog({Key key}) : super(key: key);

  @override
  _PersonDetailsDialogState createState() {
    return _PersonDetailsDialogState();
  }
}

class _PersonDetailsDialogState extends State<PersonDetailsDialog> {
  @override
  Widget build(BuildContext context) {
    Future<void> _neverSatisfied() async {
      return showDialog<void>(
        context: context,
        barrierDismissible: false, // user must tap button!
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text('Rewind and remember'),
            content: SingleChildScrollView(
              child: ListBody(
                children: <Widget>[
                  Text('You will never be satisfied.'),
                  Text('You\’re like me. I’m never satisfied.'),
                ],
              ),
            ),
            actions: <Widget>[
              FlatButton(
                child: Text('Regret'),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
            ],
          );
        },
      );
    }
  }
}

下面是main.dart文件。

mport 'package:flutter/material.dart';
import 'package:practical_0/homepage.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
          primarySwatch: Colors.blue
      ),
      home: Homepage(),
    );
  }
}

下面是homepage.dart文件,我试图在用户单击时显示对话框,RaisedButton但没有任何反应。

import 'package:flutter/material.dart';

class Homepage extends StatelessWidget {

  final double heightFactor = 600/896;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: RaisedButton(
          onPressed: PersonDetailsDialog(), // show dialog
        ),
      ),
    );
  }
}
4

2 回答 2

4

您必须在要显示对话框的位置使用ShowDialog 。

我希望下面的例子能澄清你的想法。

class Delete extends StatefulWidget {
  @override
  _DeleteState createState() => _DeleteState();
}

class _DeleteState extends State<Delete> {
  BuildContext parent, child;

  @override
  Widget build(BuildContext context) => Scaffold(
        body: Container(
          child: Center(
            child: RaisedButton(
              onPressed: () {
                showDialog(
                    context: context,
                    barrierDismissible: false,
                    child: PersonDetailsDialog());
              }, // show dialog
            ),
          ),
        ),
      );
}

class PersonDetailsDialog extends StatefulWidget {
  PersonDetailsDialog({Key key}) : super(key: key);

  @override
  _PersonDetailsDialogState createState() {
    return _PersonDetailsDialogState();
  }
}

class _PersonDetailsDialogState extends State<PersonDetailsDialog> {
  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text('Rewind and remember'),
      content: SingleChildScrollView(
        child: ListBody(
          children: <Widget>[
            Text('You will never be satisfied.'),
            Text('You\’re like me. I’m never satisfied.'),
          ],
        ),
      ),
      actions: <Widget>[
        FlatButton(
          child: Text('Regret'),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
      ],
    );
  }
}
于 2020-01-05T08:28:20.830 回答
4

这是一个例子:

显示对话框是一个异步函数

child: RaisedButton(
              onPressed: () async{
              final result = await showDialog(
                context: context,
                builder: (_) => AlertWidget(),
              );
              return result;
            },
于 2020-01-05T08:33:21.093 回答