Dropdown框中的值在Flutter中未更新。

huangapple go评论66阅读模式
英文:

Dropdown box value not updating in flutter

问题

我基本上正在尝试使用一列下拉框来实现文件导航系统。第一个框有两个选项:“random”和“choose”。一旦选择了其中一个选项,我有代码来使用该字符串并获取下一个下拉框的选项列表。一旦选择了一个选项,我想添加一个下拉框,以提供下一组选项,然后一旦我从该框中选择了一个选项,我想添加另一个框,其中包含下一个相应的选项,依此类推。

如下所示,我有两个列表,一个是包含所选选项的字符串列表,另一个是传递给列部件的下拉框列表。下拉框的值设置为所选选项列表中第一项的值(最初是一个包含一个null值的列表)。在onChanged属性中,我正在将值从null值更新为所选值,并打印语句表明值实际上正在更改,但由于某种原因,下拉框不显示更新的选择。我还没有到添加新下拉框的部分。

下拉框列表最初为空,但如果下拉列表最初为空,我会在列部件的构建中添加第一个。

请帮助,谢谢!

P.S. 对于随机的注释和代码混乱,我在使用它们进行调试,我仍然是一个新手。

以下是我实现的代码。它给我一个下拉框,似乎更新了leftDropDownBoxOptions的值,但是下拉框的值在屏幕上没有更改。

class TrainingScreenRoute extends StatefulWidget {

  ... // <--- 其他变量
  TrainingScreenRoute(this.user, this.currRelativePathLeft, this.currRelativePathMain){}

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return TrainingScreenRouteState(user, this.currRelativePathLeft, this.currRelativePathMain);
  }

}

class TrainingScreenRouteState extends State<TrainingScreenRoute> {

  ... // <--- 其他变量

  List<String?> leftDropDownBoxOptions = [null];

  List<Widget> leftDropDownBoxes = [];

  TrainingScreenRouteState(this.user, this.currRelPathLeft, this.currRelPathMain);

  @override
  Widget build(BuildContext context) {

  ... // <--- 其他变量

    return MaterialApp(
      home: Scaffold(
        body: leftTrainPanel(context, currRelPathLeft),
      ),
    );
  }

  Widget leftTrainPanel(BuildContext context, String currRelPathLeft) {

    int curr_index = leftDropDownBoxes.length;

    print("------------------>got to here");
    print(leftDropDownBoxOptions);
    if (curr_index == 0) { 
      Widget firstOptBox = createDropDownBox(context, ["random", "choose"], add, 0);
      print("here 5");

      setState(() {
        print("------------>box added");

        leftDropDownBoxes.add(firstOptBox);
      });

    }

  Widget createDropDownBox(BuildContext context, List<String> options, Function operation, int boxIndex) {

    double screen_h = MediaQuery.of(context).size.height;
    List<DropdownMenuItem<String>> menuItems = [];

    for (String option in options) {
      menuItems.add(DropdownMenuItem(child: Text(option, style: TextStyle(fontSize: screen_h/20)),value: option));
    }

    print("yolsssssssss");

    return DropdownButton(
      value: leftDropDownBoxOptions[0],
      hint: Text('Select'),
      items: menuItems,
      onChanged: (String? value) {
        setState(() {
          leftDropDownBoxOptions[0] = value!;
          //val = value;
          print("yolo");
          print(leftDropDownBoxOptions);
          print(leftDropDownBoxes);
          operation();
        });
      },
    );
  }


}
英文:

I am basically trying to implement a file navigation system using a column of dropdown boxes. The first box has 2 options: "random" and "choose".
once one of the options are selected, I have the code to use that string and get the next list of options for the next dropdown box. Once, an option is selected, I want to add a dropdown box that gives you the next set of options, and then once I choose an option from that box, I want to add another box with the next corresponding options and so on.

As seen below, I have 2 lists, one is a list of Strings containing the selected options, and one is a list of the dropdown boxes that are passed to the column widget. The value of the dropdown boxes is set to the value of the first item in the list of selected options (initially a list with one null value). in the onChanged property, I am updating the value from the null value to the selected value, and print statements indicate that the value is actually changing, but for some reason, the dropdown box is not showing an updated selection. I have not yet gotten to the part when I add new dropdown boxes.

The list of dropdown boxes is initially empty, but I add the first one in the build of the column widget if the dropdown list is initially empty.

Please help, Thank you!

P.S. Sorry about the random comments and messiness of code, I was using them to debug and I am still a Novice.

Here is the code that I implemented. It gives me a dropdown box and seems to update the values of leftDropDownBoxOptions but the value of the dropdown box does not change on screen.

class TrainingScreenRoute extends StatefulWidget {
... // &lt;--- other variables
TrainingScreenRoute(this.user, this.currRelativePathLeft, this.currRelativePathMain){}
@override
State&lt;StatefulWidget&gt; createState() {
// TODO: implement createState
return TrainingScreenRouteState(user, this.currRelativePathLeft, this.currRelativePathMain);
}
}
class TrainingScreenRouteState extends State&lt;TrainingScreenRoute&gt; {
... // &lt;--- other variables
List&lt;String?&gt; leftDropDownBoxOptions = [null];
List&lt;Widget&gt; leftDropDownBoxes = [];
TrainingScreenRouteState(this.user, this.currRelPathLeft, this.currRelPathMain);
@override
Widget build(BuildContext context) {
... // &lt;--- other variables
return MaterialApp(
home: Scaffold(
body: leftTrainPanel(context, currRelPathLeft),
),
);
}
Widget leftTrainPanel(BuildContext context, String currRelPathLeft) {
int curr_index = leftDropDownBoxes.length;
print(&quot;------------------&gt;got to here&quot;);
print(leftDropDownBoxOptions);
if (curr_index == 0) { 
Widget firstOptBox = createDropDownBox(context, [&quot;random&quot;, &quot;choose&quot;], add, 0);
print(&quot;here 5&quot;);
setState(() {
print(&quot;-------------&gt;box added&quot;);
leftDropDownBoxes.add(firstOptBox);
});
}
Widget createDropDownBox(BuildContext context, List&lt;String&gt; options, Function operation, int boxIndex) {
double screen_h = MediaQuery.of(context).size.height;
List&lt;DropdownMenuItem&lt;String&gt;&gt; menuItems = [];
for (String option in options) {
menuItems.add(DropdownMenuItem(child: Text(option, style: TextStyle(fontSize: screen_h/20)),value: option));
}
print(&quot;yolsssssssss&quot;);
return DropdownButton(
value: leftDropDownBoxOptions[0],
hint: Text(&#39;Select&#39;),
items: menuItems,
onChanged: (String? value) {
setState(() {
leftDropDownBoxOptions[0] = value!;
//val = value;
print(&quot;yolo&quot;);
print(leftDropDownBoxOptions);
print(leftDropDownBoxes);
operation();
});
},
);
}
}

答案1

得分: 0

以下是要翻译的内容:

"Ok so I'm not sure why, but I was able to fix the problem. the dropdown box widgets cannot be a state variable. Instead the information to create the dropdown box can be, but the box has to be created within the build."

英文:

Ok so I'm not sure why, but I was able to fix the problem. the dropdown box widgets cannot be a state variable. Instead the information to create the dropdown box can be, but the box has to be created within the build.

huangapple
  • 本文由 发表于 2023年5月21日 17:00:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299069.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定