简体   繁体   English

如何将字符串添加到下拉按钮

[英]How to add String to Dropdownbutton

I have map<dynamic, String> with units and names.我有带有单位和名称的 map<dynamic, String> 。

Map timeUnits = <dynamic, String>{
  TimeUnit.second: 'second',
  TimeUnit.minute: 'minute',
  TimeUnit.hour: 'hour',
  TimeUnit.day: 'day',
  TimeUnit.week: 'week',
  TimeUnit.month: 'month',
  TimeUnit.calendarYear: 'year',
};

How to add String to Dropdownbutton?如何将字符串添加到下拉按钮? Dropdownbutton shows this problem "The argument type 'Map<dynamic, dynamic>' can't be assigned to the parameter type 'List'." Dropdownbutton 显示此问题“无法将参数类型 'Map<dynamic, dynamic>' 分配给参数类型 'List'。” Dropdownbutton code:下拉按钮代码:

child: DropdownButton(
              isExpanded: true,
              underline: Container(),
              value: currentUnits[widget.convertorType],
              icon: Padding(
                padding: const EdgeInsets.only(right: 8.0, bottom: 6),
                child: Image.asset("assets/icons/down-arrow2.png"),
              ),
              iconSize: 35,
              elevation: 20,
              style: TextStyle(
                fontSize: 18,
                color: Color.fromRGBO(114,137,218, 1)
              ),
              items: widget.items
                  .map(
                    (e) => DropdownMenuItem(
                      child: e == TimeUnit.calendarYear
                          ? Text("year")
                          : e.toString().length == 3
                              ? Text(e)
                              : Text(e.toString().split(".")[1]),
                      value: e,
                    ),
                  )
                  .toList(),
              onChanged: (newValue) {
                setState(() {
                  updateCurrentUnit(widget.convertorType, newValue);
                  widget.calculate();
                });
              },
            ),

first, you have to convert your Map to a list then create a list of dropDownMenuItem and then send it over to dropdown widget首先,您必须将 Map 转换为列表,然后创建 dropDownMenuItem 列表,然后将其发送到下拉小部件

for example in the code below provinces is a list and provincesItems is a List of dropdownMenueItem, i fill provincesItems with items I desire from provinces list and then send it over to dropDown Widget例如,在下面的代码中,provinces 是一个列表,provincesItems 是一个 dropdownMenueItem 的列表,我用我想要从省份列表中获取的项目来填充 prosesItems,然后将其发送到 dropDown Widget

for (int i = 0; i < provinces.length; i++) {
      provincesItems.add(DropdownMenuItem(
        value: i,
        child: Column(
          children: [
            Text(
              provinces[i].persianName,
              style: TextStyle(
                  color: Colors.black, fontWeight: FontWeight.normal),
            ),
          ],
        ),
      ));
    }

As per my understanding, it is due to the (items: widget.item) error so change it to (items: widget.items.entries) and e to e.key or e.value据我了解,这是由于 (items: widget.item) 错误,因此将其更改为 (items: widget.items.entries) 并将 e 更改为 e.key 或 e.value

 DropdownButton(
          isExpanded: true,
          underline: Container(),
          value: currentUnits[widget.convertorType],
          icon: Padding(
            padding: const EdgeInsets.only(right: 8.0, bottom: 6),
            child: Image.asset("assets/icons/down-arrow2.png"),
          ),
          iconSize: 35,
          elevation: 20,
          style: TextStyle(
            fontSize: 18,
            color: Color.fromRGBO(114,137,218, 1)
          ),
          items: widget.items.entries
              .map(
                (e) => DropdownMenuItem(
                  child: e.key == TimeUnit.calendarYear
                      ? Text("year")
                      : e.toString().length == 3
                          ? Text(e.key)
                          : Text(e.value),
                  value: e,
                ),
              )
              .toList(),
          onChanged: (newValue) {
            setState(() {
              updateCurrentUnit(widget.convertorType, newValue);
              widget.calculate();
            });
          },
        )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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