Search code examples
flutterflutter-alertdialogflutter-textinputfield

I've been trying to output AlertDialog in flutter after someone enters something, But alertdialog doesn't work


I wanted to get an output of whatever user typed inside the textfield, I referred to official documentation of flutter dev. I couldn't come up where I am wrong, I would be really glad if you guys help. I want the textfield input as output using a controller but alertdialog doesn't work.

Here's my code:


void main() {
  runApp(const MyApp());
}

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final myController = TextEditingController();

  @override
  void dispose() {
    // Clean up the controller when the widget is disposed.
    myController.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
          appBar: AppBar(title: const Text('Employee List')),
          body: Column(
            children: [
              Padding(
                padding: const EdgeInsets.all(16.0),
                child: TextField(
                  controller: myController,
                  decoration: const InputDecoration(
                    border: OutlineInputBorder(),
                    hintText: 'Enter Employee Name',
                  ),
                ),
              ),
              ElevatedButton(
                // When the user presses the button, show an alert dialog containing
                // the text that the user has entered into the text field.
                onPressed: () {
                  showDialog(
                    context: context,
                    builder: (context) {
                      return AlertDialog(
                        // Retrieve the text that the user has entered by using the
                        // TextEditingController.
                        content: Text(myController.text),
                      );
                    },
                  );
                },
                child: const Icon(Icons.text_fields),
              ),
            ],
          )),
    );
  }
}

Solution

  • You can create another widget(new one) or move MaterialApp on top level.

    void main() {
      runApp(
        const MaterialApp(
          debugShowCheckedModeBanner: false,
          home: MyApp(),
        ),
      );
    }
    
    class MyApp extends StatefulWidget {
      const MyApp({Key? key}) : super(key: key);
    
      @override
      State<MyApp> createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      final myController = TextEditingController();
    
      @override
      void dispose() {
        // Clean up the controller when the widget is disposed.
        myController.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: const Text('Employee List')),
          body: Column(
            children: [
              Padding(
                padding: const EdgeInsets.all(16.0),
                child: TextField(
                  controller: myController,
                  decoration: const InputDecoration(
                    border: OutlineInputBorder(),
                    hintText: 'Enter Employee Name',
                  ),
                ),
              ),
              ElevatedButton(
                // When the user presses the button, show an alert dialog containing
                // the text that the user has entered into the text field.
                onPressed: () {
                  showDialog(
                    context: context,
                    builder: (context) {
                      return AlertDialog(
                        // Retrieve the text that the user has entered by using the
                        // TextEditingController.
                        content: Text(myController.text),
                      );
                    },
                  );
                },
                child: const Icon(Icons.text_fields),
              ),
            ],
          ),
        );
      }
    }