Flutter - No breaking words into newline at hyphen in Text

六眼飞鱼酱① 提交于 2021-01-24 09:41:26

问题


How can I stop breaking word into newline at hyphen character. Here is the relevant code

Container(
  color: Colors.red.withOpacity(.4),
  width: 315,
  padding: const EdgeInsets.symmetric(
    horizontal: 12.0,
    vertical: 6.0,
  ),
  child: Text(
    'Ultra Low-Fat',
    style: Theme.of(context).textTheme.display1,
  ),
)

Currently the screen looks like this

But I want the text to show like this

Ultra
Low-Fat

回答1:


I managed to solve this by replacing all hyphens with non-breaking hyphen character \u2011

const nonBreakingHyphen = '\u2011';

...

Text(
  // text will now not break at hyphen
  title.replaceAll('-', nonBreakingHyphen),
)



回答2:


You can do this

child:Column(
    children:<widgets>(
         Row(
            children:<widgets>(
                Text("Ultra", //your theme)),
         Row(
            children:<widgets>(
                Text("Low-Fat", //your theme)
            )
        )
    )


来源:https://stackoverflow.com/questions/59441148/flutter-no-breaking-words-into-newline-at-hyphen-in-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!