custom-dropdown
custom-dropdown copied to clipboard
[Question] Enabling/Disabling items to be selected
Very nice work @AbdullahChauhan!
Problem: I want to enable/disable items inside the dropdown list, it must not be selected based on an index/condition but it should still show inside the list
Sample code:
class MyFieldWidget extends StatefulWidget {
const MyFieldWidget({super.key});
@override
State<MyFieldWidget> createState() => _MyFieldWidgetState();
}
class _MyFieldWidgetState extends State<MyFieldWidget> {
String? _value;
final ctrl = TextEditingController();
final items = List<DropdownMenuItem<String>>.generate(
10,
(i) {
final idx = i + 2;
if (idx == 5) {
return DropdownMenuItem(
value: '$idx',
enabled: false,
onTap: null, // ???
child: Text(
'$idx',
style: const TextStyle(), // Disabled styling
),
);
}
return DropdownMenuItem(
value: '$idx',
child: Text('$idx'),
);
},
);
@override
Widget build(BuildContext context) {
return CustomDropdown(
controller: ctrl,
hintText: '--',
items: items,
onChanged: (value) {
if (value != null) {
setState(() {
_value = value.value;
});
}
},
decoration: CustomDropdownDecoration(
closedSuffixIcon: const Icon(
Icons.access_time,
color: Colors.grey,
),
expandedSuffixIcon: const SizedBox(),
closedBorder: Border.all(color: Colors.grey),
closedBorderRadius: BorderRadius.circular(8.0),
),
);
}
}