Flutter snackbar throws render overflow error when displaying multiple lines of text

361

Remove Expanded and wrap Text with Flexible,

 content: Row(
          children: [
            const Icon(
              Icons.warning,
              color: Colors.red,
              size: 22,
            ),
            const SizedBox(
              width: 7,
            ),
            Flexible(
              child: Text(
                errorMessage,
                style: const TextStyle(
                    fontWeight: FontWeight.normal,
                    fontSize: 17,
                    color: Colors.red),
              ),
            ),
          ],
        ),
Share:
361
Sumchans
Author by

Sumchans

Updated on January 03, 2023

Comments

  • Sumchans
    Sumchans over 1 year

    Is there a way to display multiple lines of text on a snackbar without it throwing a renderflex error?

    enter image description here

    I tried wrapping it with Flexible and Expanded widgets but both didn't help, I still get the error. The errorMessage received by the below method could be long sometimes.

      void displayMessage(context, errorMessage) {
        ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              content: Expanded(
                child: Row(
                    children: [
                      const Icon(
                        Icons.warning,
                        color: Colors.red,
                        size: 22,
                      ),
                      const SizedBox(
                        width: 7,
                      ),
                      Text(
                        errorMessage,
                        style: const TextStyle(
                            fontWeight: FontWeight.normal,
                            fontSize: 17,
                            color: Colors.red),
                      ),
                    ],
                  ),
              ),
              backgroundColor: Colors.black,
              padding: const EdgeInsets.all(20),
              duration: const Duration(milliseconds: 5000),
              behavior: SnackBarBehavior.floating,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10.0),
              ),
            )
        );
      }
    
    • Yeasin Sheikh
      Yeasin Sheikh over 2 years
      Can you include a demo image ? Removing Expanded works fine.
    • Sumchans
      Sumchans over 2 years
      @YeasinSheikh just added the image
  • Sumchans
    Sumchans over 2 years
    I am still getting the same error after the modification
  • Yeasin Sheikh
    Yeasin Sheikh over 2 years
    check update answer