您好,我正在尝试更新警报对话框中的 DropdownButton,但它似乎没有更新 selectedValue。
这是代码:
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
final dbHelper = DataBaseHelper.instance;
int tabIndex = 1;
List<Characters> characters = List<Characters>();
List<Classes> dofusClass = List<Classes>();
String defaultValueSelect = "";
@override
void initState() {
super.initState();
populateCharacters();
getClasses();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar:
AppBar(title: Text('Dofpedia'), backgroundColor: Colors.lightGreen),
body: new Container(
child: new Stack(
children: <Widget>[
Align(
alignment: Alignment.topCenter,
child: new IconButton(
icon: Icon(CupertinoIcons.add, color: Colors.blue),
onPressed: () {
displayAndInput(context);
},
),
),
Padding(
padding: const EdgeInsets.all(40.0),
child: buildCharactersListView(context),
),
],
)),
bottomNavigationBar: new BottomNavigationBar(
currentIndex: tabIndex,
onTap: (int index) {
setState(() {
this.tabIndex = index;
});
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Equipments()),
);
},
fixedColor: Colors.lightGreen,
items: <BottomNavigationBarItem>[
new BottomNavigationBarItem(
icon: new Icon(CupertinoIcons.settings),
title: new Text("Equipements"),
),
new BottomNavigationBarItem(
icon: new Icon(CupertinoIcons.profile_circled),
title: new Text("Personnages"),
),
new BottomNavigationBarItem(
icon: new Icon(CupertinoIcons.book),
title: new Text("Encyclopédie"),
)
],
),
);
}
Future displayAndInput(BuildContext context) async{
TextEditingController tec = TextEditingController();
List<String> namesValues = dofusClass.map((f) => f.name).toList();
Characters c = new Characters();
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: new Text("Nom du personnage"),
content: new TextField(
controller: tec,
),
actions: <Widget>[
Container(
child: Column(
children: <Widget>[
DropdownButton<String>(
value: defaultValueSelect,
style: TextStyle(color: Colors.lightGreen),
underline: Container(
height: 1,
color: Colors.lightGreen,
),
items: namesValues
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value)
);
}).toList(),
onChanged: (String newValue) {
setState((){
defaultValueSelect = newValue;
c.type = newValue;
c.url = dofusClass.firstWhere((d)=> d.name == newValue).maleImg;
});
},
),
FloatingActionButton.extended(
shape: new RoundedRectangleBorder(),
backgroundColor: Colors.lightGreen,
icon: Icon(Icons.save),
label: Text("Sauvegarde"),
onPressed: () {
//Sauvegarde dans la base de données
c.name = tec.value.text;
insertName(c);
Navigator.of(context).pop();
setState(() {
characters.add(c);
});
},
),
],
),
)
],
);
});
不过,SetState 可以完美地使用角色名称。当我更改选择值并单击输入名称时,下拉按钮选择的值会更新。
如果您有任何想法,非常欢迎您的帮助:)
编辑:抱歉缩进不好,稍后会尝试解决这个问题:/