user17107802
user17107802

Reputation:

Cannot figure out why my Slider won't rebuild

First of all, I'm new to coding so please don't roast me too hard. I've spent my whole sunday afternoon trying to figure out how to make this work.

Basically I want to have 3 sliders but all with a different initial value. I thought it's a smart idea to put the code for this into a builder method and just call the builder method 3 times in the widget tree to make my code cleaner.

My problem is that when setState gets called it won't rebuild the Slider.

class _HomeScreenState extends State<HomeScreen> {
  double pomodoro = 25;
  double shortBreak = 5;
  double longBreak = 20;

  Widget _slider({
    required String name,
    required int divisions,
    required double minValue,
    required double maxValue,
    required double duration,
  }) {
    return Slider(
      min: minValue,
      max: maxValue,
      divisions: divisions,
      value: duration,
      onChanged: (newDuration) {
        setState(() => duration = newDuration);
      },
    );
  }

Now if instead of 'duration' I put 'pomodoro' in setState then everything works fine and the slider rebuilds but that would make the whole builder method obsolete since I want different initial values each time i call that method. How do I make this work?

Upvotes: 0

Views: 66

Answers (1)

user17107802
user17107802

Reputation:

After 4 days of sweat and tears and almost losing my mind, I finally figured out what my problem was.

First of all, my code snippet was too short and crucial parts were missing. Basically everything boils down to the fact that I had only one state object for all the sliders. Every slider needs its on state.

Upvotes: 1

Related Questions