Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Why slider doesn't update in AlertDialog?

I doing a AlertDialog, so when I tried to insert Slider widget inside the state of value sound realy stranger, and this doesn't happens if Slider is outside of AlertDialog

new Slider(
  onChanged: (double value) {
    setState(() {
      sliderValue = value;
    });
  },
  label: 'Oi',
  divisions: 10,
  min: 0.0,
  max: 10.0,
  value: sliderValue,
)

The complete widget code of AlertDialog

Future<Null> _showDialog() async {
  await showDialog<Null>(
      context: context,
      builder: (BuildContext context) {
        return new AlertDialog(
          title: const Text('Criar novo cartão'),
          actions: <Widget>[
            new FlatButton(onPressed: () {
              Navigator.of(context).pop(null);
            }, child: new Text('Hello'))
          ],
          content: new Container(
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                new Text('Deseja iniciar um novo cartão com quantos pedidos ja marcados?'),
                new Slider(
              onChanged: (double value) {
                setState(() {
                  sliderValue = value;
                });
              },
              label: 'Oi',
              divisions: 10,
              min: 0.0,
              max: 10.0,
              value: sliderValue,
            )
              ],
            ),
          ),
        );
      }
  );
}

and everything is under State class of StatefullWidget.

Its look like doesn't update the value and when try to change the value keep in same position.

Update 1

The problem is there are 2 required parameters in Slider (onChanged, value), So I shoud update this or UI keep quite, see the video how the aplication is running

Video on Youtube

Update 2

I've also opened a issue to get help with this at Github repository, if someone wants to get more information can go to issue #19323

like image 421
Helio Soares Junior Avatar asked Jul 10 '18 17:07

Helio Soares Junior


People also ask

How does AlertDialog return value in Flutter?

You can access and use the value that comes from your dialog option like this: showDialog( context: context, builder: (context) => Dialog( val: vale, ), ). then((valueFromDialog){ // use the value as you wish print(valueFromDialog); });

How do I use AlertDialog Flutter?

Create a Flutter project in Android Studio and replace the following code with main. dart file. To show an alert, you must have to call showDialog() function, which contains the context and itemBuilder function. The itemBuilder function returns an object of type dialog, the AlertDialog.

How to create alertdialog widget in flutter?

Run the application and you will see a raised button widget. Click on the button. Alert dialog appears. You choose an action or click on the greyed out area to dismiss the Alert dialog. Summary In this Flutter Tutorial, we learned about AlertDialog widget: how to create it in Flutter application, and some of its behaviors.

Why doesn't the dialog creator update the dialog?

I read it and have kind of understood it. The accepted answer says that: The problem is, dialogs are not built inside build method. They are on a different widget tree. So when the dialog creator updates, the dialog won't. However I am not able to understand how exactly does it have to be implemented as not enough background code is provided.

Can I use a statefulbuilder in an alertdialog?

The docs suggest that you use a StatefulBuilder in the content section of the AlertDialog. Even the StatefulBuilder docs actually have an example with a dialog. ” Dart is the programming language used to code Flutter apps. The recent Dart 2.3 update introduced the Spread operator, which can be used for conditional UI widgets.

What is alertdialog widget in Salesforce?

An AlertDialog widget displays a popup on top of the content. You can use it to notify user about an information or warning and allow the user to take an action. You can also use it to get input from user, like letting user choose from options, or providing text input. Following is a basic AlertDialog widget.


3 Answers

The problem is that it's not your dialog that holds the state. It's the widget that called showDialog. Same goes for when you call setState, you are calling in on the dialog creator.

The problem is, dialogs are not built inside build method. They are on a different widget tree. So when the dialog creator updates, the dialog won't.

Instead, you should make your dialog stateful. Hold the data inside that dialog. And then use Navigator.pop(context, sliderValue) to send the slider value back to the dialog creator.

The equivalent in your dialog would be

FlatButton(
  onPressed: () => Navigator.of(context).pop(sliderValue),
  child: Text("Hello"),
)

Which you can then catch inside the showDialog result :

final sliderValue = await showDialog<double>(
  context: context,
  builder: (context) => MyDialog(),
)
like image 183
Rémi Rousselet Avatar answered Oct 20 '22 19:10

Rémi Rousselet


Easiest and least amount of lines: Use StatefulBuilder as top widget of Content in the AlertDialog.

                StatefulBuilder(
                  builder: (context, state) => CupertinoSlider(
                    value: brightness,
                    onChanged: (val) {
                      state(() {
                        brightness = val;
                      });
                    },
                  ),
                ));
like image 39
cmd_prompter Avatar answered Oct 20 '22 19:10

cmd_prompter


I've come up with the same issue with a checkbox and that's my solution, even if it's not the best approach. (see the comment in the code)

Future<Null>_showDialog() async {
  return showDialog < Null > (
    context: context,
    barrierDismissible: true,
    builder: (BuildContext context) {
      return new AlertDialog(
        title: Text("title"),
        content: Container(
          height: 150.0,
          child: Checkbox(
            value: globalSearch,
            onChanged: (bool b) {
              print(b);
              globalSearch = b;
              Navigator.of(context).pop(); // here I pop to avoid multiple Dialogs
              _showDialog(); //here i call the same function
            },
          )),
      );
    },
  );
}
like image 5
Raoul Scalise Avatar answered Oct 20 '22 19:10

Raoul Scalise