cgustav / lite_rolling_switch

Full customable rolling switch widget for flutter apps based on Pedro Massango's 'crazy-switch' widget https://github.com/pedromassango/crazy-switch
MIT License
55 stars 52 forks source link

on change function is being called on load #2

Closed MasterNeuromancer closed 2 years ago

MasterNeuromancer commented 4 years ago

when the widget loads, the state is false, and therefore unintended calls are being made on the app. Is there a way to isolate the on tap function only. I only want the calls to be made when a user taps the widget, not on load. Thank you.

Bader-Al commented 4 years ago

I had the same issue and found two solutions.. however, the second one destroyed the drag of a DraggableBottomSheet I have in my app.. So i stuck to the first one

1 - just wrap the onChanged function with WidgetsBinding as such :

WidgetsBinding.instance.addPostFrameCallback((_){

  // Add Your Code here.

});

2- the other method is to use SchedulerBinding :

SchedulerBinding.instance.addPostFrameCallback((_) {

  // add your code here.
});
Bader-Al commented 4 years ago

Incase you'd like to see where i found those solutions : https://stackoverflow.com/questions/47592301/setstate-or-markneedsbuild-called-during-build

MasterNeuromancer commented 4 years ago

Thanks @Bader-Al , I will look into that!

rodrigobastosv commented 4 years ago

I just made a PR to solve this issue.

https://github.com/cgustav/lite_rolling_switch/pull/6

Huzaifa2019 commented 4 years ago

Thank you so much

0ne0rZer0 commented 3 years ago

I was having the same problem what i did was add an if condition inside the _determine() function (as it is called during initState) if (changeState) widget.onChanged(turnState);