Search code examples
flutterresponsive-designflutter-layoutrowflutter-test

Responsive Row Flutter


What's the best way to make this row responsive? One of the problems, I running into is if the team name have a lot of characters then there is an overflow pixel error. See screenshot

Screenshot overflow

Also see code below

child: Row(
                //mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  const SizedBox(
                    child: Padding(
                      padding: EdgeInsets.all(10.0),
                      child: Text(
                        'Bosnia and Herzegovina',
                        style: TextStyle(
                          fontFamily: 'Poppins',
                        ),
                      ),
                    ),
                  ),
                  SizedBox(
                    child: Image.network(
                      'https://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Flag_of_Bosnia_and_Herzegovina.svg/1920px-Flag_of_Bosnia_and_Herzegovina.svg.png',
                      height: 50,
                      fit: BoxFit.cover,
                    ),
                  ),
                  Card(
                    child: Container(
                      width: 70,
                      color: Colors.amber,
                      child: const Padding(
                          padding: EdgeInsets.all(10.0),
                          child: Text(
                            "4:0",
                            textAlign: TextAlign.center,
                          )),
                    ),
                  ),
                  SizedBox(
                    child: Image.network(
                      'https://upload.wikimedia.org/wikipedia/commons/thumb/c/cb/Flag_of_the_Czech_Republic.svg/1920px-Flag_of_the_Czech_Republic.svg.png',
                      height: 50,
                      fit: BoxFit.cover,
                    ),
                  ),
                  const SizedBox(
                    child: Padding(
                      padding: EdgeInsets.all(10.0),
                      child: Text(
                        'Czech Republic',
                        style: TextStyle(
                          fontFamily: 'Poppins',
                        ),
                      ),
                    ),
                  )
                ],
              ),

Solution

  • use the Expanded Widget as children of the row, so they only take the available space:

    Row(
          //mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            const Expanded(
              child: SizedBox(
                child: Padding(
                  padding: EdgeInsets.all(10.0),
                  child: Text(
                    'Bosnia and Herzegovina',
                    style: TextStyle(
                      fontFamily: 'Poppins',
                    ),
                  ),
                ),
              ),
            ),
            Expanded(
              child: SizedBox(
                child: Image.network(
                  'https://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Flag_of_Bosnia_and_Herzegovina.svg/1920px-Flag_of_Bosnia_and_Herzegovina.svg.png',
                  height: 50,
                  fit: BoxFit.cover,
                ),
              ),
            ),
            Expanded(
              child: Card(
                child: Container(
                  width: 70,
                  color: Colors.amber,
                  child: const Padding(
                      padding: EdgeInsets.all(10.0),
                      child: Text(
                        "4:0",
                        textAlign: TextAlign.center,
                      )),
                ),
              ),
            ),
            Expanded(
              child: SizedBox(
                child: Image.network(
                  'https://upload.wikimedia.org/wikipedia/commons/thumb/c/cb/Flag_of_the_Czech_Republic.svg/1920px-Flag_of_the_Czech_Republic.svg.png',
                  height: 50,
                  fit: BoxFit.cover,
                ),
              ),
            ),
            const Expanded(
              child: SizedBox(
                child: Padding(
                  padding: EdgeInsets.all(10.0),
                  child: Text(
                    'Czech Republic',
                    style: TextStyle(
                      fontFamily: 'Poppins',
                    ),
                  ),
                ),
              ),
            )
          ],
        ),
    

    the result :

    enter image description here

    you can add a row inside a column and set the flex of the Expanded Widgets:

    Column(
    
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Row(
    
            children: [
              Expanded(
                flex: 2,
                child: SizedBox(
                  child: Image.network(
                    'https://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Flag_of_Bosnia_and_Herzegovina.svg/1920px-Flag_of_Bosnia_and_Herzegovina.svg.png',
                    height: 50,
                  ),
                ),
              ),
              Expanded(
                flex: 1,
                child: Card(
                  child: Container(
                    width: 70,
                    color: Colors.amber,
                    child: const Padding(
                        padding: EdgeInsets.all(10.0),
                        child: Text(
                          "4:0",
                          textAlign: TextAlign.center,
                        )),
                  ),
                ),
              ),
              Expanded(
                flex: 2,
                child: SizedBox(
                  child: Image.network(
                    'https://upload.wikimedia.org/wikipedia/commons/thumb/c/cb/Flag_of_the_Czech_Republic.svg/1920px-Flag_of_the_Czech_Republic.svg.png',
                    height: 50,
                  ),
                ),
              ),
    
            ],
          ),
            Row(
              children: [
                const Expanded(
                  flex: 2,
                  child: SizedBox(
                    child: Padding(
                      padding: EdgeInsets.all(10.0),
                      child: Text(
                        'Bosnia and Herzegovina',
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          fontFamily: 'Poppins',
                        ),
                      ),
                    ),
                  ),
                ),
                Expanded(
                  flex: 1,
                    child: Container()
                ),
                const Expanded(
                  flex: 2,
                  child: SizedBox(
                    child: Padding(
                      padding: EdgeInsets.all(10.0),
                      child: Text(
                        'Czech Republic',
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          fontFamily: 'Poppins',
                        ),
                      ),
                    ),
                  ),
                )
              ],
            )
        ]
        ),
    

    the result:

    enter image description here enter image description here