AbdullahChauhan / custom-dropdown

Custom dropdown widget allows to add highly customizable dropdown widget in your projects. Features includes Search on list data, Network search, Multi-selection and many more.
https://pub.dev/packages/animated_custom_dropdown
BSD 3-Clause "New" or "Revised" License
158 stars 73 forks source link

[Question] Enabling/Disabling items to be selected #80

Open omi-ramdelatina opened 5 months ago

omi-ramdelatina commented 5 months ago

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),
      ),
    );
  }
}