skip to Main Content

I was trying to create an app with Flutter and Dart and I was wondering if there was a way to go newline if a container inside a row overflows.
Here’s an example, the thing I want to do is to make the "button" go newline if it intersects into another one to make it more readable.
I hope someone can help me, I will link the code also for a better understanding.

  Padding(
    padding: EdgeInsets.fromLTRB(0, MediaQuery.of(context).size.height/40, 0, 0),
    
    child: Column(
      children: [
        Row(   //I tre bottoni di 'Like', 'Commenta' e 'Condividi'
          mainAxisAlignment: MainAxisAlignment.center,
          
          children: [
            
            Container(    //Like button
              width: MediaQuery.of(context).size.width/4,
              height: MediaQuery.of(context).size.height/15,
              decoration: const BoxDecoration(
                color: Colors.blue,
                borderRadius: BorderRadius.all(Radius.circular(20)),
                boxShadow: [
                  BoxShadow(
                    color: Colors.blue,
                    spreadRadius: 5,
                    blurRadius: 20,
                  )
                ]
              ),

              child: Row(   //Contenuto del container, 'child:' perché è il figlio del container
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [   //'Children:[]' perché contiene più elementi
                  Padding(
                    padding: const EdgeInsets.fromLTRB(10, 0, 5, 0),
                    child: Icon(
                      Icons.thumb_up, color: Colors.white,
                      size: MediaQuery.of(context).size.height/40,
                    ),
                  ),

                  Center(
                    child: Text('Like',
                      overflow: TextOverflow.fade,
                      style: TextStyle(
                        fontSize: MediaQuery.of(context).size.height/40,
                        fontFamily: 'Poppins',
                        color: Colors.white,
                      ),
                    ),
                  ),
                ],
              ),
            ),
            
            Padding(
              padding: EdgeInsets.fromLTRB(MediaQuery.of(context).size.width/40, 0, MediaQuery.of(context).size.width/40, 0),
              child: Container(    //Comment button
            
                width: MediaQuery.of(context).size.width/4,
                height: MediaQuery.of(context).size.height/15,
                
                decoration: const BoxDecoration(
            
                  borderRadius: BorderRadius.all(Radius.circular(20)),
                  color: Colors.green,
            
                  boxShadow: [
                    BoxShadow(
            
                      color: Colors.green,
                      spreadRadius: 5,
                      blurRadius: 20,
                    )
                  ]
                ),
            
                child: Row(    //Contenuto del secondo bottone (quello centrale)
            
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    
                    Padding(  
                      padding: const EdgeInsets.fromLTRB(30, 0, 5, 0),
                      child: Icon(
                        Icons.message, color: Colors.white,
                        size: MediaQuery.of(context).size.height/40,
                      ),
                    ),
                    Text('Commenta',
                      overflow: TextOverflow.clip,
                      style: TextStyle(
                        fontSize: MediaQuery.of(context).size.height/40,
                        fontFamily: 'Poppins',
                        color: Colors.white,
                      ),
                    ),
                ],),
              ),
            ),

            Container(    //Share post button
  
              width: MediaQuery.of(context).size.width/4,
              height: MediaQuery.of(context).size.height/15,

              decoration: const BoxDecoration(

                borderRadius: BorderRadius.all(Radius.circular(20)),
                color: Colors.red,

                boxShadow: [    //Parentesi quadre perché può contenere più proprietà, è perciò una lista
                  BoxShadow(

                    color: Colors.red,
                    spreadRadius: 5,
                    blurRadius: 20,
                  )
                ]
              ),

              child: Row(   //Contenuto del container

                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,

                children: [

                  Padding(
                    padding: const EdgeInsets.fromLTRB(10, 0, 5, 0),
                    child: Icon(
                      Icons.share, color: Colors.white,
                      size: MediaQuery.of(context).size.height/40,
                    ),
                  ),
                  Text('Condividi',
                    style: TextStyle(
                      
                      fontSize: MediaQuery.of(context).size.height/40,
                      fontFamily: 'Poppins',
                      color: Colors.white,
                    ),
                  )
                ]
              )
            ),
          ],
        ),
      ],
    ),
  )
],),

