如何在颤振中获取下拉按钮的选定索引 [英] how to get selected index of dropdownbutton in flutter
本文介绍了如何在颤振中获取下拉按钮的选定索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取Flutter下拉列表的selectedIndex
How to get selectedIndex of dropdown in flutter,
在dropdownbutton中,没有属性可以获取所选索引,如果有如何获取所选索引,我的代码看起来像这样:
In dropdownbutton there is no property to get selected index, if there how to get the selected index and my code look like this:
new DropdownButton( hint:new Text("Select a users"),value: selectedUser,
onChanged: (String newValue) {
setState(() {
selectedUser = newValue;
});
},
items: userInfoToMap.map((ListOfUsers value) {
return new DropdownMenuItem<String>(
value: value.name,
child:new Text(value.name,style: new TextStyle(color: Colors.black))
);
})
.toList(),
),
),),
推荐答案
您可能应该使用自定义模型对象(例如 Use r
)作为 DropdownButton
的类型。
You should probably use a custom model object (e.g. User
) as the type for DropdownButton
.
导入'package:flutter / material.dart';
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class User {
const User(this.name);
final String name;
}
class MyApp extends StatefulWidget {
State createState() => new MyAppState();
}
class MyAppState extends State<MyApp> {
User selectedUser;
List<User> users = <User>[const User('Foo'), const User('Bar')];
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
body: new Center(
child: new DropdownButton<User>(
hint: new Text("Select a user"),
value: selectedUser,
onChanged: (User newValue) {
setState(() {
selectedUser = newValue;
});
},
items: users.map((User user) {
return new DropdownMenuItem<User>(
value: user,
child: new Text(
user.name,
style: new TextStyle(color: Colors.black),
),
);
}).toList(),
),
),
),
);
}
}
这篇关于如何在颤振中获取下拉按钮的选定索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文