Flutter-为什么滑块在AlertDialog中不更新? [英] Flutter - Why slider doesn't update in AlertDialog?

查看:173
本文介绍了Flutter-为什么滑块在AlertDialog中不更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行AlertDialog,所以当我尝试将Slider小部件插入有价值的声音状态时,确实很陌生,如果Slider不在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,
)

AlertDialog的完整小部件代码

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,
            )
              ],
            ),
          ),
        );
      }
  );
}

一切都在StatefullWidget的State类下。

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.

问题是滑块(onChanged,值)中有2个必需参数,所以我应该对此进行更新或UI保持一致,观看视频,了解应用程序的运行方式

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

Youtube上的视频

如果在Github存储库中获得相关帮助,我也开设了一个问题有人想获取更多信息,请访问 issue#19323

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

推荐答案

问题在于,不是由您的对话框来保持状态。名为 showDialog 的小部件。调用 setState 时(在对话框创建者上调用),情况也是如此。

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.

问题是,对话框不是在 build 方法内构建的。它们在不同的小部件树上。因此,当对话框创建者更新时,对话框将不会。

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.

相反,您应该使对话框保持状态。将数据保存在该对话框中。然后使用 Navigator.pop(context,slideValue)将滑块值发送回对话框创建者。

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.

对话框中的等效项将是

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

然后您可以在 showDialog 结果内进行查找:

Which you can then catch inside the showDialog result :

final sliderValue = await showDialog<double>(
  context: context,
  builder: (context) => MyDialog(),
)

这篇关于Flutter-为什么滑块在AlertDialog中不更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