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(),
)
选择的值会被存储,但是当我选择一个项目时,它不会显示在下拉字段中。我怎样才能解决这个问题?
解决方案
这是一个自定义的下拉列表类,您可以在任何地方调用它
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;
},
),
);
}
}
推荐阅读
- adsense - Adsense 不显示
- android - Android片段中的动态背景
- python - 逻辑回归的损失不收敛到 0
- python - python 结构中的填充字节
- r - 从R中的单个交易日期列中提取购买和销售日期
- python - Django 密钥泄露
- flutter - 尝试将样式主题添加到 BottomNavigationBarItem 中的标题时,我收到“无效的常量值”
- javascript - 我可以让 console.log() 打印一个类的自定义消息吗?
- python - 用 sns 绘制箱线图
- selenium-webdriver - 如何使用 WebDriver 调用预定义的 Geb Page 元素?