flutter - 如何解决颤振中的“文本溢出.椭圆”?
问题描述
我无法让我的文本停止扩展屏幕和溢出。我的代码如下:
class OrderTileDisplay extends StatelessWidget {
final MenuOrderData orderItem;
final editable;
OrderTileDisplay(this.orderItem, {this.editable = true});
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
GestureDetector(
onTap: (){},
child: Container(
color: Colors.transparent,
margin: EdgeInsets.symmetric(vertical: 4),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Text(
orderItem.quantity.toString() + 'x',
style: Title16,
),
SizedBox(
width: 8,
),
Text(
orderItem.name, // OVERFLOWS WHEN IT IS A LONGER SENTENCE
style: Title18bold,
overflow: TextOverflow.ellipsis,
),
],
),
Row(
children: [
editable? Icon(Icons.edit, color: Colors.grey[200], size: 20,) : Container(),
SizedBox(width: 8,),
Text("£" + Provider.of<CP>(context).getTotalForItem(orderItem).toStringAsFixed(2),
style: Title18bold,),
],
),
],
),
),
),
Container(... and other stuff)
我曾尝试将 Text 放入容器中,然后是 Expanded 小部件,但我不断收到错误消息。不知道如何解决它。
解决方案
尝试将您的Row
and包裹Text
在一个灵活的内部:
Flexible( //flexible here
child: Row(
children: [
Text(
orderItem.quantity.toString() + 'x',
style: Title16,
),
SizedBox(
width: 8,
),
Flexible( //and here
child: Text(
orderItem.name, // no more overflow
style: Title18bold,
overflow: TextOverflow.ellipsis, //working as expected
),
),
],
),
),
正如文档所述,TextOverflow
说明应该如何处理溢出的文本,但如果它的父级没有固定大小,它将不起作用。在这种情况下,Flexible
用于限制文本的总大小以防止在达到非常大的宽度时溢出。
推荐阅读
- python - Python UnicodeDecodeError | 编码似乎不起作用
- mysql - 更新或更改表根用户的 SQL 语法错误
- python - Python Selenium:javascript错误:缺少)在参数列表之后
- bash - Ctrl-C 并不总是终止 shell 脚本
- python - SQLAlchemy 查询常用属性?
- blazor - Blazor WebAssembly JSInvokable 绑定 - 如何更新屏幕?
- dom - JavaScript - 在另一个内部添加元素总是在最后一个之前
- elm - 在 elm make 上自动缩小输出 Javascript 文件
- linux - 为什么 Linux 命令 cp 不是零拷贝?
- python - 在 Python 中访问动态字典键