我想在DropdownButtonFormField中显示提示文本。然而,当我向InputDecoration添加提示文本时,它不会显示,因为Getx强制执行初始值。有没有什么方法可以显示提示文本而不是使用Getx的初始值?我需要这个,因为如果用户没有从列表中选择任何城市,我想向他们显示一个警告,而不是接受默认城市作为他们的选择。先谢了。
我的控制器类:
class DropdownMenuController extends GetxController {
final selectedCity = "Berlin".obs;
final List<String> cityList = [
'Berlin',
'Paris',
'Rome',
];
void setSelectedCity(String value) {
selectedCity.value = value;
}
}
字符串
视图类:
DropdownMenuController controller = DropdownMenuController();
class InfoDropdown extends StatelessWidget {
const InfoDropdown({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Padding(
padding: EdgeInsets.fromLTRB(10, 2, 10, 2),
child: Text(
'City',
style: TextStyle(
color: Colors.indigo,
),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(10, 2, 10, 20),
child: SizedBox(
height: 50,
child: Obx(() => DropdownButtonFormField(
decoration: kDropdownMenuDecoration //There is a hint text here I can't display,
onChanged: (newValue) {
controller.setSelectedCity(newValue as String);
},
value: controller.selectedCity.value,
items: controller.cityList.map((selectedItem) {
return DropdownMenuItem(
value: selectedItem,
child: Text(
selectedItem,
),
);
}).toList(),
)),
),
),
],
);
}
}
//kDropdownMenuDecoration:
InputDecoration(
contentPadding: EdgeInsets.fromLTRB(10, 2, 10, 2),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.zero,
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.zero,
),
borderSide: BorderSide(color: Colors.grey),
),
);
型
1条答案
按热度按时间yi0zb3m41#
在DropdownButtonFormField小部件中使用
hint: Text("Select the Value")
。确保将此
final selectedCity = "Berlin".obs;
更新为late String selectedCity
;