Flutter scroll issue - A RenderFlex overflowed by 192 pixels on the bottom

1,149

Solution 1

You have to wrap your Form widget in SingleChildScrollView. Also, note that after adding SingleChildScrollView there is no fix height so, you can't use spacer widget. Instead of that you can use SizedBox Widget to give some space above.

final _formKey = GlobalKey<FormState>();
  String sName, fName, lName, email, phone, password;
  AuthService authService = new AuthService();
  bool _isLoading = false;

  signUp() async {
    if (_formKey.currentState.validate()) {
      setState(() {
        _isLoading = true;
      });
      authService.signUpWithEmailAndPass(email, password).then((value) {
        if (value != null) {
          setState(() {
            _isLoading = false;
          });
          Navigator.pushReplacement(
              context, MaterialPageRoute(builder: (context) => Home()));
        }
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: appBar(context),
        centerTitle: true,
        backgroundColor: Colors.transparent,
        elevation: 0.0,
        brightness: Brightness.light,
      ),
      body: _isLoading
          ? Container(
              child: Center(
                child: CircularProgressIndicator(),
              ),
            )
          : SingleChildScrollView(
              child: Form(
                key: _formKey,
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 24),
                  child: Container(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        // here you can't use spacer because their is no fix height, so you have to use SixedBox give some spacing
                        SizedBox(
                          height: 50,
                        ),
                        TextFormField(
                          validator: (val) {
                            return val.isEmpty ? "Enter School Name" : null;
                          },
                          decoration: InputDecoration(hintText: "School Name"),
                          onChanged: (val) {
                            sName = val;
                          },
                        ),
                        SizedBox(
                          height: 6,
                        ),
                        TextFormField(
                          validator: (val) {
                            return val.isEmpty ? "Enter First Name" : null;
                          },
                          decoration: InputDecoration(hintText: "First Name"),
                          onChanged: (val) {
                            fName = val;
                          },
                        ),
                        SizedBox(
                          height: 6,
                        ),
                        TextFormField(
                          validator: (val) {
                            return val.isEmpty ? "Enter Last Name" : null;
                          },
                          decoration: InputDecoration(hintText: "Last Name"),
                          onChanged: (val) {
                            lName = val;
                          },
                        ),
                        SizedBox(
                          height: 6,
                        ),
                        TextFormField(
                          validator: (val) {
                            return val.isEmpty ? "Enter Emailid" : null;
                          },
                          decoration: InputDecoration(hintText: "Email"),
                          onChanged: (val) {
                            email = val;
                          },
                        ),
                        SizedBox(
                          height: 6,
                        ),
                        TextFormField(
                          validator: (val) {
                            return val.isEmpty ? "Enter Phone Number" : null;
                          },
                          decoration: InputDecoration(hintText: "Phone"),
                          onChanged: (val) {
                            phone = val;
                          },
                        ),
                        SizedBox(
                          height: 6,
                        ),
                        TextFormField(
                          obscureText: true,
                          validator: (val) {
                            return val.isEmpty ? "Enter Password" : null;
                          },
                          decoration: InputDecoration(hintText: "Password"),
                          onChanged: (val) {
                            password = val;
                          },
                        ),
                        SizedBox(
                          height: 24,
                        ),
                        GestureDetector(
                          onTap: () {
                            signUp();
                          },
                          child: Container(
                            padding: EdgeInsets.symmetric(vertical: 18),
                            decoration: BoxDecoration(
                                color: Colors.blue,
                                borderRadius: BorderRadius.circular(30)),
                            alignment: Alignment.center,
                            width: MediaQuery.of(context).size.width - 48,
                            child: Text(
                              "Register",
                              style:
                                  TextStyle(color: Colors.white, fontSize: 16),
                            ),
                          ),
                        ),
                        SizedBox(
                          height: 18,
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          mainAxisSize: MainAxisSize.min,
                          children: <Widget>[
                            Text(
                              "Already have an account? ",
                              style: TextStyle(fontSize: 15.5),
                            ),
                            GestureDetector(
                                onTap: () {
                                  Navigator.pushReplacement(
                                      context,
                                      MaterialPageRoute(
                                          builder: (context) => SignIn()));
                                },
                                child: Text(
                                  "Sign in",
                                  style: TextStyle(
                                      fontSize: 15.5,
                                      decoration: TextDecoration.underline),
                                ))
                          ],
                        ),
                        SizedBox(
                          height: 80,
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
    );
  }
}

Solution 2

Wrap your body in 'singlechildscrollview'.

Share:
1,149
calculus
Author by

calculus

I am interested in Internet of Things, latest technology, lead-agile development and movies

Updated on December 20, 2022

Comments

  • calculus
    calculus over 1 year

    I'm building a form with 4-5 form fields. However, when I try to enter the data, it throws the below error

    Performing hot reload...
    Syncing files to device sdk gphone x86 64...
    Reloaded 3 of 512 libraries in 363ms.
    W/IInputConnectionWrapper(21437): getTextBeforeCursor on inactive InputConnection
    W/IInputConnectionWrapper(21437): getSelectedText on inactive InputConnection
    W/IInputConnectionWrapper(21437): getTextAfterCursor on inactive InputConnection
    W/IInputConnectionWrapper(21437): beginBatchEdit on inactive InputConnection
    W/IInputConnectionWrapper(21437): endBatchEdit on inactive InputConnection
    
    ════════ Exception caught by rendering library ═════════════════════════════════════════════════════
    The following assertion was thrown during layout:
    A RenderFlex overflowed by 192 pixels on the bottom.
    
    The relevant error-causing widget was: 
      Column file:///C:/Users/xyz/Documents/quizmaker/lib/views/signup.dart:54:20
    The overflowing RenderFlex has an orientation of Axis.vertical.
    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.
    
    Consider applying a flex factor (e.g. using an Expanded widget) to force the children of the 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 ClipRect widget before putting it in the flex, or using a scrollable container rather than a Flex, like a ListView.
    
    The specific RenderFlex in question is: RenderFlex#322a5 relayoutBoundary=up2 OVERFLOWING
    ...  needs compositing
    ...  parentData: offset=Offset(24.0, 0.0) (can use size)
    ...  constraints: BoxConstraints(0.0<=w<=363.4, 0.0<=h<=321.1)
    ...  size: Size(363.4, 321.1)
    ...  direction: vertical
    ...  mainAxisAlignment: start
    ...  mainAxisSize: min
    ...  crossAxisAlignment: start
    ...  textDirection: ltr
    ...  verticalDirection: down
    ◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤
    ════════════════════════════════════════════════════════════════════════════════════════════════════
    

    Here is my code

    import 'package:flutter/material.dart';
    import 'package:quizmaker/services/auth.dart';
    import 'package:quizmaker/views/home.dart';
    import 'package:quizmaker/views/signin.dart';
    import 'package:quizmaker/widgets/widgets.dart';
    
    class SignUp extends StatefulWidget {
      @override
      _SignUpState createState() => _SignUpState();
    }
    
    class _SignUpState extends State<SignUp> {
    
      final _formKey = GlobalKey<FormState>();
      String sName, fName, lName, email, phone, password;
      AuthService authService = new AuthService();
      bool _isLoading = false;
    
      signUp() async {
        if(_formKey.currentState.validate()){
          setState(() {
            _isLoading = true;
          });
          authService.signUpWithEmailAndPass(email, password).then((value){
            if(value != null){
              setState(() {
                _isLoading = false;
              });
              Navigator.pushReplacement(context, MaterialPageRoute(
                builder: (context) => Home()
              ));
            }
            });
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: appBar(context),
            centerTitle: true,
            backgroundColor: Colors.transparent,
            elevation: 0.0,
            brightness: Brightness.light,
          ),
          body: _isLoading ? Container(
            child: Center(child: CircularProgressIndicator(),),
          ) : Form(
            key: _formKey,
            child: Container(
              padding: EdgeInsets.symmetric(horizontal: 24),
              child: Container(
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                  Spacer(),
                  TextFormField(
                    validator: (val){
                      return val.isEmpty ? "Enter School Name" : null ;
                    } ,
                    decoration: InputDecoration(
                        hintText: "School Name"
                    ),
                    onChanged:  (val){
                      sName = val;
                    },
                  ),
                  SizedBox(height: 6,),
                  TextFormField(
                    validator: (val){
                      return val.isEmpty ? "Enter First Name" : null ;
                    } ,
                    decoration: InputDecoration(
                        hintText: "First Name"
                    ),
                    onChanged:  (val){
                      fName = val;
                    },
                  ),
                  SizedBox(height: 6,),
                  TextFormField(
                    validator: (val){
                      return val.isEmpty ? "Enter Last Name" : null ;
                    } ,
                    decoration: InputDecoration(
                        hintText: "Last Name"
                    ),
                    onChanged:  (val){
                      lName = val;
                    },
                  ),
                  SizedBox(height: 6,),
                  TextFormField(
                    validator: (val){
                      return val.isEmpty ? "Enter Emailid" : null ;
                    } ,
                    decoration: InputDecoration(
                        hintText: "Email"
                    ),
                    onChanged:  (val){
                      email = val;
                    },
                  ),
                  SizedBox(height: 6,),
                  TextFormField(
                    validator: (val){
                      return val.isEmpty ? "Enter Phone Number" : null ;
                    } ,
                    decoration: InputDecoration(
                        hintText: "Phone"
                    ),
                    onChanged:  (val){
                      phone = val;
                    },
                  ),
                  SizedBox(height: 6,),
                  TextFormField(
                    obscureText: true,
                    validator: (val){
                      return val.isEmpty ? "Enter Password" : null ;
                    } ,
                    decoration: InputDecoration(
                        hintText: "Password"
                    ),
                    onChanged:  (val){
                      password = val;
                    },
                  ),
                  SizedBox(height: 24,),
                  GestureDetector(
                    onTap: (){
                      signUp();
                    },
                    child: Container(
                      padding: EdgeInsets.symmetric(vertical: 18),
                      decoration: BoxDecoration(
                          color: Colors.blue,
                          borderRadius: BorderRadius.circular(30)
                      ),
                      alignment: Alignment.center,
                      width: MediaQuery.of(context).size.width - 48,
                      child: Text("Register", style: TextStyle(color: Colors.white, fontSize: 16),),
                    ),
                  ),
                  SizedBox(height: 18,),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Text("Already have an account? ", style: TextStyle(fontSize: 15.5),),
                      GestureDetector(
                        onTap: (){
                          Navigator.pushReplacement(context, MaterialPageRoute(
                          builder: (context) => SignIn()
                          ));
                        },
    
                          child: Text("Sign in", style: TextStyle(fontSize: 15.5, decoration: TextDecoration.underline),))
    
                    ],
                  ),
    
                  SizedBox(height: 80,),
    
                ],
                ),
              ),
            ),
          ),
        );;
      }
    }
    

    I tried adding Expanded to the children, it removes the error, but the form fields become very small while entering the data. Tried wrapping column in SingleChildScrollView, but it gives me the below

    RenderFlex children have non-zero flex but incoming height constraints are unbounded

    Can anyone suggest what it is that I'm doing wrong?

    Thanks!

  • calculus
    calculus almost 4 years
    You mean, the form container in body? I tried, it still gives the same error
  • Viren V Varasadiya
    Viren V Varasadiya almost 4 years
    @calculus Accept my answer of you found it useful.
  • calculus
    calculus almost 4 years
    I totally missed spacebar. You got it! accepting your answer! Thanks!