颤抖:引发了另一个异常:未找到MaterialLocalizations

时间:2018-11-25 14:52:28

标签: flutter alertdialog

我试图在按Flutter中的一个按钮时显示一个警报对话框。 以下是我的代码

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

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MyAppState();
  }
}

class MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Different Widgets",
      debugShowCheckedModeBanner: false,
      home: showAlertDialog()
      );
  }

  void _dialogResult(String value) {
    if (value == "YES") {
      print("YES");
    } else {
      print("NO");
    }
    Navigator.pop(context);
  }

  Widget showAlertDialog() {
    TextEditingController textEditingController = TextEditingController();

    return Scaffold(
      appBar: AppBar(
        title: Text("Different Widgets"),
      ),
      body: Container(
        child: Center(
          child: Column(
            children: <Widget>[
              TextField(
                controller: textEditingController,
              ),
              RaisedButton(
                onPressed: () {
                  print("Hi");
                  AlertDialog dialog = AlertDialog(
                    title: Text("Hi"),
                    content: Text(
                      textEditingController.text,
                      style: TextStyle(fontSize: 30.0),
                    ),
                    actions: <Widget>[
                      FlatButton(
                          onPressed: () {
                            _dialogResult("YES");
                          },
                          child: Text("YES")),
                      FlatButton(
                          onPressed: () {
                            _dialogResult("NO");
                          },
                          child: Text("NO")),
                    ],
                  );

                  showDialog(context: context, builder: (BuildContext context) => dialog);
                },
                child: Text("Click Me"),
              )
            ],
          ),
        ),
      ),
    );
  }

这与本地化有什么关系,我不能理解。我做了与文档相同的步骤。我能够看到该按钮,但是单击该按钮后,我会不断收到错误消息。我尝试在按钮单击内编写打印语句,并且该打印语句出现在日志中,这肯定与AlertDialog有关。

0 个答案:

没有答案