简体   繁体   English

ShowDialog 在 Flutter Android 中不工作

[英]the ShowDialog is not Working in Flutter Android

I'm starting on Flutter. Today, I learned how to coding a showDialog, but it doesn't work.我从 Flutter 开始。今天,我学习了如何编写 showDialog 代码,但它不起作用。 i have tried to write it again and again but nothing affects... here's my code:我试图一次又一次地写它,但没有任何影响......这是我的代码:


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!"),
    );
  }
}

Please help me.请帮我。 Thanks...谢谢...

You did not call MyDialog anywhere in the code.您没有在代码中的任何地方调用MyDialog

updated code:更新代码:

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!"),
    );
  }
}

The problem is that you are do not invoke dialog-function by you button.问题是您没有通过按钮调用对话框功能。 Rename dialog-function accordingly to the code-convention, for example 'showMyDialog' and then invoke it:根据代码约定重命名对话框函数,例如“showMyDialog”,然后调用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM