I want to show a hint text in DropdownButtonFormField. However, when I add a hint text into InputDecoration, it doesn't get displayed since Getx enforces an initial value. Is there any way I can show a hint text instead of the initial value using Getx? I need this because if the user doesn’t select any city from the list I want to display a warning to them instead of accepting the default city as their choice. Thanks in advance.
My controller class:
class DropdownMenuController extends GetxController {
final selectedCity = "Berlin".obs;
final List<String> cityList = [
'Berlin',
'Paris',
'Rome',
];
void setSelectedCity(String value) {
selectedCity.value = value;
}
}
View class:
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),
),
);