这是一个显示api数据的下拉列表,共有6个主要类别,我需要与api数据一起添加ALL,或者我想显示ALL或如果用户未选择任何内容,则选择ALL,我正尝试遵循此操作 https://stackoverflow.com/a/54948635/13418165,但我对此颇为困惑,我可以向其显示api数据没有问题,显示提示是我需要暂时在下拉菜单中连同api数据一起使用
变量初始化
List<CategoryModel> job_category_model = [];
String _selectedjobcategory;
查看
Flexible( flex: 0,
child: Padding(
padding: EdgeInsets.only(left: 10, right: 10, top: 15),
child: Container(
height: 45,
width: 320,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(5),
border: Border.all(
color: Color.fromRGBO(92, 52, 76, 1))),
child: DropdownButton<String>(
isExpanded: true,
itemHeight: 50,
underline: SizedBox(),
value: _selectedjobcategory,
items: job_category_model.map((maincateogry) {
return DropdownMenuItem<String>(
value: maincateogry.id,
child: new Text(
language != "ml" ? maincateogry.english: maincateogry.malayalam,
style: TextStyle(
fontSize: 15,
color: Color.fromRGBO(
92, 52, 76, 1)),
),
);
}).toList(),
hint: Padding(
padding: const EdgeInsets.all(8.0),
child: Text("ALL"),
),
onChanged: (newValues) {
setState(() {
_selectedjobcategory = newValues;
GetSubCategories(_selectedjobcategory);
subcategoryvisible=true;
});
}),
),
),
),
API
Future<String> GetWorkCategories() async {
Future auth_token = SharedPrefrence().getToken();
auth_token.then((data) async {
token = data;
var response = await http.post(
Urls.MENU,
headers: {"Content-Type": "application/json", "Authorization": token},
);
print("response:" + response.body.toString());
Map<String, dynamic> value = json.decode(response.body);
var message = value['status'];
try {
if (response.statusCode == 200) {
try {
if (message == true) {
var menu_list = value['doc'];
for (int i = 0; i < menu_list.length; i++) {
var data = menu_list[i];
job_category_model.add(CategoryModel.fromJson(data));
}
setState(() {
print("UI Updated");
});
} else {
final snackBar = SnackBar(content: Text(message));
_scaffoldKey.currentState.showSnackBar(snackBar);
}
} catch (e) {
e.toString();
}
} else {
final snackBar = SnackBar(content: Text(message));
_scaffoldKey.currentState.showSnackBar(snackBar);
}
} catch (e) {
e.toString();
}
});
}
模型类
class CategoryModel{
String id = "";
String menu_id = "";
String malayalam = "";
String english = "";
String image = "";
CategoryModel(
{this.id, this.menu_id, this.malayalam, this.english, this.image});
CategoryModel.fromJson(json)
: id = json['_id'].toString(),
menu_id = json['menuId'].toString(),
malayalam = json['translations']['malayalam'].toString(),
english = json['translations']['english'].toString(),
image = json['image'].toString();
}