【问题标题】:alertDialog didn't get called onPressedalertDialog 没有被调用 onPressed
【发布时间】:2019-12-13 07:32:35
【问题描述】:

嗨,所以当您按下卡上的关闭图标时,我试图显示 alertDialog,但是如果我点击“关闭”按钮,它不会显示用于删除确认的 alertDialog,另一方面,当我点击"check" 图标亮,它成功显示了一个snackbar。

这是我的页面代码:

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

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

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您的代码很完美,但您必须在调用它之前定义警报对话框,

    所以在按钮的 OnPress 方法中调用它之前定义 Alert Dialog将其传递给 alert 方法

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-16
      • 2018-02-09
      • 2019-01-04
      • 2020-10-12
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多