英文:
Value not selected in dropdown flutter
问题
我尝试在Flutter中创建一个下拉菜单,并根据Flutter的文档进行了调整,也尝试了一篇文章中的方法,但当我在我的应用程序中尝试选择其中一个值时,我想要的值不会出现,我必须再次单击,然后新选择的数据才会出现。
如何解决这个问题?
List listItem = [
'Transkrip Sementara',
'KHS',
];
String? selectedValue;
// 在Widget中
return Scaffold(
appBar: AppBar(
backgroundColor: primaryColor,
title: const Center(
child: Text(
'Transkrip Nilai',
),
),
actions: [
IconButton(
icon: const Icon(Icons.print),
onPressed: () {
showDialog(
context: context,
builder: (context) {
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
elevation: 5,
child: SizedBox(
width: double.infinity,
child: Padding(
padding: const EdgeInsets.all(14),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Center(
child: Text(
'Cetak Transkrip/KHS',
style: bold5,
),
),
const Divider(),
Text(
'Pilih Jenis Transkrip',
style: regular5,
),
Container(
width: double.infinity,
height: 50,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: Colors.grey,
),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
hint: const Text('Pilih Jenis Transkrip'),
value: selectedValue,
onChanged: (newValue) {
setState(() {
selectedValue = newValue;
});
},
items: listItem.map((valueItem) {
return DropdownMenuItem<String>(
value: valueItem,
child: Text(
valueItem,
),
);
}).toList(),
),
),
),
),
],
),
),
),
);
},
);
},
),
],
),
);
请检查上述代码,以确保与您的Flutter应用程序的其他部分兼容,并确保已正确处理下拉菜单的值更改。
英文:
I tried to make a dropdown in Flutter and I have adjusted it to the documentation from Flutter and also tried from the article but when I try it in my application when I select one of the values I want it doesn't appear, I have to click again and the new selected data appears.
how do i solve this?
List listItem = [
'Transkrip Sementara',
'KHS',
];
String? selectedValue;
> in widget
return Scaffold(
appBar: AppBar(
backgroundColor: primaryColor,
title: const Center(
child: Text(
'Transkrip Nilai',
),
),
actions: [
IconButton(
icon: const Icon(Icons.print),
onPressed: () {
showDialog(
context: context,
builder: (context) {
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
elevation: 5,
child: SizedBox(
width: double.infinity,
child: Padding(
padding: const EdgeInsets.all(14),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Center(
child: Text(
'Cetak Transkrip/KHS',
style: bold5,
),
),
const Divider(),
Text(
'Pilih Jenis Transkrip',
style: regular5,
),
Container(
width: double.infinity,
height: 50,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: Colors.grey,
),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
hint: const Text('Pilih Jenis Transkrip'),
value: selectedValue,
onChanged: (newValue) {
setState(() {
selectedValue = newValue;
});
},
items: listItem.map((valueItem) {
return DropdownMenuItem<String>(
value: valueItem,
child: Text(
valueItem,
),
);
}).toList(),
),
),
),
),
答案1
得分: 1
使用 StatefulBuilder
参考此链接:https://api.flutter.dev/flutter/widgets/StatefulBuilder-class.html
showDialog(
context: context,
builder: (context) {
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
child: StatefulBuilder( // 将此部分添加到您的代码中
builder: (BuildContext context, StateSetter setState) {
return 您的对话框内容
.....
英文:
use StatefulBuilder
read this for reference: https://api.flutter.dev/flutter/widgets/StatefulBuilder-class.html
showDialog(
context: context,
builder: (context) {
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
child: StatefulBuilder( // add this to your code
builder: (BuildContext context, StateSetter setState) {
return Your content dialog
.....
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论