flutter - Flutter Text 仅在空格处换行
问题描述
我的问题是颤动会在非空白字符处破坏文本:
我怎样才能告诉颤振不要在“/”处敲击文本,而只有在遇到空格时才可以?
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
body: Center(
child: Container(
width: 220.0,
child: Text(
'This should not break here x/y/z',
style: TextStyle(fontSize: 30.0),
),
),
),
),
);
}
}
我一直在研究像 auto_size_text 这样的包,但没有找到解决这个问题的方法。
编辑:
使用 '\n' 不是一个选项,因为文本的长度会有所不同,但它总是包含三个 x/y/z 字符,最后用斜线分隔。
我宁愿不提前计算渲染文本的大小来确定是否应该在 x/y/z 之前包含换行符。这似乎有点重,因为这些是包含在 ListView 中的卡片标题。
在网络上没有问题,文本只在空格处被破坏:
<!DOCTYPE html>
<html>
<body>
<div style="background-color: grey; height: 200px; width: 400px;">
<p style="font-size: 24.0pt;">
This should not break here x/y/z
</p>
</div>
</body>
</html>
解决方案
我通常使用TextAlign.justify
适合文本,并且根据我的喜好,您可以尝试:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
body: Center(
child: Container(
width: 220.0,
child: Text(
'This should not break here x/y/z',
textAlign: TextAlign.justify,
style: TextStyle(fontSize: 30.0),
),
),
),
),
);
}
}
也TextAlign.center
不错。
推荐阅读
- python - 如何在给定的边界框(lon1,lon2,lat1,lat2)内通过时间获得变量的平均值?
- dart - Matrix4 参数在间接传递给 Transform 时表现不同
- wsdl - 如何在 First Data 全球网关 e4 中使用第一个数据令牌?
- android - 我以编程方式使用来自 android 的内容值添加了谷歌日历事件
- phpspreadsheet - 使用 phpspreadsheet 的多行标题
- ionic-framework - 如何在离子滑块中延迟加载图像?
- chromecast - 用 chromecast 连接谷歌助手(如 netflix)
- docker - 当我在 gitlab ci before_script 中使用 apt 时找不到 apt
- mysql - 具有子值的 MySQL 递归查询
- angular - Angular 5:材质下拉列表的事件处理