[英]How to automatically scale a widget to fit different screen sizes?
I have a ListView of cards wrapping a ListTile, inside each list tile I have title and a subtitle, I tried to debug my app on several phones (android/ios). 我有一张包裹有ListTile的卡片的ListView,在每个列表磁贴中都有标题和副标题,我尝试在多部电话(android / ios)上调试我的应用程序。 For some of theses phones, the subtitle text overflows the card widget because they have smaller screen. 对于这些电话中的某些电话,由于字幕屏幕较小,因此字幕文本会使卡窗口小部件溢出。 Is there a way to effectively fit my text widgets according to screen size ? 有没有一种方法可以根据屏幕大小有效地适合我的文本小部件?
You can assign maxLines and overflow attributes to a Text widget: 您可以将maxLines和溢出属性分配给Text小部件:
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
static const String _longText = 'Lorem ipsum dolor sit amet, consectetur adip'
'iscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna '
'aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco labor'
'is nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in rep'
'rehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatu'
'r. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui off'
'icia deserunt mollit anim id est laborum.';
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('TestProject'),
),
body: new Card(
child: new ListTile(
title: const Text('Title'),
subtitle: const Text(
_longText,
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
)),
);
}
}
The problem is that you're using ListTile for stuff they are not made for. 问题在于,您正在使用ListTile来处理并非由其制成的东西。
A listTile, according to material.io, is at most 3 lines of text. 根据material.io,listTile 最多为 3行文本。 https://material.io/guidelines/components/lists.html#lists-usage https://material.io/guidelines/components/lists.html#lists-usage
Use a card instead, which fits the content. 请改用适合内容的卡片。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.