Home > Enterprise >  the ShowDialog is not Working in Flutter Android
the ShowDialog is not Working in Flutter Android

Time:03-14

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

CodePudding user response:

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

CodePudding user response:

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:

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