禁用DropdownButton会导致布局错误

时间:2019-02-23 11:21:18

标签: flutter flutter-layout

在尝试暂时禁用我的应用中的DropdownButton时,我发现了一个问题。当我直接将onChange参数设置为null时,Flutter框架会引发错误,因为其中一个小部件尝试在屏幕上采用无限宽度。 另一方面,将任何功能设置为onChange都不会引起问题。该文档还说:

  

如果itemsonChanged为空,则该按钮将被禁用,向下箭头将变灰,并且将显示disabledHint(如果提供)。

是我做错了一样的事还是错误?

import 'package:flutter/material.dart';

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

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

1 个答案:

答案 0 :(得分:1)

此问题已在大约一个月前解决https://github.com/flutter/flutter/issues/26875

您可能正在使用Flutter stable 1.0.0,该版本尚未包含此修复程序。

在下一个稳定版本发布之前,您可以通过切换到dev频道来获得修复程序

flutter channel dev
flutter doctor