【发布时间】:2018-05-19 18:58:51
【问题描述】:
我正在尝试在 FittedBox 的 fit 属性中使用 BoxFit.scaleDown 来缩小 Text 小部件中的字体以适应不同长度的字符串。
但是,下面的代码将缩小整个字符串并使其适合一行,对于下面的示例,我希望缩小字体以便字符串可以适合两行(根据 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: '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,
),
),
],
),
),
);
}
}
【问题讨论】:
-
你想在哪里换行?
-
我认为任何地方都可以,只要文本扩展为使用最大行数。
-
maxLines 是否适合您?它始终显示在带有拟合框的单行中