ParentWidget 的使用不正确。展开的小部件必须放在 flex 小部件内

Posted

技术标签:

【中文标题】ParentWidget 的使用不正确。展开的小部件必须放在 flex 小部件内【英文标题】:Incorrect use of ParentWidget. Expanded widgets must be placed inside flex widget 【发布时间】:2020-08-29 08:54:00 【问题描述】:
Widget build(BuildContext context) 
return Scaffold(
  backgroundColor:Colors.white,
  body:Expanded(
    child: Column(
     children: <Widget>[
       Container(
         child: Column(
           children: <Widget>[
             Container(
               width: MediaQuery.of(context).size.width*0.9,
               height: MediaQuery.of(context).size.height*0.22,
               color: Colors.white,
               child: Column(
                 mainAxisAlignment: MainAxisAlignment.spaceBetween,
                 children: <Widget>[
                   SizedBox(height: 36.0,),

                   Row(
                     mainAxisAlignment: MainAxisAlignment.spaceBetween,
                     children: <Widget>[
                       Text("MON COMPTE",style: TextStyle(fontSize:30,fontWeight:FontWeight.bold),),
                       Container(
                         height: 50.0,
                         width: 50.0,
                         child: FloatingActionButton(
                           backgroundColor: Color(0xffd57031),
                           onPressed: ()

                           ,
                           child: Icon(Icons.help_outline,size: 30.0,),),
                       ),
                     ],
                   ),
                   Row(
                     mainAxisAlignment: MainAxisAlignment.spaceBetween,
                     children: <Widget>[
                       Container(
                           decoration: BoxDecoration(

                               border:Border(bottom: BorderSide(color: Color(0xffd57031),width: check?3.0:1.0))
                           ),
                           width: MediaQuery.of(context).size.width/2.2,
                           child: FlatButton(onPressed: ()
                             setState(() 
                               check = true;

                             );
                             ctrl.animateToPage(0, duration: Duration(milliseconds: 200), curve: Curves.fastOutSlowIn);
                           , child: Text("Se connecter",style:TextStyle(color: Color(0xffd57031),fontSize: 20,fontWeight: FontWeight.bold)))),
                       Container(
                           decoration: BoxDecoration(
                               border:Border(bottom: BorderSide(color: Color(0xffd57031),width: check?1.0:3.0))
                           ),
                           width: MediaQuery.of(context).size.width/2.31,
                           child: FlatButton(onPressed: ()
                             setState(() 
                               check = false;

                             );
                             ctrl.animateToPage(1, duration: Duration(milliseconds: 200), curve: Curves.fastOutSlowIn);
                           , child: Text("S'inscrire",style:TextStyle(color: Color(0xffd57031),fontSize: 20,fontWeight: FontWeight.bold)))),
                     ],
                   )
                 ],
               ),

             ),
             Container(
               height:MediaQuery.of(context).size.height*0.78,
               child: PageView(
                 controller: ctrl,
                 children: <Widget>[
                   Container(
                     child: Column(
                       mainAxisAlignment: MainAxisAlignment.start,
                       children: <Widget>[
                         SizedBox(height:20.0),
                         txtField("E-mail"),
                         SizedBox(height:20.0),
                         txtField("Mot de passe"),
                         SizedBox(height:40.0),
                         Container(
                             decoration: BoxDecoration(
                               boxShadow: [
                                 BoxShadow(
                                     color: Colors.black12,
                                     blurRadius: 20.0,
                                     spreadRadius: 5.0
                                 )
                               ],
                             ),
                             width: MediaQuery.of(context).size.width*0.7,
                             height: 50.0,
                             child: FlatButton(onPressed: (), child: Text("Se connecter",style: TextStyle(color:Colors.white,)),color: Color(0xffd57031),)),
                         SizedBox(height:40.0),
                         Text('Mot de passe oublié ?',style: TextStyle(color:Color(0xffd57031),),),

                         Divider(color: Color(0xffd57031),thickness: 1.5,indent: 112.0,endIndent: 125,height:1,)
                       ],
                     ),
                   ),

                 ],
               ),
             )
           ],
         ),
       ),
     ],
    ),
  ),
);

并且有错误 I/flutter (20272):══╡ 小部件库发现异常╞═════════════════════════════════════════ ═════════════════════════ I/flutter (20272):在构建 _BodyBuilder 时抛出了以下断言: I/flutter (20272):ParentDataWidget 使用不正确。 I/flutter (20272):扩展的小部件必须放在 Flex 小部件内。 I/flutter (20272): Expanded(no depth, flex: 1, dirty) 根本没有 Flex 祖先。 I/flutter (20272):违规扩展的父级的所有权链是: I/flutter (20272):_BodyBuilder ← MediaQuery ← LayoutId-[<_scaffoldslot.body>] ← CustomMultiChildLayout ← I/flutter (20272): AnimatedBuilder ← DefaultTextStyle ← AnimatedDefaultTextStyle ← _InkFeatures-[GlobalKey#9611b ink I/flutter (20272): 渲染器] ← NotificationListener ← PhysicalModel ← ⋯ 我/颤振(20272): I/flutter (20272):相关的导致错误的小部件是: I/flutter (20272): 脚手架文件:///Users/macbook/androidStudioProjects/monmercato/lib/Screens/Login.dart:37:12

【问题讨论】:

【参考方案1】:

您收到错误是因为您使用Expanded 小部件来包装您的Column

要解决此问题,请尝试:

1) 移除包裹ColumnExpanded 小部件

