0

I'm using this Flutter widget: HorizontalPicker

as a very sample code here:

HorizontalPicker(
                  initialValue: Recipe.recipe.portionWeight.toInt(),
                  height: 70,
                  minValue: 0,
                  maxValue: Recipe.recipe.recipeWeight,
                  divisions: Recipe.recipe.recipeWeight.toInt(),
                  suffix: " g",
                  showCursor: false,
                  backgroundColor: Colors.grey.shade900,
                  activeItemTextColor: Colors.white,
                  passiveItemsTextColor: Colors.amber,
                  onChanged: (value) {
                    setState(() {
                      Recipe.recipe.portionWeight = value;
                      Recipe.calculatePortionGrams();
                      Speedometers.elaborateKcalNeedlePointer(value);
                      Recipe.updateRecipe();
                    });
                  },
                ),

Unfortunately this widget does not have a controller. My Users should change realtime the MAX value, however once drawed to the screen the widget does not refresh itself even if I change programmatically the MAX value on SetState. If I change page or hide the widget itself, once returned the value is refreshed.

There is a workaround for this? As for example how can I hide it for a second and display again? Or how can I force the redraw of a widget?

WizardingStudios
  • 554
  • 2
  • 7
  • 22
  • Where do you update the maxValue? I see in your code snippet that you update portionWeight, but not recipeWeight – Ozan Taskiran May 11 '23 at 17:12
  • Recipe Weight is updated elsewhere, not important where and absolutely it is updated inside a SetState event.. – WizardingStudios May 11 '23 at 17:22
  • You write in your question that you want to change the max value and give the widget your recipeWeight as max value, so it is important to know where and how it is changed. – Ozan Taskiran May 11 '23 at 18:00
  • Yes, however in this case the value is stored on a static variable that is accessed from many points. Not important what change it but how to refresh a widget without a controller. – WizardingStudios May 20 '23 at 07:56

1 Answers1

0

Found a solution: How to force Flutter to rebuild / redraw all widgets?

used a Key on the top level widget that contains the HorizontalPicker. The user to follow is: shawnblais

This is not a solution related directly to my question, this will solve any other issue with any other widget.

Here the solution copied from there:

Just use a Key on one of your high-level widgets, everything below this will lose state:

Key _refreshKey = UniqueKey();

void _handleLocalChanged() => setState((){ 
  _refreshKey = UniqueKey() 
});

Widget build(BuildContext context){
  return MaterialApp(
    key: _refreshKey ,
    ...

  )
}

You could also use a value key like:

return MaterialApp(
  key: ValueKey(locale.name)
  ...
);
WizardingStudios
  • 554
  • 2
  • 7
  • 22