2

Answers


  1. Chosen as BEST ANSWER

    Just figured out the problem, I put the Wrap widget in the wrong position and now it works as needed. Here's the code to help others who have the same problem.

    Wrap(children: [
    
                    Row(   //I tre bottoni di 'Like', 'Commenta' e 'Condividi'
                      mainAxisAlignment: MainAxisAlignment.center,
                      
                      children: [
                        
                        Container(    //Like button
                          width: MediaQuery.of(context).size.width/4,
                          height: MediaQuery.of(context).size.height/15,
                          decoration: const BoxDecoration(
                            color: Colors.blue,
                            borderRadius: BorderRadius.all(Radius.circular(20)),
                            boxShadow: [
                              BoxShadow(
                                color: Colors.blue,
                                spreadRadius: 5,
                                blurRadius: 20,
                              )
                            ]
                          ),
                  
                          child: Row(   //Contenuto del container, 'child:' perché è il figlio del container
                            crossAxisAlignment: CrossAxisAlignment.center,
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [   //'Children:[]' perché contiene più elementi
                              Padding(
                                padding: const EdgeInsets.fromLTRB(10, 0, 5, 0),
                                child: Icon(
                                  Icons.thumb_up, color: Colors.white,
                                  size: MediaQuery.of(context).size.height/40,
                                ),
                              ),
                  
                              Flexible(
                                child: Text('Like',
                                  overflow: TextOverflow.fade,
                                  style: TextStyle(
                                    fontSize: MediaQuery.of(context).size.height/40,
                                    fontFamily: 'Poppins',
                                    color: Colors.white,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                        
                        Padding(
                          padding: EdgeInsets.fromLTRB(MediaQuery.of(context).size.width/40, 0, MediaQuery.of(context).size.width/40, 0),
                          child: Container(    //Comment button
                        
                            width: MediaQuery.of(context).size.width/4,
                            height: MediaQuery.of(context).size.height/15,
                            
                            decoration: const BoxDecoration(
                        
                              borderRadius: BorderRadius.all(Radius.circular(20)),
                              color: Colors.green,
                        
                              boxShadow: [
                                BoxShadow(
                        
                                  color: Colors.green,
                                  spreadRadius: 5,
                                  blurRadius: 20,
                                )
                              ]
                            ),
                        
                            child: Row(    //Contenuto del secondo bottone (quello centrale)
                        
                              crossAxisAlignment: CrossAxisAlignment.center,
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                
                                Padding(  
                                  padding: const EdgeInsets.fromLTRB(30, 0, 5, 0),
                                  child: Icon(
                                    Icons.message, color: Colors.white,
                                    size: MediaQuery.of(context).size.height/40,
                                  ),
                                ),
                                Flexible(
                                  child: Text('Commenta',
                                    overflow: TextOverflow.ellipsis,
                                    style: TextStyle(
                                      fontSize: MediaQuery.of(context).size.height/40,
                                      fontFamily: 'Poppins',
                                      color: Colors.white,
                                    ),
                                  ),
                                ),
                            ],),
                          ),
                        ),
                  
                        Container(    //Share post button
                          
                          width: MediaQuery.of(context).size.width/4,
                          height: MediaQuery.of(context).size.height/15,
                  
                          decoration: const BoxDecoration(
                  
                            borderRadius: BorderRadius.all(Radius.circular(20)),
                            color: Colors.red,
                  
                            boxShadow: [    //Parentesi quadre perché può contenere più proprietà, è perciò una lista
                              BoxShadow(
                  
                                color: Colors.red,
                                spreadRadius: 5,
                                blurRadius: 20,
                              )
                            ]
                          ),
                  
                          child: Row(   //Contenuto del container
                  
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.center,
                  
                            children: [
                  
                              Padding(
                                padding: const EdgeInsets.fromLTRB(10, 0, 5, 0),
                                child: Icon(
                                  Icons.share, color: Colors.white,
                                  size: MediaQuery.of(context).size.height/40,
                                ),
                              ),
                              Flexible(
                                child: Text('Condividi',
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(
                                    fontSize: MediaQuery.of(context).size.height/40,
                                    fontFamily: 'Poppins',
                                    color: Colors.white,
                                  ),
                                ),
                              )
                            ]
                          )
                        ),
                      ],
                    ),
    
                  ],
        ```
    

  2. Method1: You have to use Flexible widget to avoid overflow.

    Padding(
          padding: EdgeInsets.fromLTRB(0, MediaQuery.of(context).size.height/40, 0, 0),
    
          child: Column(
            children: [
              Row(   //I tre bottoni di 'Like', 'Commenta' e 'Condividi'
                mainAxisAlignment: MainAxisAlignment.center,
    
                children: [
    
                  Container(    //Like button
                    width: MediaQuery.of(context).size.width/4,
                    height: MediaQuery.of(context).size.height/15,
                    decoration: const BoxDecoration(
                        color: Colors.blue,
                        borderRadius: BorderRadius.all(Radius.circular(20)),
                        boxShadow: [
                          BoxShadow(
                            color: Colors.blue,
                            spreadRadius: 5,
                            blurRadius: 20,
                          )
                        ]
                    ),
    
                    child: Row(   //Contenuto del container, 'child:' perché è il figlio del container
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [   //'Children:[]' perché contiene più elementi
                        Padding(
                          padding: const EdgeInsets.fromLTRB(10, 0, 5, 0),
                          child: Icon(
                            Icons.thumb_up, color: Colors.white,
                            size: MediaQuery.of(context).size.height/40,
                          ),
                        ),
    
                        Flexible(
                          child: Center(
                            child: Text('Like',
                              overflow: TextOverflow.fade,
                              style: TextStyle(
                                fontSize: MediaQuery.of(context).size.height/40,
                                fontFamily: 'Poppins',
                                color: Colors.white,
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
    
                  Padding(
                    padding: EdgeInsets.fromLTRB(MediaQuery.of(context).size.width/40, 0, MediaQuery.of(context).size.width/40, 0),
                    child: Container(    //Comment button
    
                      width: MediaQuery.of(context).size.width/4,
                      height: MediaQuery.of(context).size.height/15,
    
                      decoration: const BoxDecoration(
    
                          borderRadius: BorderRadius.all(Radius.circular(20)),
                          color: Colors.green,
    
                          boxShadow: [
                            BoxShadow(
    
                              color: Colors.green,
                              spreadRadius: 5,
                              blurRadius: 20,
                            )
                          ]
                      ),
    
                      child: Row(    //Contenuto del secondo bottone (quello centrale)
    
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
    
                          Padding(
                            padding: const EdgeInsets.fromLTRB(30, 0, 5, 0),
                            child: Icon(
                              Icons.message, color: Colors.white,
                              size: MediaQuery.of(context).size.height/40,
                            ),
                          ),
                          Flexible(
                            child: Text('Commenta',
                              overflow: TextOverflow.clip,
                              style: TextStyle(
                                fontSize: MediaQuery.of(context).size.height/40,
                                fontFamily: 'Poppins',
                                color: Colors.white,
                              ),
                            ),
                          ),
                        ],),
                    ),
                  ),
    
                  Container(    //Share post button
    
                      width: MediaQuery.of(context).size.width/4,
                      height: MediaQuery.of(context).size.height/15,
    
                      decoration: const BoxDecoration(
    
                          borderRadius: BorderRadius.all(Radius.circular(20)),
                          color: Colors.red,
    
                          boxShadow: [    //Parentesi quadre perché può contenere più proprietà, è perciò una lista
                            BoxShadow(
    
                              color: Colors.red,
                              spreadRadius: 5,
                              blurRadius: 20,
                            )
                          ]
                      ),
    
                      child: Row(   //Contenuto del container
    
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.center,
    
                          children: [
    
                            Padding(
                              padding: const EdgeInsets.fromLTRB(10, 0, 5, 0),
                              child: Icon(
                                Icons.share, color: Colors.white,
                                size: MediaQuery.of(context).size.height/40,
                              ),
                            ),
                            Flexible(
                              child: Text('Condividi',
                                style: TextStyle(
    
                                  fontSize: MediaQuery.of(context).size.height/40,
                                  fontFamily: 'Poppins',
                                  color: Colors.white,
                                ),
                              ),
                            )
                          ]
                      )
                  ),
                ],
              ),
            ],
          ),
        )
    

    Edited
    Method 2:

       Padding(
          padding: EdgeInsets.fromLTRB(0, MediaQuery.of(context).size.height / 40, 0, 0),
          child: Column(
            children: [
              Row(
                //I tre bottoni di 'Like', 'Commenta' e 'Condividi'
                mainAxisAlignment: MainAxisAlignment.center,
    
                children: [
                  Expanded(
                    child: Container(
                      //Like button
                      margin: EdgeInsets.all(10),
                      padding: EdgeInsets.all(10),
                      decoration: const BoxDecoration(
                          color: Colors.blue,
                          borderRadius: BorderRadius.all(Radius.circular(20)),
                          boxShadow: [
                            BoxShadow(
                              color: Colors.blue,
                              spreadRadius: 5,
                              blurRadius: 20,
                            )
                          ]),
    
                      child: Row(
                        //Contenuto del container, 'child:' perché è il figlio del container
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          //'Children:[]' perché contiene più elementi
                          Padding(
                            padding: const EdgeInsets.only(right: 5),
                            child: Icon(
                              Icons.thumb_up,
                              color: Colors.white,
                              size: MediaQuery.of(context).size.height / 40,
                            ),
                          ),
    
                          Flexible(
                            child: Center(
                              child: Text(
                                'Like',
                                overflow: TextOverflow.fade,
                                style: TextStyle(
                                  fontSize: MediaQuery.of(context).size.height / 40,
                                  fontFamily: 'Poppins',
                                  color: Colors.white,
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Expanded(
                    child: Container(
                      //Comment button
    
                      padding: EdgeInsets.all(10),
                      margin: EdgeInsets.all(10),
    
                      decoration: const BoxDecoration(
                          borderRadius: BorderRadius.all(Radius.circular(20)),
                          color: Colors.green,
                          boxShadow: [
                            BoxShadow(
                              color: Colors.green,
                              spreadRadius: 5,
                              blurRadius: 20,
                            )
                          ]),
    
                      child: Row(
                        //Contenuto del secondo bottone (quello centrale)
    
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Padding(
                            padding: const EdgeInsets.only(right: 5),
                            child: Icon(
                              Icons.message,
                              color: Colors.white,
                              size: MediaQuery.of(context).size.height / 40,
                            ),
                          ),
                          Flexible(
                            child: Text(
                              'Commenta',
                              overflow: TextOverflow.clip,
                              style: TextStyle(
                                fontSize: MediaQuery.of(context).size.height / 40,
                                fontFamily: 'Poppins',
                                color: Colors.white,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Expanded(
                    child: Container(
                        //Share post button
                        padding: EdgeInsets.all(10),
                        margin: EdgeInsets.all(10),
                        decoration: const BoxDecoration(
                            borderRadius: BorderRadius.all(Radius.circular(20)),
                            color: Colors.red,
                            boxShadow: [
                              //Parentesi quadre perché può contenere più proprietà, è perciò una lista
                              BoxShadow(
                                color: Colors.red,
                                spreadRadius: 5,
                                blurRadius: 20,
                              )
                            ]),
                        child: Row(
                            //Contenuto del container
    
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              Padding(
                                padding: const EdgeInsets.only(right: 5),
                                child: Icon(
                                  Icons.share,
                                  color: Colors.white,
                                  size: MediaQuery.of(context).size.height / 40,
                                ),
                              ),
                              Flexible(
                                child: Text(
                                  'Condividi',
                                  style: TextStyle(
                                    fontSize: MediaQuery.of(context).size.height / 40,
                                    fontFamily: 'Poppins',
                                    color: Colors.white,
                                  ),
                                ),
                              )
                            ])),
                  ),
                ],
              ),
            ],
          ),
        )
    

    Method 3: Try with Wrap to make the buttons in multiple lines but text will be in single line

    Padding(
          padding: EdgeInsets.fromLTRB(0, MediaQuery.of(context).size.height / 40, 0, 0),
          child: Column(
            children: [
              Container(
                width: double.infinity,
                child: Wrap(
                  alignment: WrapAlignment.center,
                  children: [
                    Container(
                      //Like button
                      margin: EdgeInsets.all(10),
                      padding: EdgeInsets.all(10),
                      decoration: const BoxDecoration(
                          color: Colors.blue,
                          borderRadius: BorderRadius.all(Radius.circular(20)),
                          boxShadow: [
                            BoxShadow(
                              color: Colors.blue,
                              spreadRadius: 5,
                              blurRadius: 20,
                            )
                          ]),
    
                      child: Row(
                        mainAxisSize: MainAxisSize.min,
                        //Contenuto del container, 'child:' perché è il figlio del container
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          //'Children:[]' perché contiene più elementi
                          Padding(
                            padding: const EdgeInsets.only(right: 5),
                            child: Icon(
                              Icons.thumb_up,
                              color: Colors.white,
                              size: MediaQuery.of(context).size.height / 40,
                            ),
                          ),
    
                          Flexible(
                            child: Text(
                              'Like',
                              overflow: TextOverflow.fade,
                              style: TextStyle(
                                fontSize: MediaQuery.of(context).size.height / 40,
                                fontFamily: 'Poppins',
                                color: Colors.white,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Container(
                      //Comment button
    
                      padding: EdgeInsets.all(10),
                      margin: EdgeInsets.all(10),
    
                      decoration: const BoxDecoration(
                          borderRadius: BorderRadius.all(Radius.circular(20)),
                          color: Colors.green,
                          boxShadow: [
                            BoxShadow(
                              color: Colors.green,
                              spreadRadius: 5,
                              blurRadius: 20,
                            )
                          ]),
    
                      child: Row(
                        //Contenuto del secondo bottone (quello centrale)
                        mainAxisSize: MainAxisSize.min,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Padding(
                            padding: const EdgeInsets.only(right: 5),
                            child: Icon(
                              Icons.message,
                              color: Colors.white,
                              size: MediaQuery.of(context).size.height / 40,
                            ),
                          ),
                          Flexible(
                            child: Text(
                              'Commenta',
                              overflow: TextOverflow.clip,
                              style: TextStyle(
                                fontSize: MediaQuery.of(context).size.height / 40,
                                fontFamily: 'Poppins',
                                color: Colors.white,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Container(
                        //Share post button
                        padding: EdgeInsets.all(10),
                        margin: EdgeInsets.all(10),
                        decoration: const BoxDecoration(
                            borderRadius: BorderRadius.all(Radius.circular(20)),
                            color: Colors.red,
                            boxShadow: [
                              //Parentesi quadre perché può contenere più proprietà, è perciò una lista
                              BoxShadow(
                                color: Colors.red,
                                spreadRadius: 5,
                                blurRadius: 20,
                              )
                            ]),
                        child: Row(
                            //Contenuto del container
                            mainAxisSize: MainAxisSize.min,
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              Padding(
                                padding: const EdgeInsets.only(right: 5),
                                child: Icon(
                                  Icons.share,
                                  color: Colors.white,
                                  size: MediaQuery.of(context).size.height / 40,
                                ),
                              ),
                              Flexible(
                                child: Text(
                                  'Condividi',
                                  style: TextStyle(
                                    fontSize: MediaQuery.of(context).size.height / 40,
                                    fontFamily: 'Poppins',
                                    color: Colors.white,
                                  ),
                                ),
                              )
                            ])),
                  ],
                ),
              ),
            ],
          ),
        )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search