Home > Software engineering >  Flutter CupertinoSlider not moving
Flutter CupertinoSlider not moving

Time:08-04

I'm using a CupertinoSldier to Adjust the font size. the size does change, but the slider itself isn't moving from it's original location.

Here's the slider code:

child: CupertinoSlider(
    min: 8.0,
    max: 34.0,
    activeColor: Colors.blue,
    thumbColor: Colors.blueAccent,
    value: widget.fontSize,
    onChanged: (value) {
      setState(() {
        widget.fontSize = value;
      });
    },
)

Any ideas?

CodePudding user response:

widget.fontSize is the problem here. setState() will not update the widget.fontSize value, so assign widget.fontSize to a new variable double fontValue = widget.fontSize;

CupertinoSlider(
   min: 8.0,
   max: 34.0,
   activeColor: Colors.blue,
   thumbColor: Colors.blueAccent,
   value: fontValue,
   onChanged: (value) {
    setState(() {
      fontValue = value;
    });
   },
),

this should work

CodePudding user response:

This code works except when the value is less than 8.. You can do a small check here like

child: CupertinoSlider(
    min: 8.0,
    max: 34.0,
    activeColor: Colors.blue,
    thumbColor: Colors.blueAccent,
    value: widget.fontSize <= 8 ? 8 : widget.fontSize, //<--here
    onChanged: (value) {
      setState(() {
        widget.fontSize = value;
      });
    },
)
  • Related