0

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?

Kaushik Chandru
  • 15,510
  • 2
  • 12
  • 30
Benny
  • 695
  • 2
  • 6
  • 19
  • first, your widget.fontSize, maybe `final`. 2. everytime that widget parent rebuild state, they always asisgn to `value:widget.FontSize`, that why your cuppertuno look freeze. you need to cache it into new parameter, basically every one answer below is right – Sayyid J Aug 04 '22 at 10:52

2 Answers2

1

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

Samia Ashraf
  • 195
  • 5
  • didn't work. plus it did update the widget.ontSize, the font changes. the only thing is that the slider doesn't move – Benny Aug 04 '22 at 07:31
  • the slider with this code worked for me, can you include a video after adding this code? – Samia Ashraf Aug 04 '22 at 12:25
0

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;
      });
    },
)
Kaushik Chandru
  • 15,510
  • 2
  • 12
  • 30