flutter 删除下拉按钮的文本和尾部图标之间的空白

o4hqfura  于 2023-06-24  发布在  Flutter
关注(0)|答案(6)|浏览(122)

我有一个显示字符串列表的下拉列表。字符串的值包含仅由四个字母组成的单词和由许多字母组成的单词。当选定的项目是四个字母的项目时,这会产生布局问题。在文本和下拉按钮的尾随图标之间可以看到空白或白色。如何才能将这一空白空间移除?如何根据所选值调整下拉按钮的大小?
文本和尾随图标之间的空白照片:

列表:

List<String> textList = ["test", "this_is_a_long_text"];

下拉列表:

DropdownButtonHideUnderline(
      child: ButtonTheme(
       alignedDropdown: true,
       child: DropdownButton<String>(
        items: textList.map((String dropDownStringItem) {
           return DropdownMenuItem<String>(
                      value: dropDownStringItem,
                      child: Text(dropDownStringItem),
                    );
                  }).toList(),
              onChanged: (String newValueSelected) {
                _onDropDownItemSelected(newValueSelected);
              },
              value: _currentItemSelected,
            ),
          )),
0mkxixxg

0mkxixxg1#

作为一个选项,您可以基于PopupMenuButton而不是常规DropdownButton构建它
下面是一个例子

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(child: AwesomeDropdown()),
      ),
    );
  }
}

class AwesomeDropdown extends StatefulWidget {
  @override
  _AwesomeDropdownState createState() => _AwesomeDropdownState();
}

class _AwesomeDropdownState extends State<AwesomeDropdown> {
  final List<String> textList = ["test", "this_is_a111111_long_text"];
  String _currentItemSelected;

  @override
  void initState() {
    super.initState();
    _currentItemSelected = textList[0];
  }

  @override
  Widget build(BuildContext context) {
    return PopupMenuButton<String>(
      itemBuilder: (context) {
        return textList.map((str) {
          return PopupMenuItem(
            value: str,
            child: Text(str),
          );
        }).toList();
      },
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Text(_currentItemSelected),
          Icon(Icons.arrow_drop_down),
        ],
      ),
      onSelected: (v) {
        setState(() {
          print('!!!===== $v');
          _currentItemSelected = v;
        });
      },
    );
  }
}

wfauudbj

wfauudbj2#

另一个解决方法是使它成为一个可点击的文本,将显示下拉选项作为一个对话框。下面是一个例子:
Preview Gif

import 'package:flutter/material.dart';

class CustomDialogTest extends StatefulWidget {
  @override
  _CustomDialogTestState createState() => _CustomDialogTestState();
}

class _CustomDialogTestState extends State<CustomDialogTest> {
  String _onDropDownItemSelected = '(Choose Option ▼)';

  var textList = [
    'Cat',
    'Dog',
    'Colorfull Unicorn',
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      appBar: AppBar(
        title: Text(
          'Dropdown spacing',
        ),
      ),
      body: Padding(
        padding: EdgeInsets.only(top: 8.0),
        child: Container(
          color: Colors.white,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                'I am a ',
                style: TextStyle(
                  color: Colors.black,
                  fontSize: 18,
                  fontWeight: FontWeight.w500,
                ),
              ),
              InkWell(
                onTap: () {
                  showDialog(
                    context: context,
                    child: Dialog(
                      backgroundColor: Colors.blue[100],
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(18.0),
                      ),
                      child: ListView.builder(
                          shrinkWrap: true,
                          itemCount: textList.length,
                          itemBuilder: (context, index) {
                            return GestureDetector(
                              child: Row(
                                children: <Widget>[
                                  Icon(
                                    Icons.arrow_right,
                                    color: Colors.black,
                                  ),
                                  Text(
                                    textList[index],
                                    style: TextStyle(
                                      color: Colors.black,
                                      fontSize: 20.0,
                                    ),
                                  ),
                                ],
                              ),
                              onTap: () {
                                Navigator.pop(context);
                                setState(() {
                                  _onDropDownItemSelected = textList[index];
                                });
                              },
                            );
                          }),
                    ),
                  );
                },
                child: Text(
                  _onDropDownItemSelected,
                  style: TextStyle(
                    color: Colors.blue[900],
                    fontSize: 18,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
              Text(
                ' Person',
                style: TextStyle(
                  color: Colors.black,
                  fontSize: 18,
                  fontWeight: FontWeight.w500,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
sg3maiej

sg3maiej3#

结账DropdownButton2
有一个customButton属性。一个我如何使用它的例子。

customButton: Row(
            children: [
              const Icon(
                Icons.plus_one,
                size: 12,
              ),
              Text(
                "References",
              )
            ],
          ),

选项框将展开以适应行的大小。
编辑:在张贴这一点,我意识到,它仍然受到限制,不能有一个更小的盒子相比,里面的项目

4smxwvx5

4smxwvx54#

你不能。如果你看代码

// The width of the button and the menu are defined by the widest
// item and the width of the hint.

克隆这个小部件,如果必要的话,可以根据您的需求进行更改(不推荐,因为当下划线代码更改时,您必须维护它)。

2w3kk1z5

2w3kk1z55#

使用flexfit.loose,下拉按钮的剩余空间(来自此小部件内部的堆栈)将被削减

Row(
  children: [
    Flexible(
      fit: FlexFit.loose,
      child: Padding(
        padding: const EdgeInsets.all(8),
        child: Container(
          decoration: BoxDecoration(
            border: Border.all(
              width: 2,
              color: Theme.of(context).primaryColor,
            ),
          ),
        child: Padding(
          padding: const EdgeInsets.only(left: 8.0, right: 8),
          child: DropdownButton(
            icon: const Icon(Icons.filter_alt),
            underline: Container(),
            hint: const Text(
              'Test',
              style: TextStyle(
                color: Colors.black,
              ),
            ),
          items: const [],
        ),
      ),
    ),
  ),
),
])
cedebl8k

cedebl8k6#

使用DropdownButton中的属性alignment,您可以放置文本centerRight并制作所需的效果。
Flutter 2.12测试

DropdownButton<String>(
                  value: dropdownValue,
                  elevation: 16,
                  underline: const SizedBox(),
                  isDense: true,
                  alignment: Alignment.centerRight,
)

相关问题