Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
omi-ramdelatina opened this issue Jun 14, 2024 · 0 comments
Open

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

omi-ramdelatina opened this issue Jun 14, 2024 · 0 comments

Comments

@omi-ramdelatina
Copy link

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),
      ),
    );
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant