0

I have a DropdownButton which displays user type.

List<String> items = ['Engineer', 'Technician', 'Sales'];
String? currentSelectedValue;

                      child: DropdownButtonHideUnderline(
                        child: Padding(
                          padding:
                              const EdgeInsets.symmetric(horizontal: 20.0),
                          child: DropdownButton<String>(
                            dropdownColor: Colors.blue.shade100,
                            isExpanded: true,
                            hint: Text('Select the user Type'),
                            onChanged: (newValue) {
                              setState(() {
                                currentSelectedValue = newValue;
                              });
                              print(currentSelectedValue);
                            },
                            items: items.map((String value) {
                              return DropdownMenuItem(
                                value: value,
                                child: Text(
                                  value,
                                  style: TextStyle(color: Colors.black),
                                ),
                              );
                            }).toList(),
                            value: currentSelectedValue,
                          ),
                        ),
                      ),

I can see the list, but when I select a value, it is not displaying on the Text portion of the DropdownButton. I could see the selected value printed in the console. Can anyone help me to find the mistake?

1
  • Can you include full widget Commented Nov 2, 2022 at 17:15

1 Answer 1

1

Make sure to put currentSelectedValue outside the build method.

class Ft extends StatefulWidget {
  const Ft({super.key});

  @override
  State<Ft> createState() => _FtState();
}

class _FtState extends State<Ft> {
  List<String> items = ['Engineer', 'Technician', 'Sales'];
  String? currentSelectedValue;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: DropdownButtonHideUnderline(
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 20.0),
          child: DropdownButton<String>(
            dropdownColor: Colors.blue.shade100,
            isExpanded: true,
            hint: Text('Select the user Type'),
            onChanged: (newValue) {
              setState(() {
                currentSelectedValue = newValue;
              });
              print(currentSelectedValue);
            },
            items: items.map((String value) {
              return DropdownMenuItem(
                value: value,
                child: Text(
                  value,
                  style: TextStyle(color: Colors.black),
                ),
              );
            }).toList(),
            value: currentSelectedValue,
          ),
        ),
      ),
    );
  }
}

Sign up to request clarification or add additional context in comments.

1 Comment

Glad to help, you can find more about UI

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.