英文:
How to make dropdown visibility in flutter
问题
I have a case where I want to make a dropdown containing options where when the user selects the KHS option, a new dropdown will appear below containing the INT list.
我有一个情况,我想创建一个包含选项的下拉菜单,当用户选择KHS选项时,下面会出现一个包含INT列表的新下拉菜单。
how do i make it i am a bit confused here.
我应该如何做呢,我有点困惑。
maybe someone here can help me or have a recommendation for an article I've been looking for but haven't found anything suitable.
也许这里有人可以帮助我,或者推荐一篇我一直在寻找但没有找到合适内容的文章。
Thank you in advance.
提前谢谢。
initialization
初始化
List listItem = [
'Transkrip Sementara',
'KHS',
];
List
1,
2,
3,
4,
5,
6,
7,
8,
];
in widget
在小部件中
const SizedBox(
height: 8,
),
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
hint: const Text('Pilih Jenis Transkrip'),
value: selectedValue,
onChanged: (newValue) {
setState(() {
selectedValue = newValue!;
selectSmt = null!;
});
},
items: listItem.map((valueItem) {
return DropdownMenuItem
value: valueItem,
child: Text(
valueItem,
),
);
}).toList(),
),
),
),
),
Visibility(
visible: selectedValue != null,
child: DropdownButton
value: selectSmt,
items: listItem2
.where((element) => element?.contains(selectedValue))
.map((value) {
return DropdownMenuItem
value: value,
child: Text(value as String),
);
}).toList(),
onChanged: (value) {
setState(() {
selectSmt = value!;
});
},
),
),
and an error occurred
发生了错误
what I want
我想要的效果
英文:
I have a case where I want to make a dropdown containing options where when the user selects the KHS option, a new dropdown will appear below containing the INT list.
how do i make it i am a bit confused here.
maybe someone here can help me or have a recommendation for an article I've been looking for but haven't found anything suitable.
Thank you in advance.
> initialization
List listItem = [
'Transkrip Sementara',
'KHS',
];
List<int> listItem2 = [
1,
2,
3,
4,
5,
6,
7,
8,
];
> in widget
const SizedBox(
height: 8,
),
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!;
selectSmt = null!;
});
},
items: listItem.map((valueItem) {
return DropdownMenuItem<String>(
value: valueItem,
child: Text(
valueItem,
),
);
}).toList(),
),
),
),
),
Visibility(
visible: selectedValue != null,
child: DropdownButton<int>(
value: selectSmt,
items: listItem2
.where((element) => element?.contains(selectedValue))
.map((value) {
return DropdownMenuItem<int>(
value: value,
child: Text(value as String),
);
}).toList(),
onChanged: (value) {
setState(() {
selectSmt = value!;
});
},
),
),
> what I want
答案1
得分: 2
使用map
会更容易。关键点是在更改主下拉菜单时重置第二个下拉菜单。
class DropDS extends StatefulWidget {
const DropDS({super.key});
@override
State<DropDS> createState() => _DropDSState();
}
class _DropDSState extends State<DropDS> {
List<int> listItem1 = List.generate(4, (index) => index + 10);
List<int> listItem2 = [1, 2, 3, 4, 5, 6, 7, 8];
// 使用 map 来轻松访问
late final dropDownData = {
'Transkrip Sementara': listItem1,
'KHS': listItem2
};
String? selectedValue; // 主类型
var selectSmt; // 用于 listItem1 和 listItem2
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
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!;
selectSmt = null;
});
},
items: dropDownData.keys.map((valueItem) {
return DropdownMenuItem<String>(
value: valueItem,
child: Text(valueItem),
);
}).toList(),
),
),
),
),
Visibility(
visible: selectedValue != null,
child: DropdownButton<int>(
value: selectSmt,
items: dropDownData[selectedValue]?.map((value) {
return DropdownMenuItem<int>(
value: value,
child: Text(value.toString()),
);
}).toList(),
onChanged: (value) {
selectSmt = value;
setState(() {});
},
),
),
],
));
}
}
如果你有任何其他问题,欢迎提出。
英文:
It will be easier while using map. The keypoint is resetting second dropdown while changing main dropdown.
class DropDS extends StatefulWidget {
const DropDS({super.key});
@override
State<DropDS> createState() => _DropDSState();
}
class _DropDSState extends State<DropDS> {
List<int> listItem1 = List.generate(4, (index) => index + 10);
List<int> listItem2 = [1, 2, 3, 4, 5, 6, 7, 8];
//lets use map for easy access
late final dropDownData = {
'Transkrip Sementara': listItem1,
'KHS': listItem2
};
String? selectedValue; // main type
var selectSmt; // for listItem1 and listItem2
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
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!;
selectSmt = null;
});
},
items: dropDownData.keys.map((valueItem) {
return DropdownMenuItem<String>(
value: valueItem,
child: Text(valueItem),
);
}).toList(),
),
),
),
),
Visibility(
visible: selectedValue != null,
child: DropdownButton<int>(
value: selectSmt,
items: dropDownData[selectedValue]?.map((value) {
return DropdownMenuItem<int>(
value: value,
child: Text(value.toString()),
);
}).toList(),
onChanged: (value) {
selectSmt = value;
setState(() {});
},
),
),
],
));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论