提问者:小点点

flutter:单击开关时禁用滑块


我有开关和滑块。 该按钮应启用和禁用滑块,以便用户不能更改滑块。

我怎样才能完成这件事呢? 谢谢你的帮助。

               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";
                });
              },
            )

共1个答案

匿名用户

switch值为true时,使滑块的onchanged为空

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";
                });
              },
            )