auto_size_text_field: ^0.1.7 copied to clipboard
Flutter TextField widget that automatically resizes text field to fit perfectly within its bounds.
Auto Size TextField #
✍️ Flutter TextField widget that automatically resizes text field to fit perfectly within its bounds.
Note: Kudos to @leisim! This work is inspired by his awesome lib Auto Size Text. Please check it out if you want to auto size the Flutter
Text widget content.
Show some ❤️ and star the repo to support the project
- [ ] Update README.md to show examples of more use cases
- [ ] Add more unit tests
AutoSizeTextField behaves exactly like a
TextField. The only difference is that it resizes text to fit within its bounds.
AutoSizeTextField( controller: _textEditingController, style: TextStyle(fontSize: 20), maxLines: 2, )
AutoSizeTextField needs bounded constraints to resize the text. More info here.
fullwidth (default value:
fullwidth parameter extends the width of
TextField to its parent's
BoxContraints to occupy full width.
false to let the width
TextField determined by the width of the text.
maxLines parameter works like you are used to with the
Text widget. If there is no
maxLines parameter specified, the
AutoSizeTextField only fits the text according to the available width and height.
AutoSizeTextField( controller: _textEditingController, style: TextStyle(fontSize: 30), maxLines: 2, )
minFontSize & maxFontSize #
AutoSizeTextField starts with
TextStyle.fontSize. It measures the resulting text and rescales it to fit within its bonds. You can however set the allowed range of the resulting font size.
minFontSize you can specify the smallest possible font size. If the text still doesn't fit, it will be handled according to
overflow. The default
maxFontSize sets the largest possible font size. This is useful if the
TextStyle inherits the font size and you want to constrain it.
AutoSizeTextField( controller: _textEditingController, style: TextStyle(fontSize: 30), minFontSize: 18, maxLines: 4, overflow: TextOverflow.ellipsis, )
AutoSizeTextField will try each font size, starting with
TextStyle.fontSize until the text fits within its bounds.
stepGranularity specifies how much the font size is decreased each step. Usually, this value should not be below
1 for best performance.
AutoSizeTextField( controller: _textEditingController, style: TextStyle(fontSize: 40), minFontSize: 10, stepGranularity: 10, maxLines: 4, overflow: TextOverflow.ellipsis, )
If you want to allow only specific font sizes, you can set them with
presetFontSizes is set,
stepGranularity will be ignored.
AutoSizeTextField( controller: _textEditingController, presetFontSizes: [40, 20, 14], maxLines: 4, )
If the text is overflowing and does not fit its bounds, this widget is displayed instead. This can be useful to prevent text being too small to read.
AutoSizeTextField( maxLines: 1, controller: _textEditingController )
|Controls how one widget replaces another widget in the tree.|
|Sets the key for the resulting |
|If non-null, the style to use for this text|
|The minimum text size constraint to be used when auto-sizing text. |
Is being ignored if
|The maximum text size constraint to be used when auto-sizing text. |
Is being ignored if
|The step size in which the font size is being adapted to constraints.|
|Predefines all the possible font sizes.|
|Used to select a font when the same Unicode character can be rendered differently, depending on the locale.|
|Whether words which don't fit in one line should be wrapped. Defaults to |
|If the text is overflowing and does not fit its bounds, this widget is displayed instead.|
|An optional maximum number of lines for the text to span.|
|An optional min width for input field. Only set it when fullwidth = false|
- Parameters marked with * behave exactly the same as in
- The unlisted parameters behave exactly the same as
AutoSizeTextField is really fast. In fact, you can replace all your
TextField widgets with
Nevertheless you should not use an unreasonable high
fontSize in your
TextStyle. E.g. don't set the
1000 if you know, that the text will never be larger than
If your font size has a very large range, consider increasing
Missing bounds #
AutoSizeTextField overflows or does not resize the text, you should check if it has constrained width and height.
Row( children: <Widget>[ AutoSizeTextField( controller: _textEditingController, maxLines: 1, ), ], )
Row and other widgets like
ListView do not constrain their children, the text will overflow.
You can fix this by constraining the
AutoSizeTextField. Wrap it with
Expanded in case of
Column or use a
SizedBox or another widget with fixed width (and height).
Row( children: <Widget>[ Expanded( // Constrains AutoSizeTextField to the width of the Row child: AutoSizeTextField( controller: _textEditingController, maxLines: 1, ) ), ], ) }
MinFontSize too large #
AutoSizeTextField does not resize text below the
minFontSize which defaults to 12.:
MIT License #
Copyright (c) 2020 Zhuoran LI Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.