M4trix Dev
M4trix Dev

Reputation: 2274

How to show 2 consecutive Dialogs in Flutter

I have a list with a Dismissible widget. When the user swap startToEnd I would like to sow a dialog when the user can add a note. Once the user click ok in that dialog I would like another dialog to pop to confirm a note was added and then disappear after 2 seconds. When the user swap right to left I would like to show a dialog with a message for 2 seconds

Here is my code

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

Here are my classes

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

Now for some strange reason the ShowAlertAndAutoDismiss inside the AddDetailsInDialog got called twice ..... mmm?

Here is the log I get if I do swap from right to left (delete) and then swipe left to right (add note). the delete work but the addNote get called 2 times. Why??? does anyone understand why?

Log when I delete

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

Log when I add a note

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

Upvotes: 5

Views: 11198

Answers (1)

M4trix Dev
M4trix Dev

Reputation: 2274

So I resolved by moving the ShowAlertAndAutoDismiss in a function

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():
                  }

It looks like this widget is behaving like a FutureBuilder and the builder is first immediately called with a null and then after the future is resolved

Upvotes: 3

Related Questions