【问题标题】:Flutter Dropdown align selected item and hint textFlutter Dropdown 对齐所选项目和提示文本
【发布时间】:2020-04-23 15:28:30
【问题描述】:

我有以下代码。我有ListTiletitle 上有文字,trailing 上有一个下拉菜单。

Card(
      elevation: 1,
      margin: EdgeInsets.only(bottom: 3),
      child: ListTile(
        title:Text("Item Type"),
        contentPadding:
            EdgeInsets.fromLTRB(10, 0, 10, 0),
        trailing: DropdownButtonHideUnderline(
            child: DropdownButton(
              isExpanded: false,
              value: selectedLeaveType,
              items: itemTypeList.map((item) {
                return new DropdownMenuItem(
                  child: new Text(item['item_name']),
                  value: item['item_id'],
                );
              }).toList(),
              onChanged: (value) {
                setState(() {
                  selectedLeaveType = value;
                });
              },
              hint: Align(
                alignment: Alignment.centerRight,
                child: Text(
                  "Select Item Type",
                  style: TextStyle(color: Colors.grey),
                ),
              ),
              style:
                  TextStyle(color: Colors.black, decorationColor: Colors.red),
            ),
          ),
        ),
      ),
    );

下拉菜单包含各种长度的项目,我想做的是Text 左对齐,dropdown 右对齐。

问题

我只想对齐提示文本和下拉列表的选定文本以右对齐,但没有运气。

有人帮我解决这个问题吗?

【问题讨论】:

    标签: flutter dart dropdown


    【解决方案1】:

    尝试扩大宽度:

    Card(
      elevation: 1,
      margin: EdgeInsets.only(bottom: 3),
      child: ListTile(
        title: Text("Item Type"),
        contentPadding: EdgeInsets.fromLTRB(10, 0, 10, 0),
        trailing: DropdownButtonHideUnderline(
          child: DropdownButton(
            isExpanded: false,
            value: true,
            items: itemTypeList.map((item) {
              return new DropdownMenuItem(
    
                child: Container(
                  width: 150,                    //expand here
                  child: new Text(
                    item['item_name'],
                    textAlign: TextAlign.end,
                  ),
                ),
                value: item['item_id'],
              );
            }).toList(),
            onChanged: (value) {
              setState(() {
                selectedLeaveType = value;
              });
            },
            hint: Container(
              width: 150,                      //and here
              child: Text(
                "Select Item Type",
                style: TextStyle(color: Colors.grey),
                textAlign: TextAlign.end,
              ),
            ),
            style:
                TextStyle(color: Colors.black, decorationColor: Colors.red),
          ),
        ),
      ),
    ),
    

    结果:

    【讨论】:

    • 它似乎适用于hint?如何使它适用于选定的项目?仅选中的项目不是下拉列表中的所有项目。
    • 谢谢,但这会将项目也向右对齐(打开下拉菜单时)。我只希望选中的项目在右边
    • 对不起,现在我明白了你想要达到的结果,你可以用selectedItemBuilder属性自定义选中项的样式,here你可以看看。
    猜你喜欢
    • 2019-02-19
    • 1970-01-01
    • 2021-05-02
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-16
    • 1970-01-01
    相关资源
    最近更新 更多