flutter 下拉按钮菜单项宽度

l0oc07j2  于 2023-05-29  发布在  Flutter
关注(0)|答案(1)|浏览(167)

有没有办法在点击下拉按钮后减少下拉菜单窗口的宽度?我只想在点击dropdownbutton并保持dropdownbutton的isExpanded = true后改变窗口的宽度。我试着用容器 Package 它并给出宽度,但它也改变了下拉按钮的宽度。我不想使用任何外部软件包。你可以在输出图像中看到我想从下拉列表中删除红色正方形空间。任何建议或解决方案都将是超级有帮助的....提前感谢

return Container(
   // width: 300.0,
   
      child: DropdownButton(
isExpanded = true,
        value: selectedname,
        items: listOfDropdownMenuItems.map(
          (name) {
          return DropdownMenuItem<String>(
            value: name,
            child: Text(
              name,
             
            ),
          );
        }).toList(),),
        onChanged:(newValue) {
          setname(newValue!);
        },
        underline: Container(),
      ),
    ),
);

[输出图像:](https://i.stack.imgur.com/FISMs.png

gwo2fgha

gwo2fgha1#

这里有一个方法来解决您的问题

Widget customDropDown(BuildContext context) {
    return Container(
      /// Set the width of the DropdownButton
      width: MediaQuery.of(context).size.width,
      margin: const EdgeInsets.all(15),
      child: PopupMenuButton<String>(
        child: Container(
          padding: EdgeInsets.all(10),
          decoration: BoxDecoration(
            /// here you can control border and it's radius
            border: Border.all(color: Colors.grey),
            borderRadius: BorderRadius.circular(4),
          ),
          child: Row(
            children: [
              Expanded(
                child: Text(
                  _selectedItem.isNotEmpty ? _selectedItem : 'Select an item',
                  overflow: TextOverflow.ellipsis,
                ),
              ),
              Icon(Icons.arrow_drop_down),
            ],
          ),
        ),
        offset: Offset(0, 40), // Adjust the offset as needed
        itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
          PopupMenuItem<String>(
            value: 'Option 1',
            child: Container(
              width: 120, // Set the width of the PopupMenuItem
              child: Text('Option 1'),
            ),
          ),
          PopupMenuItem<String>(
            value: 'Option 2',
            child: Container(
              width: 120, // Set the width of the PopupMenuItem
              child: Text('Option 2'),
            ),
          ),
          PopupMenuItem<String>(
            value: 'Option 3',
            child: Container(
              width: 120, // Set the width of the PopupMenuItem
              child: Text('Option 3'),
            ),
          ),
        ],
        onSelected: (String value) {
          /// TODO Your Method
        },
      ),
    );
  }

相关问题