下拉列表未显示所选值

时间:2021-03-26 04:15:16

标签: flutter

DropdownButton(
              hint: Text('Select Priority Level', style: GoogleFonts.dosis() ,),
              dropdownColor: Colors.blueGrey,
              value: selectedValue,
              onChanged: (newValue){
                setState(() {
                  selectedValue = newValue;
                  priority = selectedValue;
                });
              },
              items: listPriority.map((valueItem){
                return DropdownMenuItem<String>(
                    value: valueItem,
                    child: Text(valueItem),
                );
              }).toList(),
            )

选择的值会被存储,但是当我选择一个项目时,它不会显示在下拉字段中。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

这是我做的示例代码

初始化变量

int _selectedjobcategory;

代码

Flexible(
                    flex: 0,
                    child: Padding(
                      padding: EdgeInsets.only(left: 10, right: 10, top: 15),
                      child: Container(
                        width: double.infinity,
                        height: 60,
                        decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(5),
                            border: Border.all(color: Colors.blueGrey)),
                        child: DropdownButtonFormField(
                          isExpanded: true,
                          itemHeight: 50,
                          icon: Icon(Icons.arrow_drop_down),
                          iconSize: 40,
                          // underline: SizedBox(),
                          hint: Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: Text(
                              "Select Enquiry Type",
                              style: TextStyle(fontSize: 15, color: Colors.black),
                            ),
                          ),
                          value: _selectedjobcategory,
                          onChanged: (newValues) {
                            setState(() {
                              _selectedjobcategory = newValues;
                            });
                           
                          },
                          items: jobcategory.map((jobs) {
                            return DropdownMenuItem(
                              child: Padding(
                                padding: const EdgeInsets.all(8.0),
                                child:
                                Text(jobs.name, style: TextStyle(fontSize: 15)),
                              ),
                              value: jobs.id,
                            );
                          }).toList(),
                        ),
                      ),
                    ),
                  ),

答案 1 :(得分:1)

这是一个自定义下拉列表类,您可以在任何地方调用它

    import 'package:flutter/material.dart';

    // ignore: must_be_immutable
    class DropDownClass extends StatelessWidget {
      var _hint;
      var _val;
      List _list = new List();
      bool _border;
      Color _underLineColor, _dropDownColor;

      List get list => _list;
      dynamic Function(dynamic) _listener;

      DropDownClass({List list,
        var hint,
        Color underLineColor,
        Color dropDownColor,
        Color textColor,
        double fontSize,
        bool icon,
        var val,
        int type,
        bool border = true,
        dynamic Function(dynamic) listener,})
          : _list = list,
            _hint = hint,
            _underLineColor = underLineColor,
            _dropDownColor = dropDownColor,
            _val = val,
            _border = border,
            _listener = listener;

      @override
      Widget build(BuildContext context) {
        return DropdownButtonHideUnderline(
          child: DropdownButtonFormField<String>(
            value: _val,
            dropdownColor: _dropDownColor ?? Colors.white,
            decoration:_border == true?  InputDecoration(
                enabledBorder: UnderlineInputBorder(
                  borderSide: BorderSide(
                    color: _underLineColor ?? Theme
                        .of(context)
                        .hintColor,
                    width: 1.0,
                  ),
                )
            ):InputDecoration(
                border: InputBorder.none,
                fillColor: Colors.grey[400],
                filled: true),
            isExpanded: true,
            hint: Text(_hint),
            items: list.map((item) {
              return DropdownMenuItem<String>(
                value: item,
                child: new Text(item,),
              );
            }).toList(),
            onChanged: (value) {
              _val = value;
              if (_listener != null) _listener.call(value);
              //   return val;
            },
          ),
        );
      }
    }