【发布时间】:2018-03-15 17:52:59
【问题描述】:
所以,我需要制作一个Text 小部件来获得准确的行数。在android studio中,我只需要添加属性android:Lines = 2然后TextView将是2行,无论文本有多长(如果少于2行,则用空格填充第二行)并且不管用户设备的字体大小。
问题是,我很难动态地在颤动中复制这种行为。下面的屏幕截图仅适用于特定用户设备的 fontSize,因为它使用精确的高度 (32.0)。
new Container(
height: 32.0,
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new Expanded(
child: new Text('Curry Rice'), maxLines: 2, overflow: TextOverflow.ellipsis))
])
)
当用户增加/减少他们设备的字体大小时会出现问题,这将使固定高度的容器不再工作。
那么,无论用户的设备文本大小如何,知道如何在颤振 Text 小部件中复制 android:Lines 行为吗?
更新:为更好的上下文添加了综合代码
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// final wordPair = new WordPair.random();
return new MaterialApp(
title: 'App Title',
theme:
new ThemeData(primaryColor: Colors.white, accentColor: Colors.blue),
home: new Scaffold(
appBar: new AppBar(
title: new Text('App Title'),
actions: <Widget>[
new IconButton(icon: new Icon(Icons.list), onPressed: null),
],
),
body: new Center(
child: new Padding(
padding: new EdgeInsets.all(16.0),
child: new TestWidget()))));
}
}
class TestWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new SizedBox(
width: 150.0,
child: new Card(
child: new Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new Image.asset(
'assets/thumb2.jpg',
color: Colors.red,
),
new InkWell(
onTap: () {
// _showMenuDescription(context);
},
child: new Padding(
padding: new EdgeInsets.all(8.0),
child: new Column(
children: <Widget>[
new Container(
height: 32.0,
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new Expanded(
child: new Text(
'Curry Rice With Tandoori Chicken and Sunny Side Fried Egg',
maxLines: 2,
overflow: TextOverflow.ellipsis),
)
]),
),
new Container(height: 8.0),
new Row(
children: <Widget>[
new Expanded(
child: new Text(
'BBQ Sauce',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: new TextStyle(color: Colors.black45),
),
),
new Padding(
padding: new EdgeInsets.only(left: 8.0),
child: new Text(
'20,000',
maxLines: 1,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.end,
style: new TextStyle(
color: const Color(0xffffa000)),
))
],
),
],
))),
],
),
));
}
}
【问题讨论】:
-
你有什么解决办法吗? ,请告诉我
-
@RavindraKushwaha 事实证明这是不可能的。因为没有 minLines 属性。我认为您需要使用
MediaQuery或其他方式手动计算。
标签: android text textview flutter