如何在DropdownButton中更改Flutter DropdownMenuItem的宽度/填充? [英] How can i change the width/padding of a Flutter DropdownMenuItem in a DropdownButton?

查看:163
本文介绍了如何在DropdownButton中更改Flutter DropdownMenuItem的宽度/填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Dropdown中更改Flutter DropdownMenuItem的宽度/填充?

How can we change the width/padding of a Flutter DropdownMenuItem in a Dropdown?

Row(
  children: <Widget>[
    Expanded(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          LightText(
            text: "Agent",
          ),
          Padding(
            padding: EdgeInsets.only(top: 3),
          ),
          Container(
            height: 27,
            child: Row(
              children: <Widget>[
                DropdownButtonHideUnderline(
                  child: DropdownButton<Agent>(
                    // isExpanded: true,
                    hint: Text(
                      agentName == null ? "" : agentName,
                      style: TextStyle(
                        fontSize: MediaQuery.of(context).size.width * 0.035,
                      ),
                    ),

                    value: selectedAgent,
                    onChanged: (Agent value) async {
                      selectedAgent = value;
                      agentName = selectedAgent.getAgentName();
                      agentId = selectedAgent.getAgentId();
                    },
                    items: agentList.map((Agent agent) {
                      return DropdownMenuItem<Agent>(
                        value: agent,
                        child: SizedBox(
                          width: 25.0,
                          child: LightText(
                            text: agent.name,
                            textColor: Colors.black,
                          ),
                        ),
                      );
                    }).toList(),
                  ),
                ),
              ],
            ),
            decoration: ShapeDecoration(
              shape: RoundedRectangleBorder(
                side: BorderSide(width: 1.0, color: lightGrey),
                borderRadius: BorderRadius.all(Radius.circular(3.0)),
              ),
            ),
          ),
        ],
      ),
    ),
    SizedBox(
      width: 30,
    ),
    TextBoxData(
      labelText: "% Commission",
      controllerText: percentageCommision,
      enableVal: true,
      borderColor: lightGrey,
    )
  ],
)

推荐答案

您可以通过将其包装在Container小部件中来控制DropdownButton中Flutter DropdownMenuItems的宽度/填充.然后只需为该Container小部件分配高度,宽度和填充.

You can control the width/padding of a Flutter DropdownMenuItems in a DropdownButton by wrapping it inside a Container widget. Then simply assign height, width and padding to that Container widget.

示例如下:

Widget dropDownButtonsColumn(List<String> list, String hint){
    return Padding(
      padding: const EdgeInsets.only(left: 40, right: 40 , bottom: 24,top:12),
      child: Container(
        height: 55,  //gives the height of the dropdown button
        width: MediaQuery.of(context).size.width, //gives the width of the dropdown button
        decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(3)),
          color: Color(0xFFF2F2F2)
        ),
        // padding: const EdgeInsets.symmetric(horizontal: 13), //you can include padding to control the menu items
        child: Theme(
            data: Theme.of(context).copyWith(
              canvasColor: Colors.yellowAccent, // background color for the dropdown items
              buttonTheme: ButtonTheme.of(context).copyWith(
                alignedDropdown: true,  //If false (the default), then the dropdown's menu will be wider than its button.
              )
            ),
            child: DropdownButtonHideUnderline(  // to hide the default underline of the dropdown button
              child: DropdownButton<String>(
                iconEnabledColor: Color(0xFF595959),  // icon color of the dropdown button
                items: list.map((String value){
                  return DropdownMenuItem<String>(
                    value: value,
                    child: Text(value,
                      style: TextStyle(
                          fontSize: 15
                      ),
                    ),
                  );
                }).toList(),
                hint: Text(hint,style: TextStyle(color: Color(0xFF8B8B8B),fontSize: 15),),  // setting hint
                onChanged: (String value){
                  setState(() {
                    bankSelected = value;  // saving the selected value
                  });
                },
                value: bankSelected,  // displaying the selected value
              ),
            )
        ),
      ),
    );
  }

输出:

希望这会有所帮助!

这篇关于如何在DropdownButton中更改Flutter DropdownMenuItem的宽度/填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