使用DropdownButton小部件

时间:2018-12-28 08:43:56

标签: android dart flutter

当我创建一个下拉按钮小部件时,出现此错误,提示RenderFlex被右侧的Infinity像素溢出。 源代码是:

    import 'package:flutter/material.dart';

class test extends StatefulWidget {
  @override
  _testState createState() => _testState();
}

class _testState extends State<test> {

//gender
  List<DropdownMenuItem<int>> genderList = [];
  void genderLoad(){
    genderList.add(new DropdownMenuItem(child: new Text("Male"),value: 1,));
    genderList.add(new DropdownMenuItem(child: new Text("Female"),value: 2,));
  }
//end of gender


  @override
  Widget build(BuildContext context) {
    genderLoad();
    return Scaffold(
      body: new Container(
        child: new Center(

          child: new DropdownButton(items: genderList, onChanged: null),
        ),
      ),
    );
  }
}

构建程序后,我在应用程序上没有看到任何DropDownButton,而是在控制台上收到错误消息。 下面是错误消息:

执行热重启... 正在将文件同步到针对x86构建的设备Android SDK ... 在5558毫秒内重新启动应用程序。

I/flutter (15333): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter (15333): The following assertion was thrown during performLayout():
I/flutter (15333): RenderIndexedStack object was given an infinite size during layout.
I/flutter (15333): This probably means that it is a render object that tries to be as big as possible, but it was put
I/flutter (15333): inside another render object that allows its children to pick their own size.
I/flutter (15333): The nearest ancestor providing an unbounded width constraint is:
I/flutter (15333):   RenderFlex#9b57d relayoutBoundary=up7 NEEDS-LAYOUT NEEDS-PAINT
I/flutter (15333):   creator: Row ← Padding ← Container ← DefaultTextStyle ← Stack ← Listener ← _GestureSemantics ←
I/flutter (15333):   RawGestureDetector ← GestureDetector ← Semantics ← DropdownButton<int> ← Center ← ⋯
I/flutter (15333):   parentData: offset=Offset(0.0, 0.0) (can use size)
I/flutter (15333):   constraints: BoxConstraints(0.0<=w<=411.4, 0.0<=h<=683.4)
I/flutter (15333):   size: MISSING
I/flutter (15333):   direction: horizontal
I/flutter (15333):   mainAxisAlignment: spaceBetween
I/flutter (15333):   mainAxisSize: min
I/flutter (15333):   crossAxisAlignment: center
I/flutter (15333):   textDirection: ltr
I/flutter (15333):   verticalDirection: down
I/flutter (15333): The constraints that applied to the RenderIndexedStack were:
I/flutter (15333):   BoxConstraints(0.0<=w<=Infinity, 0.0<=h<=683.4)
I/flutter (15333): The exact size it was given was:
I/flutter (15333):   Size(Infinity, 683.4)
I/flutter (15333): See https://flutter.io/layout/ for more information.
I/flutter (15333): 
I/flutter (15333): When the exception was thrown, this was the stack:
I/flutter (15333): #0      RenderBox.debugAssertDoesMeetConstraints.<anonymous closure> (package:flutter/src/rendering/box.dart:1749:9)
I/flutter (15333): #1      RenderBox.debugAssertDoesMeetConstraints (package:flutter/src/rendering/box.dart:1823:6)
I/flutter (15333): #2      RenderBox.size=.<anonymous closure> (package:flutter/src/rendering/box.dart:1543:17)
I/flutter (15333): #3      RenderBox.size= (package:flutter/src/rendering/box.dart:1543:65)
I/flutter (15333): #4      RenderStack.performLayout (package:flutter/src/rendering/stack.dart:480:7)
I/flutter (15333): #5      RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #6      RenderFlex.performLayout (package:flutter/src/rendering/flex.dart:738:15)
I/flutter (15333): #7      RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #8      RenderPadding.performLayout (package:flutter/src/rendering/shifted_box.dart:199:11)
I/flutter (15333): #9      RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #10     RenderStack.performLayout (package:flutter/src/rendering/stack.dart:510:15)
I/flutter (15333): #11     RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #12     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter (15333): #13     RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #14     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter (15333): #15     RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #16     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter (15333): #17     RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #18     RenderPositionedBox.performLayout (package:flutter/src/rendering/shifted_box.dart:385:13)
I/flutter (15333): #19     RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #20     MultiChildLayoutDelegate.layoutChild (package:flutter/src/rendering/custom_layout.dart:142:11)
I/flutter (15333): #21     _ScaffoldLayout.performLayout (package:flutter/src/material/scaffold.dart:339:7)
I/flutter (15333): #22     MultiChildLayoutDelegate._callPerformLayout (package:flutter/src/rendering/custom_layout.dart:212:7)
I/flutter (15333): #23     RenderCustomMultiChildLayoutBox.performLayout (package:flutter/src/rendering/custom_layout.dart:356:14)
I/flutter (15333): #24     RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #25     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter (15333): #26     RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #27     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter (15333): #28     _RenderCustomClip.performLayout (package:flutter/src/rendering/proxy_box.dart:1188:11)
I/flutter (15333): #29     RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #30     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter (15333): #31     RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #32     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter (15333): #33     RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #34     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter (15333): #35     RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #36     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter (15333): #37     RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #38     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter (15333): #39     RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #40     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter (15333): #41     RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #42     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter (15333): #43     RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #44     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter (15333): #45     RenderOffstage.performLayout (package:flutter/src/rendering/proxy_box.dart:2881:13)
I/flutter (15333): #46     RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #47     RenderStack.performLayout (package:flutter/src/rendering/stack.dart:510:15)
I/flutter (15333): #48     RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #49     __RenderTheatre&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter (15333): #50     RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #51     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter (15333): #52     RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #53     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter (15333): #54     RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #55     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter (15333): #56     RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #57     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter (15333): #58     RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #59     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter (15333): #60     RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter (15333): #61     RenderView.performLayout (package:flutter/src/rendering/view.dart:147:13)
I/flutter (15333): #62     RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:1509:7)
I/flutter (15333): #63     PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:768:18)
I/flutter (15333): #64     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:281:19)
I/flutter (15333): #65     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:677:13)
I/flutter (15333): #66     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:219:5)
I/flutter (15333): #67     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:990:15)
I/flutter (15333): #68     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:930:9)
I/flutter (15333): #69     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:751:7)
I/flutter (15333): #71     _Timer._runTimers (dart:isolate/runtime/libtimer_impl.dart:382:19)
I/flutter (15333): #72     _Timer._handleMessage (dart:isolate/runtime/libtimer_impl.dart:416:5)
I/flutter (15333): #73     _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
I/flutter (15333): (elided one frame from package dart:async)
I/flutter (15333): 
I/flutter (15333): The following RenderObject was being processed when the exception was fired:
I/flutter (15333):   RenderIndexedStack#d77d9 relayoutBoundary=up8 NEEDS-LAYOUT NEEDS-PAINT
I/flutter (15333):   creator: IndexedStack ← Row ← Padding ← Container ← DefaultTextStyle ← Stack ← Listener ←
I/flutter (15333):   _GestureSemantics ← RawGestureDetector ← GestureDetector ← Semantics ← DropdownButton<int> ← ⋯
I/flutter (15333):   parentData: offset=Offset(0.0, 0.0); flex=null; fit=null (can use size)
I/flutter (15333):   constraints: BoxConstraints(0.0<=w<=Infinity, 0.0<=h<=683.4)
I/flutter (15333):   size: Size(Infinity, 683.4)
I/flutter (15333):   alignment: AlignmentDirectional.centerStart
I/flutter (15333):   textDirection: ltr
I/flutter (15333):   fit: loose
I/flutter (15333):   overflow: clip
I/flutter (15333):   index: null
I/flutter (15333): This RenderObject has no descendants.
I/flutter (15333): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter (15333): Another exception was thrown: A RenderFlex overflowed by Infinity pixels on the right.

