I’m attempting to fix a button in my popup, which has an unusual issue with the last letter:
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
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.
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:
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: