L1ghtsp33D
L1ghtsp33D

Reputation: 119

showDialog works in PopUpMenuButton onSelected but not in the PopUpMenuItem onTap

I am wondering why showDialog is not showing in a slightly different context, it seems like there are many cases where a few layers of context on a route should not prevent a dialog from showing. Especially when passing navigator context explicitly. Below is a minimal reproducible sample. I also tried using

final GlobalKey? navigatorKey

and passing

navigatorKey.currentContext

to the showDialog method, and that also did not work. I'm aware it's probably because of the context in the itemBuilder, but does anyone have a better explanation of why the dialog won't show from onTap?

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(

      body: Center(
          child:PopupMenuButton(
              icon: Icon(Icons.more_vert),
              onSelected: (value){
                if (value == "val1") {
                  // works
                  // showADialog(context);
                }
                if (value == "val2"){
                  showADialog(context);
                }
              },
              itemBuilder: (context) {
                return [
                  PopupMenuItem(
                      onTap: (){
                        // doesn't work
                        showADialog(context);
                      },
                      value: "val1",
                      child: Text("val1")),
                  PopupMenuItem(
                      value:"val2",
                      child: Text("val2"))
                ];
              })
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

showADialog(BuildContext context){
  showDialog(
      context: context,
      builder: (context) {
    return AlertDialog(
        title: Text("A Dialog"));});
}

Upvotes: 0

Views: 72

Answers (1)

greetal
greetal

Reputation: 1517

showDialog is not appearing when triggered from the PopupMenuItem's onTap handler.

Pass context in this way;

return Scaffold(
          body: Center(
            child: PopupMenuButton<String>(
              icon: const Icon(Icons.more_vert),
              onSelected: (value) {
                if (value == "val2") {
                  showPopupDialog(context);
                }
              },
              itemBuilder: (context) {
                return [
                  PopupMenuItem(
                    value: "val1",
                    child: GestureDetector(
                      onTap: () => showPopupDialog(context), // Pass context here
                      child: const Text("val1"),
                    ),
                  ),
                  const PopupMenuItem(
                    value: "val2",
                    child: Text("val2"),
                  ),
                ];
              },
            ),
          ),
     );

Method 2: Using _scaffoldKey.

 final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      body: Center(
        child: PopupMenuButton(
          icon: const Icon(Icons.more_vert),
          onSelected: (value) {
            if (value == "val1") { 
            }
            if (value == "val2") {
              showADialog(_scaffoldKey.currentContext!);
            }
          },
          itemBuilder: (context) {
            return [
              PopupMenuItem(
                onTap: () {  
                  showADialog(_scaffoldKey.currentContext!);
                },
                value: "val1",
                child: const Text("val1"),
              ),
              const PopupMenuItem(
                value: "val2",
                child: Text("val2"),
              ),
            ];
          },
        ),
      ),
    );
  }

Upvotes: 0

Related Questions