alertDialog没有在onPressed上被调用

时间:2019-12-13 07:32:35

标签: flutter dart

嗨,所以我试图在您按下卡上的关闭图标时显示alertDialog,但是如果我点击“关闭”按钮,那么当我点击卡上的“关闭”按钮时,它不会显示用于删除确认的alertDialog。启用“检查”图标,它会成功显示一个快餐栏。

这是我的页面代码:

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;
      });
}

任何帮助将不胜感激!谢谢

1 个答案:

答案 0 :(得分:2)

您的代码很完美,但是您必须先定义警报对话框,

因此,请先定义警报对话框,然后再在按钮的 OnPress 方法中调用它,然后将其传递给警报方法

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;
      });
}