ShowDialog 在 Flutter 中不工作 Android

the ShowDialog is not Working in Flutter Android

我开始使用 Flutter。今天,我学习了如何编写 showDialog 代码,但它不起作用。我试过一次又一次地写它,但没有任何影响……这是我的代码:


import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(home: Scaffold(appBar: AppBar(), body: MyHome())));
}

class MyHome extends StatefulWidget {
  const MyHome({Key? key}) : super(key: key);

  @override
  State<MyHome> createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> {
  @override
  void MyDialog() {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text("title"),
            content: Text("Thanks to clock"),
            actions: <Widget>[
              TextButton(
                onPressed: () {},
                child: Text("close"),
              )
            ],
          );
        });
  }

  Widget build(BuildContext context) {
    return TextButton(
      onPressed: () {},
      child: Text("Click On me!"),
    );
  }
}

请帮帮我!谢谢...

您没有在代码中的任何地方调用 MyDialog

更新代码:

import 'package:flutter/material.dart';

class SignUpScreen extends StatefulWidget {
  const SignUpScreen({Key? key}) : super(key: key);

  @override
  State<SignUpScreen> createState() => _SignUpScreenState();
}

class _SignUpScreenState extends State<SignUpScreen> {
  @override
  void MyDialog() {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text("title"),
            content: Text("Thanks to clock"),
            actions: <Widget>[
              TextButton(
                onPressed: () {},
                child: Text("close"),
              )
            ],
          );
        });
  }

  Widget build(BuildContext context) {
    return TextButton(
      onPressed: () {
        MyDialog();
      },
      child: Text("Click On me!"),
    );
  }
}

问题是您没有通过按钮调用 dialog-function。将 dialog-function 相应地重命名为 code-convention,例如 'showMyDialog' 然后调用它:

 return TextButton(
  onPressed: () => showMyDialog(),
  child: Text("Click On me!"),
);