Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make condition on dropdownMenu

I need advice. I have made condition on dropdownMenu, at first it works, but since I made a change in my code, it didn’t work again.
The problem is the user cannot choose the menu.

You can look at the UI that I attach.

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

enter image description here

Sorry I can’t make a record with video.

And here is my code that I made so far:

There is a variable below BuildContext, far in the top:

Widget build(BuildContext context) {
String selectedCategoryArea = '';

Then this is the dropdownMenu:

child: DropdownButton<String>(
                        icon: Padding(
                          padding: const EdgeInsets.only(right: 10, top: 8),
                          child: SvgPicture.asset(
                            Assets.icons.dropdownIcon.path,
                            fit: BoxFit.scaleDown,
                          ),
                        ),
                        style: body1(color: ColorName.grey),
                        items: <String>[
                          'Block',
                          'Fining Line',
                        ].map((String value) {
                          return DropdownMenuItem(
                            value: value,
                            child: Text(value),
                          );
                        }).toList(),
                        hint: Padding(
                          padding: const EdgeInsets.only(top: 8, left: 10),
                          child: Text(
                              style: body1(color: ColorName.grey),
                              selectedCategoryArea.isEmpty
                                  ? 'Category Area'
                                  : selectedCategoryArea),
                        ),
                        borderRadius: BorderRadius.circular(10),
                        underline: const SizedBox(),
                        isExpanded: true,
                        onChanged: (value) {
                          if (value != null) {
                            setState(() {
                              selectedCategoryArea = value;
                            });
                          }
                        },
                      ),

>Solution :

The problem is that you have defined selectedCategoryArea within your build method:

Widget build(BuildContext context) {
var selectedCategoryArea = "";

So, every setState it gets reset.

To fix the issue, declare selectedCategoryArea in your _state class:

class _TestState extends State<Test> {
  var selectedCategoryArea = "";
  @override
  Widget build(BuildContext context) {
    return Scaffold(
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading