darioielardi / flutter_speed_dial

Flutter plugin to implement a Material Design Speed Dial
https://pub.dev/packages/flutter_speed_dial
MIT License
410 stars 177 forks source link

SpeedDial and SpeedDialChild do not rebuild based on parent widget state change #293

Closed git-n-pissed closed 1 year ago

git-n-pissed commented 1 year ago

Imagine a stateful widget in which we want to have a button for "select all" and a button for "deselect all". With the IconButton widget I would do it like this:

IconButton(
  icon: const Icon(Icons.select_all),
  onPressed: _selected.length == _selectable.length ? null : () {
    setState(() {
      _selected = [];
      _selected.addAll(_selectable);
    });
  },
),
IconButton(
  icon: const Icon(Icons.deselect),
  onPressed: _selected.isEmpty ? null : () {
    setState(() {
      _selected = [];
    });
  },
),

This code sets the onPressed property to null if the function the button performs doesn't make sense for the parent widget's current state. For example, if _selected.isEmpty is true, there is nothing to deselect, so the button should show as disabled. The IconButton widget automatically shows a button as disabled if its onPressed property is null.

If I try to apply the same logic to any aspect of SpeedDial or SpeedDialChild, no rebuild ever takes place. To make a rebuild take place I have to put the state based logic outside of SpeedDial and create a whole new instance of SpeedDial and its childern. Unfortunately the combination of button states I have makes such a work around untenable.

prateekmedia commented 1 year ago

The opened state of speed dial gets build once so it will not be affected but as I know someone did created a PR for implementing childrenBuilder of something like that, using that this can be solved.

prateekmedia commented 1 year ago

Check out onOpenBuilder property, you will get a idea of how to do that.