Hi so I'm trying to show alertDialog when you press the close icon on the card but if I tap the "Close" button, it didn't show the alertDialog for delete confirmation, on the other hand when I tap the "check" Icon on, it successfully show a snackbar.

here's my code for the page:

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'Schedule.dart';

class HomeView extends StatefulWidget {
  HomeViewState createState() => HomeViewState();
}

class HomeViewState extends State<HomeView> {
  final List<Schedule> scheduleList = [
    Schedule("Hotel 1", DateTime.now(), DateTime.now(), "Germany"),
    Schedule("Hotel 2", DateTime.now(), DateTime.now(), "France")
  ];

  final Icon actionIcon = Icon(Icons.plus_one);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Dashboard"),
          centerTitle: true,
          actions: <Widget>[
            IconButton(
              icon: actionIcon,
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => UnscheduledField()),
                );
              },
            )
          ],
        ),
        body: Container(
          child: ListView.builder(
              itemCount: scheduleList.length,
              itemBuilder: (context, int index) => buildCard(context, index)),
        ));
  }
}

Widget buildCard(BuildContext context, int index) {
  final List<Schedule> scheduleList = [
    Schedule("Hotel 1", DateTime.now(), DateTime.now(), "Germany"),
    Schedule("Hotel 2", DateTime.now(), DateTime.now(), "France")
  ];
  final schedule = scheduleList[index];
  return Container(
      child: GestureDetector(
    onTap: () {
      showBottomSheet(
          context: context,
          builder: (context) => Container(
                height: 550,
                color: Colors.lightBlue,
              ));
    },
    child: Card(
        child: Padding(
            padding: const EdgeInsets.all(16),
            child: Column(children: <Widget>[
              Padding(
                padding: const EdgeInsets.only(top: 8, bottom: 4),
                child: Row(
                  children: <Widget>[
                    Text(schedule.companyName,
                        style: TextStyle(
                            fontSize: 20, fontWeight: FontWeight.bold)),
                    Spacer(),
                  ],
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 4, bottom: 4),
                child: Row(
                  children: <Widget>[Text(schedule.location)],
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 4.0, bottom: 4),
                child: Row(
                  children: <Widget>[
                    Text('check in at:'),
                    Text(
                        "${DateFormat('HH:mm').format(schedule.startTime).toString()}"),
                  ],
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 4.0, bottom: 4),
                child: Row(children: <Widget>[
                  Text('check out at:'),
                  Text(
                      "${DateFormat('HH:mm').format(schedule.endTime).toString()}"),
                ]),
              ),
              Row(
                children: <Widget>[
                  Expanded(
                    child: SizedBox(
                      height: 40,
                      child: ListTile(
                        trailing: IconButton(
                          onPressed: () {
                            alert(context);
                          },
                          icon: Icon(
                            Icons.close,
                          ),
                          color: Colors.red,
                        ),
                      ),
                    ),
                  ),
                  Expanded(
                    child: SizedBox(
                      height: 40,
                      child: ListTile(
                        trailing: IconButton(
                          onPressed: () {
                            final snackbar = SnackBar(
                              content: Text('Successfully added'),
                              duration: Duration(seconds: 2),
                            );
                            Scaffold.of(context).showSnackBar(snackbar);
                          },
                          icon: Icon(
                            Icons.check,
                          ),
                          color: Colors.lightGreen[300],
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ]))),
  ));
}

void alert(BuildContext context) {
  var alertDialog = AlertDialog(
    title: Text("Confirmation"),
    content: Text("Are you sure you want to delete this?"),
    actions: <Widget>[
      FlatButton(
          child: Text("No"),
          onPressed: () {
            Navigator.of(context).pop();
          }),
      FlatButton(
          child: Text("Yes"),
          onPressed: () {
            Navigator.of(context).pop();
          })
    ],
  );

  showDialog(
      context: context,
      builder: (BuildContext context) {
        return alertDialog;
      });
}

Any help would be appreciated! thanks


Solution 1: Jay Gadariya

your code is perfect but you have to define alert dialog before you call it,

So define Alert Dialog before you call it in OnPress method of button and Pass it to alert method

class HomeView extends StatefulWidget {
  HomeViewState createState() => HomeViewState();
}

class HomeViewState extends State<HomeView> {
   final List<Schedule> scheduleList = [
    Schedule("Hotel 1", DateTime.now(), DateTime.now(), "Germany"),
    Schedule("Hotel 2", DateTime.now(), DateTime.now(), "France")
  ];

  final Icon actionIcon = Icon(Icons.plus_one);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Dashboard"),
        centerTitle: true,
        actions: <Widget>[
            IconButton(
              icon: actionIcon,
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => UnscheduledField()),
                );
              },
            )
        ],
      ),
      body: buildCard(context, 0),
       body: Container(
          child: ListView.builder(
              itemCount: scheduleList.length,
              itemBuilder: (context, int index) => buildCard(context, index)),
        )
    );
  }
}

