【问题标题】:How to set dynamic height for dropdown popup in flutter如何在颤动中为下拉弹出窗口设置动态高度
【发布时间】:2019-12-12 17:37:21
【问题描述】:

我是 Flutter 开发的新手。我正在使用我的应用程序的下拉按钮。打开下拉菜单时,弹出对话框中的文本被剪切。下面我附上了带有编码的屏幕截图。请指导我解决此问题。

                 DropdownButtonHideUnderline(
                  child: new DropdownButton(
                    isExpanded: true,
                    value: dropDownValue,
                    isDense: true,
                    //icon: Icon(Icons.keyboard_arrow_down, color: Colors.white,),
                    onChanged: (String newValue) {
                      setState(() {
                        dropDownValue = newValue;

                        state.didChange(newValue);
                      });
                    },
                    items: dropDownList.map((String value) {
                      return new DropdownMenuItem(

                        value: value,
                          child: new SizedBox(
                          width: MediaQuery.of(context).size.width / 1.4,
                          child: new Text(value,
                          softWrap: true,
                          style: TextStyle(color: Colors.white, fontSize: 18.0),),)
                      );
                    }).toList(),
                  ),
                ),);

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    按照其他人的建议复制DropdownMenuItem 类是不够的,因为DropdownButton 要求items 的类型为List<DropdownMenuItem<T>>

    我创建了以下小部件,应该可以帮助您解决问题:

    import 'package:flutter/material.dart';
    
    /// Looks like a DropdownButton but has a few differences:
    ///
    /// 1. Can be opened by a single tap even if the keyboard is showing (this might be a bug of the DropdownButton)
    ///
    /// 2. The width of the overlay can be different than the width of the child
    ///
    /// 3. The current selection is highlighted in the overlay
    class CustomDropdown<T> extends PopupMenuButton<T> {
      CustomDropdown({
        Key key,
        @required PopupMenuItemBuilder<T> itemBuilder,
        @required T selectedValue,
        PopupMenuItemSelected<T> onSelected,
        PopupMenuCanceled onCanceled,
        String tooltip,
        double elevation = 8.0,
        EdgeInsetsGeometry padding = const EdgeInsets.all(8.0),
        Icon icon,
        Offset offset = Offset.zero,
        Widget child,
        String placeholder = "Please select",
      }) : super(
        key: key,
        itemBuilder: itemBuilder,
        initialValue: selectedValue,
        onSelected: onSelected,
        onCanceled: onCanceled,
        tooltip: tooltip,
        elevation: elevation,
        padding: padding,
        icon: icon,
        offset: offset,
        child: child == null ? null : Stack(
          children: <Widget>[
            Builder(
              builder: (BuildContext context) => Container(
                height: 48,
                alignment: AlignmentDirectional.centerStart,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    DefaultTextStyle(
                      style: selectedValue!= null ? Theme.of(context).textTheme.subhead
                          : Theme.of(context).textTheme.subhead.copyWith(color:     
    Theme.of(context).hintColor),
                      child: Expanded(child: selectedValue== null ? Text(placeholder) : child),
                    ),
                    IconTheme(
                      data: IconThemeData(
                        color: Theme.of(context).brightness == Brightness.light
                            ? Colors.grey.shade700 : Colors.white70,
                      ),
                      child: const Icon(Icons.arrow_drop_down),
                    ),
                  ],
                ),
              ),
            ),
            Positioned(
              left: 0.0,
              right: 0.0,
              bottom: 8,
              child: Container(
                height: 1,
                decoration: const BoxDecoration(
                  border: Border(bottom: BorderSide(color: Color(0xFFBDBDBD), width: 0.0)),
                ),
              ),
            ),
          ],
        ),
      );
    }
    

    如您所见,它实际上扩展了PopupMenuButton,但我让它看起来与DropdownButton 相同。

    itemBuilder 需要返回List&lt;PopupMenuEntry&lt;T&gt;&gt;,每个条目通常是一个PopupMenuItem,您可以向其提供任何child 小部件。

    selectedValue 是当前选定的值,将在叠加层中突出显示。如果为空,则显示带有placeholder 字符串的Text 小部件。如果不为 null,则显示 child 小部件。

    您应该能够通过修改这个类来禁用突出显示,以使用 null 的 initialValue 调用 super(),或者更好地向构造函数添加一个布尔值以从外部控制它。

    【讨论】:

    • 是否也可以发布关于如何使用“itemBuilder 需要返回 List> 的部分代码,每个条目通常是一个 PopupMenuItem,您可以向其提供任何子小部件."。我已经尝试过,但收到错误错误:参数类型“List>”无法分配给参数类型“List> Function(BuildContext)”。
    • itemBuilder 是一个带有BuildContext 参数的函数,该参数需要返回List&lt;PopupMenuEntry&gt;,因此可以这样使用:itemBuilder: (BuildContext context) =&gt; myList 其中myList 的类型为List&lt;PopupMenuEntry&gt;跨度>
    • 谢谢,这符合预期。另一篇有趣的文章可以在这里找到gist.github.com/slightfoot/52a5be814cb4fab3f863517238ca368d
    【解决方案2】:

    DropdownMenuItem 的高度被硬编码为_kMenuItemHeight

    https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/material/dropdown.dart#L486

    您唯一能做的就是复制整个文件并根据您的需要进行调整。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-10
      • 2023-03-28
      • 2019-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多