搜尋套件、儲存庫與 APP 分類

auto_size_text_field 封面
套件詳情v2.3.0

auto_size_text_field

自動調整大小以完美適應其邊界的 Flutter TextField 小部件。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
19.9萬
喜歡數
302
Pub 分數
150

安裝套件

flutter pub add auto_size_text_field

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個 Flutter TextField 小部件,可自動調整大小以完美適應其邊界內的內容。它會隨著使用者輸入動態調整文字欄位的大小,確保最佳空間使用,避免溢出或過多空白區域。非常適合表單和輸入欄位,其中佈局一致性與響應式設計至關重要。該套件支援 Android、iOS、Web、macOS、Windows 與 Linux 等多個平台,適用於跨平台應用。輕量且可無縫整合至現有的 Flutter 項目中。

適用情境

  • 動態表單輸入
  • 響應式文字輸入欄位
  • 跨平台 UI 一致性
  • 自動調整大小的輸入框
  • 減少手動佈局調整

主要特色

  • 基於內容的自動調整大小
  • 跨平台相容性
  • 與 Flutter 的無縫整合
  • 支援所有主要平台
  • 所需設定最少

適合對象

  • 開發響應式表單的開發者
  • 需要自適應輸入欄位的應用程式
  • 跨平台 Flutter 項目
  • UI 設計師專注於佈局效率
  • 需要最少樣板程式碼的文字欄位專案

使用建議

  • 可能需要額外的樣式來適應複雜佈局
  • 長文字帶來的效能影響
  • 超出基本尺寸以外的自訂功能有限
  • 不適合固定大小的輸入需求
  • 依賴於父容器的約束

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Auto Size TextField

Version Ask DeepWiki GitHub license

✍️ Flutter TextField widget that automatically resizes text field to fit perfectly within its bounds.

📢 Announcement:
  • 🚒 ️️IMPORTANT v2.0.0 and above is for Flutter v3. For Flutter v2 and below, please continue to use v1.0.1
  • 🚒 BREAKING v2.2.0 Flutter v3.3 support: deprecated ToolbarOptions toolbarOptions and added support of its replacement ContextMenuBuilder. Use 2.1.1 instead if your AutoSizeTextField is still dependent on toolbarOptions.

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

TODO

  • [x] Update README.md to show examples of more use cases
  • [x] Update the example to support Flutter v3

Contents

Usage

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,
)

Note: AutoSizeTextField needs bounded constraints to resize the text. More info here.

fullwidth (default value: true)

The fullwidth parameter extends the width of TextField to its parent's BoxContraints to occupy full width.

Set to false to let the width TextField determined by the width of the text.

maxLines

The 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: null,
  decoration: InputDecoration(
    border: InputBorder.none,
    isDense: true,
    contentPadding: const EdgeInsets.all(20)
  ),
)

minFontSize & maxFontSize

The 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.

With 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 minFontSize is 12.

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,
)
stepGranularity

The 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,
)
presetFontSizes

If you want to allow only specific font sizes, you can set them with presetFontSizes. If presetFontSizes is set, minFontSize, maxFontSize and stepGranularity will be ignored.

AutoSizeTextField(
  controller: _textEditingController,
  presetFontSizes: [40, 20, 14],
  maxLines: 4,
)
overflowReplacement

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
)

Parameters

Parameter Description
key* Controls how one widget replaces another widget in the tree.
textKey Sets the key for the resulting Text widget
style* If non-null, the style to use for this text
fullwidth Default: true, It extends the width of TextField to its parent's BoxContraints to occupy full width. Set to false to let the width TextField determined by the width of the text.
minFontSize The minimum text size constraint to be used when auto-sizing text.
Is being ignored if presetFontSizes is set.
maxFontSize The maximum text size constraint to be used when auto-sizing text.
Is being ignored if presetFontSizes is set.
stepGranularity The step size in which the font size is being adapted to constraints.
presetFontSizes Predefines all the possible font sizes.
Important: presetFontSizes have to be in descending order.
locale* Used to select a font when the same Unicode character can be rendered differently, depending on the locale.
wrapWords Whether words which don't fit in one line should be wrapped. Defaults to true to behave like Text.
overflowReplacement If the text is overflowing and does not fit its bounds, this widget is displayed instead.
maxLines An optional maximum number of lines for the text to span.
minWidth An optional min width for input field. Only set it when fullwidth = false
  • Parameters marked with * behave exactly the same as in TextField.
  • The unlisted parameters behave exactly the same as TextField.

Performance

AutoSizeTextField is really fast. In fact, you can replace all your TextField widgets with AutoSizeTextField.
Nevertheless you should not use an unreasonable high fontSize in your TextStyle. E.g. don't set the fontSize to 1000 if you know, that the text will never be larger than 30.

If your font size has a very large range, consider increasing stepGranularity.

Troubleshooting

Missing bounds

If AutoSizeTextField overflows or does not resize the text, you should check if it has constrained width and height.

Wrong code:

Row(
  children: <Widget>[
    AutoSizeTextField(
      controller: _textEditingController,
      maxLines: 1,
    ),
  ],
)

Because Row and other widgets like Container, Column or 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 Row and Column or use a SizedBox or another widget with fixed width (and height).

Correct code:

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.

所屬分類

原始碼儲存庫

使用此套件的專案