如何自动缩小“文本”小部件中的字体以适合最大行数? [英] How do I auto scale down a font in a Text widget to fit the max number of lines?
问题描述
我试图在 FittedBox
的fit属性中使用 BoxFit.scaleDown
来缩小字体在 Text
小部件中可以容纳不同长度的字符串。
I'm trying to use BoxFit.scaleDown
in a FittedBox
's fit property to scale the font down in a Text
widget to accommodate strings of varying length.
但是,以下代码将按比例缩小整个字符串并使其适合于一行,对于下面的示例,我希望按比例缩小字体,以便字符串可以适合于两行(每个 maxLines
属性c $ c> Text 小部件)。
However, the below code will scale down the entire string and make it fit on one line, For the below example, I would like the font scaled down so that the string can fit on two lines (per maxLines
property of the Text
widget).
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Multi-Line Label Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Multi-Line Label Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
final textStyle = const TextStyle(fontSize: 28.0);
final text =
'Lorem Ipsum is simply dummy text of the printing and typesetting'
'industry. Lorem Ipsum has been the industry\'s standard dummy text'
'ever since the 1500s, when an unknown printer took a galley of type'
'and scrambled it to make a type specimen book.';
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new FittedBox(
fit: BoxFit.scaleDown,
child: new Text(
text,
maxLines: 2,
style: textStyle,
textAlign: TextAlign.center,
),
),
],
),
),
);
}
}
推荐答案
you可以使用自动文本包
you can use the auto text package
https:// pub.dartlang.org/packages/auto_size_text
安装后,只需将所有'Text'替换为'AutoSizeText',您会发现一切都会很好:)
after install it, just replace all of 'Text' to 'AutoSizeText' and you will find every things will be good :)
这篇关于如何自动缩小“文本”小部件中的字体以适合最大行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!