【问题标题】:Trying to get dropdown menu and date to populate after selection in a dialog尝试在对话框中选择后填充下拉菜单和日期
【发布时间】:2020-09-15 07:33:09
【问题描述】:

您好,我在警报对话框中有一个下拉菜单和一个日期选择器。选择打印在控制台 onChange 上,所以我知道它正在工作,但它们不会在实际的警报对话框中改变,除非我关闭并再次重新打开它。有什么方法可以让我实时更改日期和下拉菜单选择??

我的下拉代码如下:

void rightButtonPressed() {
setState(() {
  walkDuration = dependencies.stopwatch.elapsedMilliseconds~/60000;
  if (dependencies.stopwatch.isRunning) {
    dependencies.stopwatch.stop();
    showDialog(
      context: context,
      builder: (BuildContext context){
        return AlertDialog(
          actions: <Widget>[
            Theme(
              child: Button(
                child: const Text('Add'),
                onPressed: (){
                  add();
                  Navigator.of(context).pop();
                },
                style: Style(
                  color: Colors.white,
                )
              ),
            )
          ],
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(8)),
            content: Stack(
              overflow: Overflow.visible,
              children: <Widget>[
                Form(
                  child: SingleChildScrollView(
                    child: Column(
                      mainAxisSize: MainAxisSize.min, 
                      children: <Widget>[
                        Padding(
                          padding: EdgeInsets.all(5.0),
                          child: Column(
                            // mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            children: [
                              DropdownButton(
                                value: numberTimes,
                                items: [
                                  DropdownMenuItem(
                                    child: Text("0", style: TextStyle(fontSize:15.0)),
                                    value: 0,
                                  ),
                                  DropdownMenuItem(
                                    child: Text("1", style: TextStyle(fontSize:15.0)),
                                    value: 1,
                                  ),
                                  DropdownMenuItem(
                                    child: Text("2", style: TextStyle(fontSize:15.0)),
                                    value: 2,
                                  ),
                                  DropdownMenuItem(
                                    child: Text("3", style: TextStyle(fontSize:15.0)),
                                    value: 3,
                                  ),
                                  DropdownMenuItem(
                                    child: Text("4", style: TextStyle(fontSize:15.0)),
                                    value: 4,
                                  ),
                                  DropdownMenuItem(
                                    child: Text("5", style: TextStyle(fontSize:15.0)),
                                    value: 5,
                                  ),
                                  DropdownMenuItem(
                                    child: Text("5+", style: TextStyle(fontSize:15.0)),
                                    value: 6,
                                  ),
                                ],
                                onChanged:(value) {
                                  setState((){
                                    numberTimes = value;
                                    print(numberTimes);
                                  });
                                }
                              ),
                            ],
                          )
                        ),

【问题讨论】:

标签: flutter dart state dropdown


【解决方案1】:

用 statefullBuilder 扭曲你的对话 并使用 setState

示例:

 showDialog(
  context: context,
  builder: (context) {
    String contentText = "Content of Dialog";
    return StatefulBuilder(
      builder: (context, setState) {
        return AlertDialog(
          title: Text("Title of Dialog"),
          content: Text(contentText),
          actions: <Widget>[
            FlatButton(
              onPressed: () => Navigator.pop(context),
              child: Text("Cancel"),
            ),
            FlatButton(
              onPressed: () {
                setState(() {
                  contentText = "Changed Content of Dialog";
                });
              },
              child: Text("Change"),
            ),
          ],
        );
      },
    );
  },
);

【讨论】:

    【解决方案2】:

    请将DropdownButton 更改为DropdownButtonFormField&lt;int&gt;。我已经编辑了你的代码,你在那里写了多少。因为你没有写完整的代码。

    这是我编辑的代码。

           showDialog<void>(
                  context: context,
                  builder: (BuildContext context) {
                    return AlertDialog(
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(8)),
                        content: Stack(
                            overflow: Overflow.visible,
                            children: <Widget>[
                              Form(
                                  child: DropdownButtonFormField<int>(
                                      value: numberTimes,
                                      items: [
                                        DropdownMenuItem(
                                          child: Text("0",
                                              style: TextStyle(
                                                  color: Colors.black,
                                                  fontSize: 15.0)),
                                          value: 0,
                                        ),
                                        DropdownMenuItem(
                                          child: Text("1",
                                              style: TextStyle(fontSize: 15.0)),
                                          value: 1,
                                        ),
                                        DropdownMenuItem(
                                          child: Text("2",
                                              style: TextStyle(fontSize: 15.0)),
                                          value: 2,
                                        ),
                                        DropdownMenuItem(
                                          child: Text("3",
                                              style: TextStyle(fontSize: 15.0)),
                                          value: 3,
                                        ),
                                        DropdownMenuItem(
                                          child: Text("4",
                                              style: TextStyle(fontSize: 15.0)),
                                          value: 4,
                                        ),
                                        DropdownMenuItem(
                                          child: Text("5",
                                              style: TextStyle(fontSize: 15.0)),
                                          value: 5,
                                        ),
                                        DropdownMenuItem(
                                          child: Text("5+",
                                              style: TextStyle(fontSize: 15.0)),
                                          value: 6,
                                        ),
                                      ],
                                      onChanged: (int value) {
                                        setState(() => numberTimes = value);
                                      }))
                            ]));
                  })
    

    你的代码是。

    void rightButtonPressed() {
    setState(() {
      walkDuration = dependencies.stopwatch.elapsedMilliseconds~/60000;
      if (dependencies.stopwatch.isRunning) {
        dependencies.stopwatch.stop();
        showDialog(
          context: context,
          builder: (BuildContext context){
            return AlertDialog(
              actions: <Widget>[
                Theme(
                  child: Button(
                    child: const Text('Add'),
                    onPressed: (){
                      add();
                      Navigator.of(context).pop();
                    },
                    style: Style(
                      color: Colors.white,
                    )
                  ),
                )
              ],
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(8)),
                content: Stack(
                  overflow: Overflow.visible,
                  children: <Widget>[
                    Form(
                      child: SingleChildScrollView(
                        child: Column(
                          mainAxisSize: MainAxisSize.min, 
                          children: <Widget>[
                            Padding(
                              padding: EdgeInsets.all(5.0),
                              child: Column(
                                // mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                                children: [
                                  DropdownButtonFormField<int>(
                                    value: numberTimes,
                                    items: [
                                      DropdownMenuItem(
                                        child: Text("0", style: TextStyle(fontSize:15.0)),
                                        value: 0,
                                      ),
                                      DropdownMenuItem(
                                        child: Text("1", style: TextStyle(fontSize:15.0)),
                                        value: 1,
                                      ),
                                      DropdownMenuItem(
                                        child: Text("2", style: TextStyle(fontSize:15.0)),
                                        value: 2,
                                      ),
                                      DropdownMenuItem(
                                        child: Text("3", style: TextStyle(fontSize:15.0)),
                                        value: 3,
                                      ),
                                      DropdownMenuItem(
                                        child: Text("4", style: TextStyle(fontSize:15.0)),
                                        value: 4,
                                      ),
                                      DropdownMenuItem(
                                        child: Text("5", style: TextStyle(fontSize:15.0)),
                                        value: 5,
                                      ),
                                      DropdownMenuItem(
                                        child: Text("5+", style: TextStyle(fontSize:15.0)),
                                        value: 6,
                                      ),
                                    ],
                                    onChanged:(value) {
                                      setState((){
                                        numberTimes = value;
                                        print(numberTimes);
                                      });
                                    }
                                  ),
                                ],
                              )
                            ),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-23
      • 2014-12-11
      • 1970-01-01
      • 2022-11-17
      相关资源
      最近更新 更多