Article directory
foreword
auto_size_text :https://pub.flutter-io.cn/packages/auto_size_text
1. What is auto_size_text?
Third-party plug-ins can automatically adapt to the size of your text. to fit the boundaries.
Two, use
1. Easy to use
The style part is the same as the text, and the basic functional devices are the same as the use of the text text.
Let's make a simple comparison. We constrain to a width height. Put text in it.
- Use text: the text is not fully displayed
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: const Center(
child: SizedBox(
width: 200,
height: 140,
child: Text(
style: TextStyle(fontSize: 100),
maxLines: 2,
'Here we take the value from the MyHomePage object that was created by'),
))
// This trailing comma makes auto-formatting nicer for build methods.
);
- Auto_size_text is used: the size of the text is automatically reduced so that it can be displayed
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: const Center(
child: SizedBox(
width: 200,
height: 140,
child: AutoSizeText(
style: TextStyle(fontSize: 30),
maxLines: 2,
'Here we take the value from the MyHomePage object that was created by'),
))
// This trailing comma makes auto-formatting nicer for build methods.
);
2. Parameter description
parameter | describe |
---|---|
key* | Controls how one widget replaces another widget in the tree. |
textKey | Sets the key Text of the result widget |
style* | if non-null, the style to use for this text |
minFontSize | The minimum text size constraint to use when autosizing text. Ignored if a default font size is set. |
maxFontSize | The maximum text size constraint to use when autosizing text. Ignored if a default font size is set. |
stepGranularity | Font size fits in steps of constraints. |
presetFontSizes | All possible font sizes are predefined. Important: Must be in descending order. presetFontSizes |
group | Synchronization multiples the size of AutoSizeText |
textAlign* | How the text should be aligned horizontally. |
textDirection* | The directionality of the text. This determines how similar values are interpreted. textAlignTextAlign.startTextAlign.end |
locale* | Used to select a font when the same Unicode character can be rendered differently, depending on the locale. |
softWrap* | Whether the text should break on soft newlines. |
wrapWords | Whether words that don't fit on a line should wrap. Defaults to true to behave like text. |
overflow* | How visual overflow should be handled. |
overflowReplacement | If the text overflows and doesn't fit within its bounds, this widget is displayed instead. |
textScaleFactor* | Font pixels per logical pixel. Also affects, and. minFontSizemaxFontSizepresetFontSizes |
maxLines | An optional maximum number of lines that the text spans. |
semanticsLabel* | An alternative semantic label for this text. |
3.group
The size of each autosizetext can be unified. fontsize size. To achieve a unified font size of each text is consistent
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Column(
children: [
AutoSizeText(
"text1",
group: myautosize,
maxLines: 1,
),
AutoSizeText(
"Here we take the value from the MyHomePage object that was created byHere we take the value from the MyHomePage object that was created by",
minFontSize: 100,
group: myautosize,
)
],
)
// This trailing comma makes auto-formatting nicer for build methods.
);
- minFontSize: 100, the smallest fontsize will not be used in the end, the result is as shown in the figure
4.rich text
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Column(
children:const [
AutoSizeText.rich(
TextSpan(text: 'A really long String'),
style: TextStyle(fontSize: 20),
minFontSize: 5,
),
AutoSizeText.rich(
TextSpan(children: [
TextSpan(text: '我是 1'),
TextSpan(text: '我是 2'),
TextSpan(text: '我是 3'),
TextSpan(text: '我是 4'),
TextSpan(text: '我是 5', style: TextStyle(color: Colors.green)),
]),
style: TextStyle(fontSize: 20),
minFontSize: 5,
),
],
)
// This trailing comma makes auto-formatting nicer for build methods.
);
Summarize
Welcome to pay attention, leave a message, consult and communicate!