简体   繁体   English

如何自动缩放窗口小部件以适合不同的屏幕尺寸?

[英]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.

相关问题 如何针对不同的屏幕尺寸缩放 Flutter Gridview 内容 - How to scale Flutter Gridview content for different screen sizes 如何在 flutter 中针对不同屏幕尺寸的堆栈中的 position 文本小部件? - How to position text widget in stack for different screen sizes in flutter? 如何根据 flutter 中的不同屏幕尺寸对齐堆栈内的小部件 - How to align widget inside stack according to different screen sizes in flutter 调整按钮以适应不同的屏幕尺寸 - Resizing button to fit different screen sizes 根据 Flutter 中的不同屏幕尺寸有效缩放此 UI 的最佳实践 - Best practice for effectively scale this UI according to different screen sizes in Flutter 如何在不同的屏幕尺寸上测试 Flutter 小部件? - How to test Flutter widgets on different screen sizes? 如何使用颤振应用处理不同的屏幕尺寸 - how to handle different screen sizes with flutter app 如何在颤动中获得相对于屏幕大小的小部件大小? - How can I have widget sizes relative to the screen size in flutter? 使不同的图像尺寸适合按钮 - Fit different image sizes to buttons 将小部件定位在堆栈中,在模拟器中或根据不同的屏幕尺寸更改 position。 Flutter - Positioned widget in a Stack changing position in emulator or according to different screen sizes. Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM