3

When I pick a new value with the NumberPicker, it always jumps back to the previous value and not to the current chosen value.

I am using the NumberPicker within AlertDialog and I call the NumberPicker with the pickValue() function.

 void pickValue() {
 showDialog<int>(
 context: context,
 builder: (BuildContext context) {
 return AlertDialog(
 title: Text("Wähle eine Aufgabenanzahl"),
 content: NumberPicker(
 selectedTextStyle: TextStyle(color: Colors.red),
 value: currentValue,
 minValue: 1,
 maxValue: 10,
 onChanged: (value) => setState(() => currentValue = value),
 ),
 actions: [
 TextButton(
 child: Text("OK", style: TextStyles.body),
 onPressed: () {
 Navigator.of(context).pop();
 },
 )
 ],
 );
 });
 }
asked Oct 17, 2021 at 20:34

2 Answers 2

8

In This widget tree(using separate demo) you can see the showDialog() separated from the current context.

enter image description here

The use StatefulBuilder inside showDialog() or showModalBottomSheet()+... is these widgets are separated from current context tree.

We use StatefulBuilder when we like to show changes on dialogs. StatefulBuilder has its own statebuilder: (BuildContext context, setState) and calling setState is using StatefulBuilder's setstate.

Now let's say you want to change both UI, to do that you need to simply rename StatefulBuilder's state to something like SBsetState inside builder.

  • to update the _WidgetState use setState((){})
  • to update on dialog UI, use StatefulBuilder's state like SBsetState((){})

For more,

 showDialog<int>(
 context: context,
 builder: (BuildContext context) {
 return AlertDialog(
 title: Text("Wähle eine Aufgabenanzahl"),
 content:StatefulBuilder(
 builder: (context, SBsetState) {
 return NumberPicker(
 selectedTextStyle: TextStyle(color: Colors.red),
 value: currentValue,
 minValue: 1,
 maxValue: 10,
 onChanged: (value) { 
 setState(() => currentValue = value);// to change on widget level state 
 SBsetState(() => currentValue = value); //* to change on dialog state
 }
 );
 }
 ),
 
 actions: [
 TextButton(
 child: Text("OK", style: TextStyles.body),
 onPressed: () {
 Navigator.of(context).pop();
 },
 )
 ],
 );
 });
answered Oct 17, 2021 at 22:56
Sign up to request clarification or add additional context in comments.

5 Comments

thank you, works fine :)
I can't scroll with mouse drag. when I move the vertical scroll bar, the numbers go too high and exceed the maxValue.
OK, that happens when I set the maxValue to be too large
@Rony Tesler Can you provide minimal-reproducible-example and you can find answer about mouse drag behavior.
@YeasinSheikh Seems to work now, maybe it was a problem with the hot reload, not sure. Thanks!
1

Wrap the content of the alert dialog in a StatefulBuilder. It will provide a new context and a setState function to rebuild the widget.

void pickValue() {
showDialog<int>(
 context: context,
 builder: (BuildContext context) {
 return AlertDialog(
 title: Text("Wähle eine Aufgabenanzahl"),
 content:StatefulBuilder(
 builder: (context, setState) {
 return NumberPicker(
 selectedTextStyle: TextStyle(color: Colors.red),
 value: currentValue,
 minValue: 1,
 maxValue: 10,
 onChanged: (value) => setState(() => currentValue = value),
 );
 }
 ),
 
 actions: [
 TextButton(
 child: Text("OK", style: TextStyles.body),
 onPressed: () {
 Navigator.of(context).pop();
 },
 )
 ],
 );
 });
}
answered Oct 17, 2021 at 20:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.