Flutter 如何在颤振下拉按钮中设置动态提示?

Flutter 如何在颤振下拉按钮中设置动态提示?,flutter,dart,input,setstate,dropdownbutton,Flutter,Dart,Input,Setstate,Dropdownbutton,我希望我的下拉按钮根据用户选择的项目进行更改。以下是我的变量声明: class\u HomeState扩展状态{ 最终gb_控制器=文本编辑控制器(); 最终Bruto_控制器=TextEditingController(); 最终bAV_控制器=文本编辑控制器(); int steuerklassen_值=1; 字符串selectedValue=“Steuerklasse”; 字符串hintValue=“Steuerklasse 1”; 布尔加载=假; final formkey=Global

我希望我的下拉按钮根据用户选择的项目进行更改。以下是我的变量声明:

class\u HomeState扩展状态{
最终gb_控制器=文本编辑控制器();
最终Bruto_控制器=TextEditingController();
最终bAV_控制器=文本编辑控制器();
int steuerklassen_值=1;
字符串selectedValue=“Steuerklasse”;
字符串hintValue=“Steuerklasse 1”;
布尔加载=假;
final formkey=GlobalKey();
最终steuerklassen=['I','II','III','IV','V','VI'];
这是我的下拉按钮:

child:下拉按钮(
提示:文本(hintValue),
iconSize:20,
项目:steuerklassen
.map((字符串dropDownStringItem){
返回下拉菜单项(
值:dropDownStringItem,
子项:文本(dropDownStringItem),
);
}).toList(),
一旦更改:(selectedValue){
设置状态(){
对于(int i=1;

i在下拉按钮中添加

var changedValue;
DropdownButton<String>(
  hint: Text(hintValue),
  iconSize: 20,
  value: changedValue, //add this parameter
  items: steuerklassen.map((String dropDownStringItem) {
    return DropdownMenuItem(
      value: dropDownStringItem,
      child: Text(dropDownStringItem),
    );
  }).toList(),
  onChanged: (selectedValue) {
    setState(() {
      for (int i = 1; i <= steuerklassen.length; i++)
        if (steuerklassen[i] == selectedValue) {
          this.steuerklassen_value = i + 1;
        }
      changedValue= selectedValue; //this will change the value
    });
  },
),
var变化值;
下拉按钮(
提示:文本(hintValue),
iconSize:20,
value:changedValue,//添加此参数
条目:steuerklassen.map((字符串dropDownStringItem){
返回下拉菜单项(
值:dropDownStringItem,
子项:文本(dropDownStringItem),
);
}).toList(),
一旦更改:(selectedValue){
设置状态(){

对于(int i=1;i您可以复制下面的粘贴运行完整代码
您的代码有运行时错误
范围错误(索引):无效值:不在包含范围内

你可以从

for (int i = 1; i <= steuerklassen.length; i++)
for(int i=1;i_MyHomePageState();
}
类_MyHomePageState扩展状态{
int steuerklassen_值=1;
字符串selectedValue=“Steuerklasse”;
字符串hintValue=“Steuerklasse 1”;
最终steuerklassen=['I','II','III','IV','V','VI'];
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:中(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
下拉按钮(
提示:文本(“$hintValue”),
iconSize:20,
条目:steuerklassen.map((字符串dropDownStringItem){
返回下拉菜单项(
值:dropDownStringItem,
子项:文本(dropDownStringItem),
);
}).toList(),
一旦更改:(selectedValue){
设置状态(){
for(int i=0;i
谢谢!工作如期,很高兴能提供帮助。如果有帮助,请将此标记为答案。谢谢。
for (int i = 0; i < steuerklassen.length; i++)    
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int steuerklassen_value = 1;
  String selectedValue = "Steuerklasse";
  String hintValue = "Steuerklasse 1";
  final steuerklassen = ['I', 'II', 'III', 'IV', 'V', 'VI'];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            DropdownButton<String>(
              hint: Text("$hintValue"),
              iconSize: 20,
              items: steuerklassen.map((String dropDownStringItem) {
                return DropdownMenuItem(
                  value: dropDownStringItem,
                  child: Text(dropDownStringItem),
                );
              }).toList(),
              onChanged: (selectedValue) {
                setState(() {
                  for (int i = 0; i < steuerklassen.length; i++)
                    if (steuerklassen[i] == selectedValue) {
                      this.steuerklassen_value = i + 1;
                    }
                  hintValue = "Steuerklasse $selectedValue";
                  return steuerklassen_value;
                });
              },
            ),
          ],
        ),
      ),
    );
  }
}