我最近开始使用飞镖和颤振进行编程,尽管我最近想添加下拉菜单为用户提供多种选择,但我的应用程序运行一直很顺利。一切都按计划进行,但是当我从列表中选择一个值时,它不会更改框中的值,而是返回到提示或空框。任何帮助,将不胜感激!
这是我的下拉按钮代码:
Widget buildDropdownButton() { String newValue; return new Padding( padding: const EdgeInsets.all(24.0), child: new Column( mainAxisAlignment: MainAxisAlignment.start, children: <Widget>[ new ListTile( title: const Text('Frosting'), trailing: new DropdownButton<String>( hint: Text('Choose'), onChanged: (String changedValue) { newValue=changedValue; setState(() { newValue; print(newValue); }); }, value: newValue, items: <String>['None', 'Chocolate', 'Vanilla', 'ButterCream'] .map((String value) { return new DropdownMenuItem<String>( value: value, child: new Text(value), ); }).toList()), ), ], ), ); }
错误的原因是,您在声明方法变量newValue时必须在变量内部声明该变量为全局变量StatefulWidget。
newValue
StatefulWidget
String newValue; Widget buildDropdownButton() { return new Padding( padding: const EdgeInsets.all(24.0), child: new Column( mainAxisAlignment: MainAxisAlignment.start, children: <Widget>[ new ListTile( title: const Text('Frosting'), trailing: new DropdownButton<String>( hint: Text('Choose'), onChanged: (String changedValue) { newValue=changedValue; setState(() { newValue; print(newValue); }); }, value: newValue, items: <String>['None', 'Chocolate', 'Vanilla', 'ButterCream'] .map((String value) { return new DropdownMenuItem<String>( value: value, child: new Text(value), ); }).toList()), ), ], ), ); }