Home > Software design >  Flutter SingleChildScrollView inside column with spaceBetween
Flutter SingleChildScrollView inside column with spaceBetween

Time:08-12

I have a dialog that must consist of two parts: Top part is SingleChildScrollView that will list messages (so its height can't be very long). Bottom part is a text field for adding messages with variable number of lines. So, what I want to do is like a simple chat.

This is my code:

    AlertDialog(
      insetPadding: EdgeInsets.zero,
      contentPadding: EdgeInsets.zero,
      actions: [
        FlatButton(
          child: Text("Cancel"),
          onPressed: () {
            Navigator.of(context).pop(null);
          },
        ),
      ],
      content: Container(
        width: MediaQuery.of(context).size.width - 50,
        height: MediaQuery.of(context).size.height - 300,
        child: Scaffold(
          appBar: AppBar(
            automaticallyImplyLeading: false,//remove back button
            title: Text("Info", style: TextStyle(fontSize: 16, color: Colors.white),),
          ),
          body: Column(  // LINE X 
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              SingleChildScrollView(
                child: Column(
                  children: [
                    //here I will have my messages
                     Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras at eros quis mi scelerisque volutpat eu id purus."), 
                      Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras at eros quis mi scelerisque volutpat eu id purus."), 
                      Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras at eros quis mi scelerisque volutpat eu id purus."),   
                      Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras at eros quis mi scelerisque volutpat eu id purus."),
                      Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras at eros quis mi scelerisque volutpat eu id purus."), 
                      Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras at eros quis mi scelerisque volutpat eu id purus."), 
                      Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras at eros quis mi scelerisque volutpat eu id purus."), 
                      Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras at eros quis mi scelerisque volutpat eu id purus."),
                      Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras at eros quis mi scelerisque volutpat eu id purus."),
                      Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras at eros quis mi scelerisque volutpat eu id purus."),
                  ],
                ),
              ),
              TextField() //This textField can be from 1 to N lines height
            ],
          ),
        ),
      ),
    );

And this is what I get:

The relevant error-causing widget was: 
  Column Column:file: LINE X
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.

Could anyone say how to fix it?

CodePudding user response:

Wrap your SingleChildScrollView with Expanded widget.

mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
  Expanded(
    child: SingleChildScrollView(
      child: Column(
        children: [
  • Related