icemanbsi / searchable_dropdown

MIT License
107 stars 162 forks source link

Show AlertDialog before remove all saved options #75

Open daybson opened 4 years ago

daybson commented 4 years ago

I think would be better for the user if I show an YES/NO dialog before clear all options. So, as I needed this in my app, I made a small modification on searchable_dropdown.dart, inside the clearSelection() function.

If there is a better place to make this modification, please tell me. Thanks!

image

clearSelection() {
    var yes = FlatButton(
      child: Text('YES'),
      onPressed: () {
        Navigator.of(context).pop();
        //--------- ORIGINAL CODE -------------------
        selectedItems.clear();
        if (widget.onChanged != null) {
          widget.onChanged(selectedResult);
        }
        if (widget.onClear != null) {
          widget.onClear();
        }
        //-----------------------------------
      },
    );

    var no = FlatButton(
      child: Text('NO'),
      onPressed: () async {
        Navigator.of(context).pop();
      },
    );

    var alert = AlertDialog(
      title: Text('Remove all?'),
      content: Text('This will remove all saved options.'),
      actions: [no, yes],
    );

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

    setState(() {});
  }
}