【问题标题】:Remove Empty Space between Text and Trailer Icon of DropdownButton删除 DropdownButton 的 Text 和 Trailer Icon 之间的空白
【发布时间】:2020-07-11 16:26:10
【问题描述】:

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

文本和尾随图标之间的空白区域的照片:

列表:

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,
            ),
          )),

【问题讨论】:

  • 您希望 DropdownButton 根据所选值调整其大小?
  • @FPerroch 是的。我希望它根据所选值调整其大小。

标签: flutter dart dropdown flutter-layout


【解决方案1】:

使用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 [],
        ),
      ),
    ),
  ),
),
])

【讨论】:

    【解决方案2】:

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

    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,
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      }
    }
    

    【讨论】:

    • 你有点晚了,但谢谢。我赞成你的答案,因为它也有效。
    【解决方案3】:

    作为一个选项,您可以基于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;
            });
          },
        );
      }
    }
    

    【讨论】:

      【解决方案4】:

      你不能。如果你看代码

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

      如果需要,克隆小部件并将其更改为您的要求(不推荐,因为当下划线代码更改时您必须维护它)。

      【讨论】:

      • 我如何才能将其更改为我的要求?你能提供代码吗?抱歉,我以前从未编辑过颤振的代码。当下划线代码更改时,我可以维护它。
      • github.com/flutter/flutter/blob/master/packages/flutter/lib/src/… 那是源文件,但是注意它导入了很多素材源文件。沿着这条路走下去会做很多工作(并且会“破坏”材料设计)。
      • 你是对的。它会破坏很多东西,而且代码调试起来很长。不过感谢您的回答。
      猜你喜欢
      • 2012-09-25
      • 1970-01-01
      • 2020-07-05
      • 1970-01-01
      • 1970-01-01
      • 2017-11-15
      • 1970-01-01
      • 1970-01-01
      • 2015-04-09
      相关资源
      最近更新 更多