简体   繁体   中英

Dropdown list with condition. Flutter/Dart

This is a dropdown list I have. How do I change the code to have a condition where if the user chooses an option it will give "valueChoose_f4" that chosen option else give "valueChoose_f4" the value that is stored in "M${widget.orgUnit}". Thanks, hope my question made sense.

ListTileTheme(
                      tileColor: Colors.white,
                      child: ListTile(
                        title: Container(
                          decoration: BoxDecoration(
                            color: Colors.transparent,
                            border: Border.all(color: Colors.blueGrey),
                            borderRadius: BorderRadius.circular(10),
                          ),
                          child: Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: DropdownButtonHideUnderline(
                              child: DropdownButton<String>(
                                isExpanded: true,
                                value: valueChoose_f4,
                                hint: Text('M${widget.orgUnit}'),
                                onChanged: (newValue){
                                  setState(() {
                                    valueChoose_f4 = newValue;
                                  });
                                },
                                items: listUserType.map((map) {
                                  return DropdownMenuItem<String>(
                                    value: map['value'],
                                    child: Text(map['name']),
                                  );
                                }).toList(),
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),

You can't give a dropdown value that is not contained in your listUserType so if you want to set a default value where the dropdown initializes and not has been selected yet you can do it like:

value: valueChoose_f4 ?? 'Your_default_value'

that means when valueChoose_f4 is null, the default value will be instead the null.

String? valueChoose_f4;

ListTile(
        title: Container(
          decoration: BoxDecoration(
            color: Colors.transparent,
            border: Border.all(color: Colors.blueGrey),
            borderRadius: BorderRadius.circular(10),
          ),
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: DropdownButtonHideUnderline(
              child: DropdownButton<String>(
                isExpanded: true,
                value: valueChoose_f4 ?? listUserType.first['value'], // changed
                hint: const Text('M${widget.orgUnit}'),
                onChanged: (newValue){
                  setState(() {
                    valueChoose_f4 = newValue;
                  });
                },
                items: listUserType.map((map) {
                  return DropdownMenuItem<String>(
                    value: map['value'],
                    child: Text(map['name']),
                  );
                }).toList(),
              ),
            ),
          ),
        ),
      )

I hope this solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM