Home > Mobile >  onTap to AlertDialog flutter 2021
onTap to AlertDialog flutter 2021

Time:12-13

I faced a problem in flutter 2021 version because when i search for any solution it was old anyway the problem is i have a slide menu with exit button when i click on that button must show up dialog aler to give the user to choose if he want to exit or not

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';


class MenuItems extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
   return Drawer(
     child: ListView(
       padding: EdgeInsets.zero,
       children: [
         UserAccountsDrawerHeader(
            accountName: Text("Admin",
              style: TextStyle(
                  fontSize: 25,
                  color: Colors.black87,
            ),
     ),
            accountEmail: Text("[email protected]",
              style: TextStyle(
                fontSize: 25,
                color: Colors.black87,
              ),
            ),
           currentAccountPicture: CircleAvatar(
             child: ClipOval(
               child: Image.asset("assets/images/me.jpg",
                 fit:BoxFit.cover,
               ),
             ),
           ),
           decoration: BoxDecoration(
             image: DecorationImage(
               image: AssetImage("assets/images/image.png"),
               fit: BoxFit.cover,
             )
           ),
        ),
         ListTile(
           leading:Icon(Icons.save),
           title: Text('Saved Results'),
           onTap: ()=>print("saved result"),
         ),
         Divider(),
         ListTile(
           leading:Icon(Icons.contact_page),
           title: Text('Contact Us'),
           onTap: ()=>print("Contact us"),
         ),
         ListTile(
           leading:Icon(Icons.info),
           title: Text('About US'),
           onTap: ()=>print("About us"),
         ),
         Divider(),
         ListTile(
           leading:Icon(Icons.exit_to_app),
           title: Text('Exit'),
           //the function doesn't proccess i don't know why
           onTap: ()=>showDialogWidget(context),
         ),

       ],
     ),
   );
  }
}
//this is the function to return alerDialog but it doesn't work why i dont know
 AlertDialog showDialogWidget(BuildContext context) {
  return AlertDialog(
     // when i did print("sth") it printed 
     title: Text("Are you sure?"),
     content: Text("Would you really want to exit the app?"),
     actions: [
       TextButton(onPressed: () {}, child: Text("Exit")),
       TextButton(onPressed: () {}, child: Text("Cancel")),
     ],
    elevation: 24.0,
    backgroundColor: Colors.green,
    shape: CircleBorder(),
   );
 }

so please how i can navigate from the buttons in the slide menu and how i con popup the alert messages from the slide menu thank you in advance

CodePudding user response:

You are only missing the showDialog method from material.dart (let the auto-import) :

AlertDialog showDialogWidget(BuildContext context) {
  return showDialog(
        context: context,
        builder: (newContext) {
          return AlertDialog(
     // when i did print("sth") it printed 
     title: Text("Are you sure?"),
     content: Text("Would you really want to exit the app?"),
     actions: [
       TextButton(onPressed: () {}, child: Text("Exit")),
       TextButton(onPressed: () {}, child: Text("Cancel")),
     ],
    elevation: 24.0,
    backgroundColor: Colors.green,
    shape: CircleBorder(),
   );
});
 }

CodePudding user response:

this example is exactly what you want to do

 ListTile(
                        title: Text(GlobalString().exit),
                        onTap: () {
                          return showDialog(
                              context: context,
                              builder: (context) => AlertDialog(
                                    title: Text(GlobalString().exitAnswer),
                                    actions: [
                                      TextButton(
                                          onPressed: () =>
                                              Navigator.pop(context, false),
                                          child: Text('No')),
                                      TextButton(
                                          onPressed: () {
                                            if (prefs.getString(
                                                    Constant().INPROCESS) !=
                                                '') {
                                              exit(0);
                                            } else {
                                              prefs.clear().whenComplete(() {
                                                dbHelper.deleteDatabase();
                                                exit(0);
                                              });
                                            }
                                          },
                                          child: Text('Si'))
                                    ],
                                  ));
                        },
                      )
  • Related