Brahim CHEBBI
Brahim CHEBBI

Reputation: 55

Incorrect use of ParentWidget. Expanded widgets must be placed inside flex widget

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,)
                       ],
                     ),
                   ),

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

} and there is the error I/flutter (20272): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter (20272): The following assertion was thrown building _BodyBuilder: I/flutter (20272): Incorrect use of ParentDataWidget. I/flutter (20272): Expanded widgets must be placed inside Flex widgets. I/flutter (20272): Expanded(no depth, flex: 1, dirty) has no Flex ancestor at all. I/flutter (20272): The ownership chain for the parent of the offending Expanded was: I/flutter (20272): _BodyBuilder ← MediaQuery ← LayoutId-[<_ScaffoldSlot.body>] ← CustomMultiChildLayout ← I/flutter (20272): AnimatedBuilder ← DefaultTextStyle ← AnimatedDefaultTextStyle ← _InkFeatures-[GlobalKey#9611b ink I/flutter (20272): renderer] ← NotificationListener ← PhysicalModel ← ⋯ I/flutter (20272): I/flutter (20272): The relevant error-causing widget was: I/flutter (20272): Scaffold file:///Users/macbook/AndroidStudioProjects/monmercato/lib/Screens/Login.dart:37:12

Upvotes: 0

Views: 3314

Answers (2)

timilehinjegede
timilehinjegede

Reputation: 14053

You are getting the error because you are using an Expanded widget to wrap your Column.

To fix this, try:

1) Remove the Expanded widget wrapping the Column

2) If you want the Widgets to take the screen height, remove the Expanded widget and wrap your Column widget with a Container(give the container a width and height property)

Check the code below, it works fine:

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,)
                       ],
                     ),
                   ),

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

I hope this helps.

Upvotes: 2

Viren V Varasadiya
Viren V Varasadiya

Reputation: 27177

You have to just remove First Expanded Widget.

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

Upvotes: 0

Related Questions