Widget buildCard(BuildContext context, int index) {
   final List<Schedule> scheduleList = [
    Schedule("Hotel 1", DateTime.now(), DateTime.now(), "Germany"),
    Schedule("Hotel 2", DateTime.now(), DateTime.now(), "France")
  ];
  final schedule = scheduleList[index];
  return Container(
      child: GestureDetector(
    onTap: () {
      showBottomSheet(
          context: context,
          builder: (context) => Container(
                height: 550,
                color: Colors.lightBlue,
              ));
    },
    child: Card(
        child: Padding(
            padding: const EdgeInsets.all(16),
            child: Column(children: <Widget>[
              Padding(
                padding: const EdgeInsets.only(top: 8, bottom: 4),
                child: Row(
                  children: <Widget>[
                    Text(schedule.companyName,
                        style: TextStyle(
                            fontSize: 20, fontWeight: FontWeight.bold)),
                    Spacer(),
                  ],
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 4, bottom: 4),
                child: Row(
                  children: <Widget>[Text(schedule.location)],
                    ),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 4.0, bottom: 4),
                child: Row(
                    children: <Widget>[
                    Text('check in at:'),
                    Text(
                        "${DateFormat('HH:mm').format(schedule.startTime).toString()}"),
                  ],
                    ),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 4.0, bottom: 4),
                child: Row(children: <Widget>[
                  Text('check out at:'),
                  Text(
                      "${DateFormat('HH:mm').format(schedule.endTime).toString()}"),
                ]),
              ),
              Row(
                children: <Widget>[
                  Expanded(
                    child: SizedBox(
                      height: 40,
                      child: ListTile(
                        trailing: IconButton(
                          onPressed: () {
                            var alertDialog = AlertDialog(
                              title: Text("Confirmation"),
                              content:
                                  Text("Are you sure you want to delete this?"),
                              actions: <Widget>[
                                FlatButton(
                                    child: Text("No"),
                                    onPressed: () {
                                      Navigator.of(context).pop();
                                    }),
                                FlatButton(
                                    child: Text("Yes"),
                                    onPressed: () {
                                      Navigator.of(context).pop();
                                    })
                              ],
                            );
                            alert(context, alertDialog);
                          },
                          icon: Icon(
                            Icons.close,
                          ),
                          color: Colors.red,
                        ),
                        title: Text("hello"),
                      ),
                    ),
                  ),
                  Expanded(
                    child: SizedBox(
                      height: 40,
                      child: ListTile(
                        trailing: IconButton(
                          onPressed: () {
                            final snackbar = SnackBar(
                              content: Text('Successfully added'),
                              duration: Duration(seconds: 2),
                            );
                            Scaffold.of(context).showSnackBar(snackbar);
                          },
                          icon: Icon(
                            Icons.check,
                          ),
                          color: Colors.lightGreen[300],
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ]))),
  ));
}

void alert(BuildContext context, AlertDialog alertDialog) {
  showDialog(
      context: context,
      builder: (BuildContext context) {
        return alertDialog;
      });
}