Ad

Flutter: Disable Slider On Click Of Switch

I've got a Switch and a Slider. The Button should enable and disable the Slider, so that the User cant change the Slider.

How can I accomplish this? Thanks for the Help.

               Switch(
                 value: notifyswitch,
                 onChanged: (value) {
                   setState(() {
                     notifyswitch = value;
                   });

                 },
               ),

            Center(
              child: Text(timeoutstring, style: TextStyle(fontSize: 20, color: Theme.of(context).accentColor))
            ),

            Slider(
              min: 0.0,
              max: 48,
              divisions: 24,
              value: _timeoutvalue,

              onChanged: (value) {
                setState(() {
                  _timeoutvalue = value;
                  timeoutstring = _timeoutvalue.toInt().toString() + "h";
                });
              },
            )
Ad

Answer

Make the Slider's onChangednull when your Switch value is true

Switch(
                 value: notifyswitch,
                 onChanged: (value) {
                   setState(() {
                     notifyswitch = value;
                   });

                 },
               ),

            Center(
              child: Text(timeoutstring, style: TextStyle(fontSize: 20, color: Theme.of(context).accentColor))
            ),

            Slider(
              min: 0.0,
              max: 48,
              divisions: 24,
              value: _timeoutvalue,

              onChanged: notifyswitch?null:(value) {
                setState(() {
                  _timeoutvalue = value;
                  timeoutstring = _timeoutvalue.toInt().toString() + "h";
                });
              },
            )
Ad
source: stackoverflow.com
Ad