如何在Flutter中显示2个连续的对话框

时间:2019-10-08 19:16:07

标签: flutter flutter-alertdialog

我有一个带有Dismissible小部件的列表。当用户交换startToEnd时,我想在用户可以添加注释时播出一个对话框。用户在该对话框中单击“确定”后,我希望弹出另一个对话框以确认已添加注释,然后在2秒钟后消失。当用户从右向左切换时,我想显示一个对话框并显示一条消息,持续2秒钟

这是我的代码

  List<Container> _buildBasketList() {
    return appData.basketList.map((bList) {
      var container = Container(
        child: Builder(
          builder: (context) => Dismissible(
            key: Key(UniqueKey().toString()),
            background: Container(
              margin: EdgeInsets.all(8.0),
              color: kColorAccent,
              child: Align(
                  alignment: Alignment(-0.90, 0.00),
                  child: Icon(Icons.add_comment)),
            ),
            onDismissed: (direction) async {
              print('ondismiss  started');
              if (direction == DismissDirection.startToEnd) {
                print('adding a note');
                await showDialog(
                    context: context,
                    builder: (context) => AddDetailsInDialog(
                          dialogText: 'Enter your note',
                          hintText: 'eg. xxx',
                        )).then((value) async {
                  if (value != null) {
                    print('got value');
                    await showDialog(
                        context: context,
                        builder: (context) => ShowAlertAndAutoDismiss(
                              text: 'Note added',
                            ));
                  }
                });
              } else {
                print('deleting record in basketList');
                await showDialog(
                    context: context,
                    builder: (context) => ShowAlertAndAutoDismiss(
                          text:
                               "Item removed from your list",
                        ));
              }
            },

这是我的课程

class ShowAlertAndAutoDismiss extends StatelessWidget {
  final String text;
  ShowAlertAndAutoDismiss({this.text});

  @override
  Widget build(BuildContext context) {
    print('building ShowAlertAndAutoDismiss');
    Future.delayed(Duration(milliseconds: 1000)).then((_) {
      print('ziiiip');
    });
    return AlertDialog(
      title: Center(child: Text(text)),
    );
  }
}
class AddDetailsInDialog extends StatelessWidget {
  final String dialogText;
  final String hintText;
  final myController = TextEditingController();

  AddDetailsInDialog({
    this.dialogText,
    this.hintText,
  });

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text(dialogText),
      content: new Row(
        children: <Widget>[
          new Expanded(
              child: new TextField(
            controller: myController,
            autofocus: true,
            decoration: new InputDecoration(hintText: hintText),
          ))
        ],
      ),
      actions: <Widget>[
        FlatButton(
          child: Text('OK'),
          onPressed: () {
            Navigator.of(context).pop(myController.toString());
          },
        ),
      ],
    );
  }

  @override
  void dispose() {
    myController.dispose();
  }
}

现在由于某种奇怪的原因,AddDetailsInDialog内部的ShowAlertAndAutoDismiss被调用了两次..... mmm?

这是我从右向左交换(删除)然后从左向右滑动(添加注释)时得到的日志。删除工作,但addNote被调用2次。为什么???有人知道为什么吗?

删除时记录

I/flutter ( 7941): ondismiss  started
I/flutter ( 7941): deleting record in basketList
I/flutter ( 7941): building ShowAlertAndAutoDismiss
I/flutter ( 7941): ziiiip

添加便笺时记录

I/flutter ( 7941): ondismiss  started
I/flutter ( 7941): adding a note
I/flutter ( 7941): got value
I/flutter ( 7941): building ShowAlertAndAutoDismiss
I/flutter ( 7941): building ShowAlertAndAutoDismiss
I/flutter ( 7941): ziiiip
I/flutter ( 7941): ziiiip

1 个答案:

答案 0 :(得分:0)

所以我通过在函数中移动ShowAlertAndAutoDismiss来解决

void alertToDismiss() {
    showDialog(
        context: context,
        builder: (context) => ShowAlertAndAutoDismiss(
              text: 'Note added',
            ));
  }

...

await showDialog(
                    context: context,
                    builder: (context) => AddDetailsInDialog(
                          dialogText: 'Enter your note',
                          hintText: 'eg. xxx',
                        )).then((value) async {
                  if (value != null) {
                    print('got value');
                    alertToDismiss():
                  }

该小部件的行为类似于FutureBuilder,并且首先使用null立即调用该构建器,然后在解析未来之后