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

How to update the value programatically #17

Open MySelfMukund opened 3 years ago

MySelfMukund commented 3 years ago

HI, Can you please suggest how to update the value programatically.

cgustav commented 2 years ago

Hi, im really sorry for the late response :(

In case you still want to continue with this case, what value do you mean exactly? thank you for your interest in the project

hulan0523 commented 1 year ago

I got a similar question. I want to set the value by using setState() when the notification API returns 200, but the widget only seems to change when onChanged happens.

             LiteRollingSwitch(
                  //initial value
                  value: bSubscribe,
                  width: ScreenUtil().setWidth(250),
                  textOn: t.ON,
                  textOff: t.OFF,
                  colorOn: AppColors.themeBlue,
                  colorOff: AppColors.darkGrey,
                  iconOn: Icons.notifications,
                  iconOff: Icons.notifications_off,
                  textSize: ScreenUtil().setSp(40),
                  textOnColor: Colors.white,
                  onChanged: (bool state) {
                    _subscriptionAPI(!bSubscribe);
                  },
                  onTap: () {},
                  onDoubleTap: () {},
                  onSwipe: () {},
                )
if (response.statusCode == 200) {
      await prefs.setBool(widget.currentDeviceName, doSubscribe);
      setState(() {
        bSubscribe = doSubscribe;
      });
    }

Also, I wonder if I should add _subscriptionAPI(!bSubscribe) to all events or just onChanged is enough.

cgustav commented 1 year ago

Hi @hulan0523, I highly recommend using the state value that you are receiving through the component callback onChanged, as this is the current value that you want to change. This way, you can ensure that your code sends the 'real' value of the component state to be updated (i.e., the value that should be sent to your API). Additionally, it is considered best practice to receive the new state directly from the API once the value has been updated, if this is an approachable option from your API's response.

             LiteRollingSwitch(
                  //initial value
                  value: bSubscribe,
                  width: ScreenUtil().setWidth(250),
                  textOn: t.ON,
                  textOff: t.OFF,
                  colorOn: AppColors.themeBlue,
                  colorOff: AppColors.darkGrey,
                  iconOn: Icons.notifications,
                  iconOff: Icons.notifications_off,
                  textSize: ScreenUtil().setSp(40),
                  textOnColor: Colors.white,
                  onChanged: (bool state) {
                    _subscriptionAPI(state);
                  },
                  onTap: () {},
                  onDoubleTap: () {},
                  onSwipe: () {},
                )

I hope this could be of help

Cheers!

Adbhutashra26 commented 1 year ago

How to update the value with Realtime Firebase

KishanMeetMighty commented 1 month ago

in case on switch tap i want to confirmation and according to dialog input if yes then only i want to change the value of switch for this type case this switch is not usefull. in future you can update this to update value programaticaly.