skip to Main Content

I’m attempting to fix a button in my popup, which has an unusual issue with the last letter:

enter image description here

I need to set "maxLines" to 2, and I’m wondering if there’s a method to wrap the text in the middle to make it more readable.

Any suggestions?

SizedBox(
  height: 40,
  child: ElevatedButton(
    onPressed: () => (),
    style: ElevatedButton.styleFrom(
      padding: const EdgeInsets.symmetric(horizontal: 20),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
      elevation: 0,
    ),
    child: const Text(
      "Thisisalongtextwr",
      style: TextStyle(
        fontWeight: FontWeight.bold,
        fontSize: 14,
      ),
      textAlign: TextAlign.center,
      maxLines: 2,
    ),
  ),
),

2

Answers


  1. Yes, simply insert a space in the middle.

    You can also insert the special Line Separator character (U+2028) which is invisible but works like a space in the sense that it breaks a word in that spot if needed.

    Login or Signup to reply.
  2. You have to break your text String into separate strings to let the compiler know where exactly you need the next line to start from, then use a Wrap widget to put them together.

    For example:

    List<String> textList = [
      'This',
      'is',
      'a',
      'long',
      'textwr',
    ];
    
    Expanded(
      child: SizedBox(
        height: 40,
        child: ElevatedButton(
          onPressed: () {},
          style: ElevatedButton.styleFrom(
            padding: const EdgeInsets.symmetric(horizontal: 20),
            shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(10)),
            elevation: 0,
          ),
          child: Wrap(
            children: [
              for (String text in words)
                Text(
                  text,
                  style: TextStyle(
                    fontSize: 14,
                    color: Colors.white,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    

    enter image description here

    You can use packages or Regex to detect words inside your string and break them. For example, if your string has specific characters like a ' to help determine where to break the line, you can use regex like this:

    String myString = "dafjb'Thisisalong'dnijn'textwr'djabkjbk";
    RegExp exp = RegExp(r"('(.*?)')");
      List<String> _list =[];
      for (var m in exp.allMatches(myString)) {
        _list.add(m[1].toString());
       
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search