我的项目中有一个课程,应该显示一列卡片。不幸的是,我遇到了一些意想不到的行为:如果字幕文本太长而被包装,整张卡的大小会缩小。
const double OUTER_SPACING = 24.0;
const double LINE_SPACING = 16.0;
class TempTypeSelection extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text('Select Type'),
),
body:
Container(
margin: EdgeInsets.all(OUTER_SPACING),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
createListTile(context, Icons.check_circle_outline, "Title", "helpertext"),
createListTile(context, Icons.check_circle_outline, "Title", "helpertext " * 5),
createListTile(context, Icons.check_circle_outline, "Title", "helpertext"),
]
))
)
);
}
Widget createListTile(BuildContext context, IconData icon, String title, String helper) {
return Card(
margin: EdgeInsets.only(bottom: LINE_SPACING),
child: InkWell(
child: Padding(
padding: const EdgeInsets.all(0.0),
child: ListTile(
leading: Icon(icon),
title: Text(title),
subtitle: Text(helper),
),
),
)
);
}
}
看这里:
这是一个错误,还是有意的,我不明白为什么?
是的,它是预期的行为。如果我们看看ListTile源代码,我们可以看到isTwoLine
处理行为:
double get _defaultTileHeight {
final bool hasSubtitle = subtitle != null;
final bool isTwoLine = !isThreeLine && hasSubtitle;
final bool isOneLine = !isThreeLine && !hasSubtitle;
if (isOneLine)
return isDense ? 48.0 : 56.0;
if (isTwoLine)
return isDense ? 64.0 : 72.0;
return isDense ? 76.0 : 88.0;
}
可悲的是,我还不能告诉你(这个行为背后的动机是什么)。如果我发现更多信息,我会更新这个答案。