首页 > 解决方案 > 下拉菜单未显示所选值

问题描述

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(),
            )

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

标签: flutter

解决方案


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

    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;
            },
          ),
        );
      }
    }

推荐阅读