Flutter showDialog无法在简单测试中使用

问题描述 投票:0回答:1

我正在尝试颤振,在进行简单的showdialog工作时遇到问题。我用一个按钮尝试了一个简单的测试:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter Test',
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.teal,
          title: Text('Flutter'),
        ),
        body: Center(
          child: ListView(
            padding: EdgeInsets.all(8),
            children: <Widget>[
              Container(
                child: RaisedButton(
                  child: Text('My Button'),
                  onPressed: () => {
                    showDialog(
                      context: context,
                      barrierDismissible: false,
                      builder: (context) {
                        return AlertDialog(
                          title: Text('Test'),
                          content: Text('Dialog content'),
                        );
                      },
                    ),
                  },
                  color: Colors.cyan,
                  textColor: Colors.white,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

我希望在点击按钮时弹出警报。我想念什么?我也通过单独的自定义函数调用中的showdialog进行了尝试,结果相同。

flutter showdialog
1个回答
0
投票

您需要使用Flutter提供的showDialog方法,如示例here所示。使用按钮,但使用showDialog方法,查看下面的示例:

class DialogIssue extends StatefulWidget {
  @override
  _DialogIssueState createState() => _DialogIssueState();
}

class _DialogIssueState extends State<DialogIssue> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: RaisedButton(
        child: Text('My Button'),
        onPressed: () => _confirmDialog(),
        color: Colors.cyan,
        textColor: Colors.white,
      ),
    );
  }

  Future<void> _confirmDialog() async {
    switch (await showDialog<bool>(
        context: context,
        builder: (BuildContext context) {
          return SimpleDialog(
            title: const Text('True or false'),
            children: <Widget>[
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  SimpleDialogOption(
                    onPressed: () { Navigator.pop(context, true); },
                    child: const Text('Confirm',
                      style: TextStyle(fontWeight: FontWeight.bold),
                    ),
                  ),
                  SimpleDialogOption(
                    onPressed: () { Navigator.pop(context, false); },
                    child: const Text('Cancel'),
                  ),
                ],
              ),
            ],
          );
        }
    )){
      case true:
        print('Confirmed');
        break;

      case false:
        print('Canceled');
        break;

      default:
        print('Canceled');
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.