androidiosfluttershowdialog

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. 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...


Solution

  • You did not call MyDialog anywhere in the code.

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