2) 如果您希望小部件采用屏幕高度,请移除 Expanded 小部件并用 Container 包裹您的 Column 小部件(给容器一个宽度和高度属性)

检查下面的代码,它工作正常:

Widget build(BuildContext context) 
return Scaffold(
  backgroundColor:Colors.white,
  Container(
   // give the container your preferred height here
   height: MediaQuery.of(context).size.height,
   // give the container your preferred width here
   width: MediaQuery.of(context).size.width,
    child: Column(
     children: <Widget>[
       Container(
         child: Column(
           children: <Widget>[
             Container(
               width: MediaQuery.of(context).size.width*0.9,
               height: MediaQuery.of(context).size.height*0.22,
               color: Colors.white,
               child: Column(
                 mainAxisAlignment: MainAxisAlignment.spaceBetween,
                 children: <Widget>[
                   SizedBox(height: 36.0,),

                   Row(
                     mainAxisAlignment: MainAxisAlignment.spaceBetween,
                     children: <Widget>[
                       Text("MON COMPTE",style: TextStyle(fontSize:30,fontWeight:FontWeight.bold),),
                       Container(
                         height: 50.0,
                         width: 50.0,
                         child: FloatingActionButton(
                           backgroundColor: Color(0xffd57031),
                           onPressed: ()

                           ,
                           child: Icon(Icons.help_outline,size: 30.0,),),
                       ),
                     ],
                   ),
                   Row(
                     mainAxisAlignment: MainAxisAlignment.spaceBetween,
                     children: <Widget>[
                       Container(
                           decoration: BoxDecoration(

                               border:Border(bottom: BorderSide(color: Color(0xffd57031),width: check?3.0:1.0))
                           ),
                           width: MediaQuery.of(context).size.width/2.2,
                           child: FlatButton(onPressed: ()
                             setState(() 
                               check = true;

                             );
                             ctrl.animateToPage(0, duration: Duration(milliseconds: 200), curve: Curves.fastOutSlowIn);
                           , child: Text("Se connecter",style:TextStyle(color: Color(0xffd57031),fontSize: 20,fontWeight: FontWeight.bold)))),
                       Container(
                           decoration: BoxDecoration(
                               border:Border(bottom: BorderSide(color: Color(0xffd57031),width: check?1.0:3.0))
                           ),
                           width: MediaQuery.of(context).size.width/2.31,
                           child: FlatButton(onPressed: ()
                             setState(() 
                               check = false;

                             );
                             ctrl.animateToPage(1, duration: Duration(milliseconds: 200), curve: Curves.fastOutSlowIn);
                           , child: Text("S'inscrire",style:TextStyle(color: Color(0xffd57031),fontSize: 20,fontWeight: FontWeight.bold)))),
                     ],
                   )
                 ],
               ),

             ),
             Container(
               height:MediaQuery.of(context).size.height*0.78,
               child: PageView(
                 controller: ctrl,
                 children: <Widget>[
                   Container(
                     child: Column(
                       mainAxisAlignment: MainAxisAlignment.start,
                       children: <Widget>[
                         SizedBox(height:20.0),
                         txtField("E-mail"),
                         SizedBox(height:20.0),
                         txtField("Mot de passe"),
                         SizedBox(height:40.0),
                         Container(
                             decoration: BoxDecoration(
                               boxShadow: [
                                 BoxShadow(
                                     color: Colors.black12,
                                     blurRadius: 20.0,
                                     spreadRadius: 5.0
                                 )
                               ],
                             ),
                             width: MediaQuery.of(context).size.width*0.7,
                             height: 50.0,
                             child: FlatButton(onPressed: (), child: Text("Se connecter",style: TextStyle(color:Colors.white,)),color: Color(0xffd57031),)),
                         SizedBox(height:40.0),
                         Text('Mot de passe oublié ?',style: TextStyle(color:Color(0xffd57031),),),

                         Divider(color: Color(0xffd57031),thickness: 1.5,indent: 112.0,endIndent: 125,height:1,)
                       ],
                     ),
                   ),

                 ],
               ),
             )
           ],
         ),
       ),
     ],
    ),
  ),
);

我希望这会有所帮助。

【讨论】:

【参考方案2】:

您只需删除 First Expanded Widget。

Scaffold(
      backgroundColor: Colors.white,
      body: Column(// remove Expandede
        children: <Widget>[

【讨论】:

以上是关于ParentWidget 的使用不正确。展开的小部件必须放在 flex 小部件内的主要内容,如果未能解决你的问题,请参考以下文章

Qt4:从 QDockedWidget 的子类访问 QtDesigner 创建的小部件

添加容器颜色时出错:ParentDataWidget 的使用不正确

flutter - 网格视图不显示在展开的小部件中。

Kivy展开和折叠一个面板,以隐藏内部结构。

Swift 3 不正确的字符串插值与隐式展开的 Optionals

七种方案!探讨Redis分布式锁的正确使用姿势