Image on the app showing error

2 个答案:

答案 0 :(得分:9)

设置isExpanded: true。并为其容器指定适当的height(例如48),或设置isDense: truedo not pass a null onChanged event or a null list of items,但是如果您出于任何原因要禁用小部件(用户不必单击它)我想设置以前的条件可以做您想做的所有事情。

class _testState extends State<test> {
//gender
  List<DropdownMenuItem<int>> genderList = [];
  void genderLoad() {
    genderList.clear();
    genderList.add(new DropdownMenuItem(
      child: new Text("Male"),
      value: 1,
    ));
    genderList.add(new DropdownMenuItem(
      child: new Text("Female"),
      value: 2,
    ));
  }
//end of gender

  @override
  Widget build(BuildContext context) {
    genderLoad();
    return Scaffold(
        appBar: AppBar(
          // Here we take the value from the MyHomePage object that was created by
          // the App.build method, and use it to set our appbar title.
          title: Text('hello'),
        ),
        body: Container(
          height: 48,
            color: Colors.lightGreenAccent,
            child: DropdownButton(
                isDense: false,
                isExpanded: true,
                items: genderList,
                onChanged: (selected){
                  print(selected);
                })));
  }
}

enter image description here

有一个打开的issue,当您在必填字段上传递空值时,DropDownButton小部件将无法呈现。

所有这些:

  • DropdownButton(items: [], onChanged: (int) {})
  • DropdownButton(items: genderList, onChanged: null)
  • DropdownButton(items: [], onChanged: null)

有问题。一种解决方法是本文中显示的解决方法。

return Scaffold(
  body: new Container(
    width: 48,
    child: new Center(
      child: new DropdownButton (
          isExpanded: true,
          items: [],
          onChanged: null
      ),
    ),
  ),
);

enter image description here

答案 1 :(得分:0)

您的问题是您为null的{​​{1}}属性传递了onChanged,这是它的必需参数。这样,DropdownButton便无法通过其内部断言并且无法正确绘制。 不幸的是Flutter在编译过程中没有发现这个问题。

尝试一下:

DropdownButton