Flutter   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了dart – Flutter – ListView中的DropdownButton溢出大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用Flutter,并且当前正尝试在对话框的列表视图显示输入字段和下拉列表.但是,我得到的下拉式溢出了视图的水平宽度并导致黄灰色条纹图案(如下所示)

Overflow of DropdownButton widget in ListView

@L_874_3@是:

class DataInput extends StatefulWidget {

      @override
      State createState() => new DataInputState("");
    }


    enum DismissDialogAction {
      cancel,discard,save,}

    class DataInputState extends State<DataInput> {
      final String _data;
      static const types = const <Map<String,String>>[
        const {
          "id": "103","desc": "0001 - lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum"
        },const {
          "id": "804","desc": "0002 - lorem ipsum lorem ipsum"
        },];

      DataInputState(this._data);

      @override
      Widget build(BuildContext context) {
        final ThemeData theme = Theme.of(context);
        return new Scaffold(
          appBar: new AppBar(
            title: const Text("Details"),actions: <Widget>[
              new FlatButton(
                  onPressed: () => Navigator.pop(context,DismissDialogAction.savE),child: new Row(
                    children: <Widget>[
                      new Icon(Icons.save,color: Colors.white,),new Text(
                          "Save",style: theme.textTheme.body1.copyWith(
                            color: Colors.white,)
                      )
                    ],)
              ),],body: new ListView(
            shrinkWrap: true,children: <Widget>[
              new Text("Set Label"),new TextField(
                autocorrect: false,new Text("SELEct Type"),new Container(
                width: new FractioncolumnWidth(0.5).value,child: new DropdownButton(
                    items: types.map((m) =>
                    new DropdownMenuItem(
                        key: new Key(m["id"]),child: new Text(m["desc"]))
                    ).toList(growable: falsE),onChanged: null
                ),);
      }
    }

错误

══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
    The following message was thrown during layout:
    A horizontal RenderFlex overflowed by 433 pixels.

    The edge of the RenderFlex that is overflowing has been marked in the rendering with a yellow and
    black Striped pattern. This is usually caused by the contents being too big for the RenderFlex.
    RenderFlex to fit within the available space instead of being sized to their natural size.
    This is considered an error condition because it inDicates that there is content that cAnnot be
    seen. If the content is legitimately bigger than the available space,consider clipping it with a
    RectClip widget before putTing it in the flex,or using a scrollable container rather than a Flex,for example using ListView.
    The specific RenderFlex in question is:
    RenderFlex#cc264 relayoutBoundary=up12 OVERFLOWING
    creator: Row ← SizedBox ← DefaultTextStyle ← Stack ← Listener ← _GestureSemantics ←
    RawGestureDetector ← GestureDetector ← DropdownButton ← ConsTrainedBox ← Container ←
    RepaintBoundary-[<3>] ← ⋯
    direction: horizontal
    mainAxisAlignment: space-between
    mainAxisSize: min
    crossAxisAlignment: center
    textDirection: ltr
    verticalDirection: down

我尝试了以下方法,但它们不起作用:

>在行,列,填充和ClipRect中包含下拉列表

有人可以帮助我理解这一点,并说明如何解决它?

更新
使用FittedBox可以防止溢出,但文本大小会缩小到不可读.

解决方法

我认为你在使用DropDownButton本身遇到了一个合法的错误.这里有一个关于这个问题的Github问题: https://github.com/flutter/flutter/issues/9211

如果您需要立即修复,您实际上可以自己修补DropDownButton!为此:

>从Flutter Framework打开dropdown.dart,并将其作为fixed_dropdown.dart粘贴到您自己的项目中.
>从此文件删除DropDownMenuItem类,以免与正常的Flutter导入冲突
>将DropDownButton重命名为FixedDropDownButton,因此它不会与Flutter导入冲突
>导航到_DropdownButtonState的构建方法.找到一行内的IndexedStack.使用Expanded小部件包装IndexedStack.

我在Github问题本身上发布了这个信息,如果你想看到修复程序在行动中,也可以找到这个解决方案的截图!

@H_618_42@
@H_675_45@

大佬总结

以上是大佬教程为你收集整理的dart – Flutter – ListView中的DropdownButton溢出全部内容,希望文章能够帮你解决dart – Flutter – ListView中的DropdownButton溢出所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。