1

So I am facing this problem that my alert Dialog isn't displaying. I had tried every possible solution and searching here and there but nothing works. When I click on the edit button from the pop up menu nothing is displayed everything remains the same. enter image description here

Calling alert Dialog

trailing: PopupMenuButton(
 icon: Icon(Icons.more_vert),
 itemBuilder: (context)=>[
 PopupMenuItem(
 value:1,
 onTap: (){
 //debugPrint('popup');
 Navigator.pop(context);
 _showMyDialog();
 },
 child: ListTile(
 leading: Icon(Icons.edit),
 title: Text('Edit'),
 )),
 PopupMenuItem(
 value:1,
 // onTap: (){
 // Navigator.pop(context);
 // showDialogBox();
 // },
 child: ListTile(
 leading: Icon(Icons.delete),
 title: Text('Delete'),
 )),
 ]),

Alert Dialog Code

Future<void> showDialogBox(String title)async{
editController.text=title;
debugPrint('dialog');
return showDialog<void>(
 context: context,
 barrierDismissible: false,
 builder: (BuildContext context){
 debugPrint('alert');
 return AlertDialog(
 title: Text('Update'),
 content: Container(
 child: TextFormField(
 controller: editController,
 ),
 ),
 actions: [
 TextButton(onPressed: (){
 Navigator.pop(context);
 }, child: Text('Update')),
 TextButton(onPressed: (){
 Navigator.pop(context);
 }, child: Text('Cancel')),
 ],
 );
 }

); } Complete Class Code

import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_database/ui/firebase_animated_list.dart';
import 'package:firebase_tutorial/utils/routes/routes_names.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:firebase_database/firebase_database.dart';
import '../../utils/utils.dart';
class PostScreen extends StatefulWidget {
 const PostScreen({Key? key}) : super(key: key);
 @override
 State<PostScreen> createState() => _PostScreenState();
}
class _PostScreenState extends State<PostScreen> {
 final ref=FirebaseDatabase.instance.ref('Post');
 FirebaseAuth _auth=FirebaseAuth.instance;
 final searchController=TextEditingController();
 final editController=TextEditingController();
 @override
 Widget build(BuildContext context) {
 return WillPopScope(
 onWillPop: ()async{
 SystemNavigator.pop();
 return true;
 },
 child: Scaffold(
 appBar: AppBar(
 automaticallyImplyLeading: false,
 title: Text('Post Screen'),
 actions: [
 GestureDetector(
 onTap: (){
 _auth.signOut().then((value){
 Navigator.pushNamed(context, RoutesNames.loginScreen);
 }).onError((error, stackTrace){
 Utils().toastMessage(error.toString());
 });
 },
 child: Icon(Icons.logout_outlined)),
 SizedBox(width: 10,),
 ],
 ),
 floatingActionButton: FloatingActionButton(
 onPressed:(){
 Navigator.pushNamed(context, RoutesNames.newPost);
 },
 child: Icon(Icons.add),),
 body: Column(
 children: [
 // Expanded(
 // child:FirebaseAnimatedList(
 // query: ref,
 // itemBuilder: (context,snapshot,animation,index){
 // return ListTile(
 // title: Text(snapshot.child('post').value.toString()),
 // );
 // }
 // ),
 // ),
 Padding(
 padding: const EdgeInsets.all(10.0),
 child: TextFormField(
 onChanged: (String value){
 setState(() {
 });
 },
 controller: searchController,
 decoration: InputDecoration(
 border: OutlineInputBorder(),
 hintText: "Search",
 ),
 ),
 ),
 Expanded(child: StreamBuilder(
 stream: ref.onValue,
 builder: (context,AsyncSnapshot<DatabaseEvent> snapshot){
 if(!snapshot.hasData){
 return CircularProgressIndicator();
 }
 else{
 return ListView.builder(
 itemCount: snapshot.data!.snapshot.children.length,
 itemBuilder: (context,index){
 Map<dynamic,dynamic> map=snapshot.data!.snapshot.value as dynamic;
 List<dynamic> list=[];
 list.clear();
 list=map.values.toList();
 final title=list[index]['post'].toString();
 if(searchController.text.isEmpty){
 return ListTile(
 title: Text(list[index]['post']),
 subtitle: Text(list[index]['id'].toString()),
 trailing: PopupMenuButton(
 icon: Icon(Icons.more_vert),
 itemBuilder: (context)=>[
 PopupMenuItem(
 value:1,
 onTap: (){
 //debugPrint('popup');
 Navigator.pop(context);
 _showMyDialog();
 },
 child: ListTile(
 leading: Icon(Icons.edit),
 title: Text('Edit'),
 )),
 PopupMenuItem(
 value:1,
 // onTap: (){
 // Navigator.pop(context);
 // showDialogBox();
 // },
 child: ListTile(
 leading: Icon(Icons.delete),
 title: Text('Delete'),
 )),
 ]),
 );
 }
 else if(title.toLowerCase().contains(searchController.text.toLowerCase())){
 return ListTile(
 title: Text(list[index]['post']),
 subtitle: Text(list[index]['id'].toString()),
 );
 }
 else{
 return Container();
 }
 });
 }
 }))
 ],
 ),
 ),
 );
 }
 Future<void> showDialogBox(String title)async{
 editController.text=title;
 debugPrint('dialog');
 return showDialog<void>(
 context: context,
 barrierDismissible: false,
 builder: (BuildContext context){
 debugPrint('alert');
 return AlertDialog(
 title: Text('Update'),
 content: Container(
 child: TextFormField(
 controller: editController,
 ),
 ),
 actions: [
 TextButton(onPressed: (){
 Navigator.pop(context);
 }, child: Text('Update')),
 TextButton(onPressed: (){
 Navigator.pop(context);
 }, child: Text('Cancel')),
 ],
 );
 }
 );
 }
}
asked Oct 31, 2022 at 12:20

1 Answer 1

2

try adding a delay before calling showDialog like this:

await Future.delayed(const Duration(milliseconds: 10));

Your dialog isnt displayed because when you select a menu item the pop() method is automatically called to close the popup menu; so if you open a dialog immediately, the dialog will get automatically popped. hope this fixes your issue

answered Oct 31, 2022 at 12:36
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much for your help its work but only when i remove the pop statement. With the pop statement it actually pop the home screen and go to the splash screen but whenever i remove the pop state and added a delay as you said it works
But it still works the same because when the alert dialog displayed the pop menu get popped itself. All thanks to you
You're very welcome. No you don't need to call the pop() method, it gets called automatically when you select a menu item, and that's exactly why your dialog wasn't showing. I'm happy I could help. if the answer worked, I would appreciate it if you accept the answer. thank you

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.