简体   繁体   English

Flutter DropDownButton自适应宽度

[英]Flutter DropDownButton Adapt Width

I have a DropDownButton , the width default is the longest DropDownMenuItem.我有一个DropDownButton ,宽度默认是最长的 DropDownMenuItem。

在此处输入图像描述

在此处输入图像描述

But I want that for example when the value is Âge: Tout the DropDownButton adapt his width to the text.但我希望例如当值为Âge: ToutDropDownButton调整其宽度以适应文本。

return DropdownButton(
        icon: const Icon(Icons.arrow_drop_down_rounded, color: Colors.white),
        dropdownColor: Colors.orange,
        value: _filterCategory,
        items: _categories.map((String e) {
          return DropdownMenuItem(
              value: e,
              child: Text("Catégories : "+e,style: const TextStyle(fontSize: 14, color: Colors.white))
          );
        }).toList(),
        onChanged: (value) {
          setState(() {
            //DATA
          });
        }
    );

Try using an input Decoration and a SizedBox尝试使用输入 Decoration 和 SizedBox

InputDecorator createTemplateWidget() {
    DropdownButton<MyClass> myDropdownButton =
        DropdownButton<MyClass>(
            items: items,
            isDense: true,
            value: _template,
            isExpanded: true,
            onChanged: (MyClass value) {
              setState(() {
                this._myVariable = value;
              });
            });
    return InputDecorator(
        decoration: const InputDecoration(
            icon: const Icon(Icons.location_city),
            hintText: "City",
            labelText: "Select a Type"),
        child: Row(mainAxisAlignment: MainAxisAlignment.start, children: [
      Container(width: 600.0, child: myDropdownButton),
      SizedBox()
    ]));
  }
}

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

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