0

When I select one of the items in the DropdownButton it is suppose to change from the hint text to the selected item.

It keeps on showing the hint text, like I did not select any thing. Although I print the selection to the console and I can see my selection there.

Your help will be appreciated. (I think I have read every post on here)

class DeviceSelectionPage extends StatefulWidget {
  const DeviceSelectionPage({Key? key}) : super(key: key);

  @override
  State<DeviceSelectionPage> createState() => _DeviceSelectionPageState();
}

class _DeviceSelectionPageState extends State<DeviceSelectionPage> {
  DateTime _dateTime = DateTime.now();
  bool _isSelected = false;

  @override
  Widget build(BuildContext context) {
    final List<String> itemsList = [
      'A1',
      'A22',
      '5E70CA9163C3EF2B',
      '1E2BD5D7D19EC75D',
      '9264ED34F8CF8A8F',
      '32D919C3F8ECC18E',
      '4ED07890CAC5A184',
      'F60AB38638788275',
      'AE65976D5F408107',
      '6EF6D048033567D5',
    ];
    String? selectedValue;

    return Scaffold(
        appBar: AppBar(
          title: const Text('Device Selection page'),
        ),
        body: Container(
            child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                  //width: 240,
                  padding: const EdgeInsets.symmetric(horizontal: 24.0),
                  decoration: BoxDecoration(
                      border:
                          Border.all(color: Colors.lightBlueAccent, width: 1.0),
                      borderRadius: kBorderRadius),
                  child: DropdownButtonHideUnderline(
                    child: DropdownButton(
                      hint: Text(
                        'Devices',
                        style: TextStyle(color: Colors.white54, fontSize: 20),
                      ),
                      style:
                          const TextStyle(color: Colors.orange, fontSize: 20),
                      borderRadius: kBorderRadius,
                      iconSize: 40,
                      elevation: 16,
                      onChanged: (value) {
                        setState(() {
                          selectedValue = value.toString();
                          setState(() {
                            selectedValue;
                            print(selectedValue);
                          });
                        });
                      },
                      value: selectedValue,
                      items: itemsList.map((listItem) {
                        return DropdownMenuItem<String>(
                          value: listItem,
                          child: Text(listItem),
                        );
                      }).toList(),
                    ),
                  ),
                )
              ],
            ),

When I select a device ('A22') I do see it getting printed in the console

D/EGL_emulation( 8542): app_time_stats: avg=1279.83ms min=1279.83ms max=1279.83ms count=1
I/flutter ( 8542): A22
2
  • Try setting value as null. value: null, Commented Aug 25, 2022 at 12:16
  • Thanks @Prashant , where must I set value: null? Commented Aug 25, 2022 at 13:05

2 Answers 2

1

Declare this outside of the build method.

String? selectedValue;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, outside of the build method works
0

I will suggest you that you are calling setState method twice you can update your all value in one setState method.

Comments

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.