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

fitted_text_field_container 封面
套件詳情v2.0.0

fitted_text_field_container

一個自動調整大小以適應 Material TextField 文字值的 Container。

近 30 天下載
45
喜歡數
27
Pub 分數
70

安裝套件

flutter pub add fitted_text_field_container

套件介紹

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

一個 Flutter 套件,將 Material TextField 包裝在 Container 中,該容器會動態調整大小以適應文字內容。它確保容器的尺寸在使用者輸入時自動調整,維持視覺一致性而無需手動設定大小。非常適合表單和輸入欄位,其中空間效率和版面回應性至關重要。此套件輕量且可順暢整合至現有的 Flutter UI 中。

適用情境

  • 動態表單佈局
  • 響應式輸入容器
  • 自動調整大小的文本欄位
  • 具有變數文字長度的乾淨使用者介面設計
  • 輸入欄位在受限空間中

主要特色

  • 自動容器調整大小
  • Material TextField 結合
  • 無需手動設定寬度/高度
  • 輕量且簡單
  • 無縫相容 Flutter

適合對象

  • 需要響應式輸入容器的開發者
  • UI 設計師專注於簡潔的版面
  • 具有可變文字輸入的表單建構器
  • 需要最少樣板程式碼的專案

使用建議

  • 僅限文字內容
  • 不支援多行文字
  • 無內建驗證或遮蔽
  • 不適合複雜的表單邏輯
  • 依賴於父級 widget 的約束

原始 README

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

Fitted TextField Container

A Container that automatically resizes it's width to fit the containing TextField's text value.

Read the short blog: https://medium.com/@huyffs/fitted-textfield-container-for-flutter-c9c63644441c

Usage

FittedTextFieldContainer
Param Type Default Description
child TextField * required The TextField to fit
calculator CalculateFunction FittedTextFieldCalculator.fitVisible A function to calculate the width. The default function fits the visible content & decorations (i.e. it collapses to the content/label's width when hint text disappears)
builder Widget Function(BuildContext context, TextField child) null A builder that can be used to build complex layouts. The child TextField is provided to the build function
Example
FittedTextFieldContainer(
    child: TextField(
        controller: someTextEditingController,
        textAlign: TextAlign.right,
        decoration: InputDecoration(
            prefixText: "£",
            labelText: "Amount",
        ),
    ),
)
AnimatedFittedTextFieldContainer

All params from FittedTextFieldContainer plus the following

Param Type Default Description
growCurve Curve Curves.easeOutCirc The curve to use in the grow animation
growDuration Duration Duration(milliseconds: 300) Duration to animate the container's width when TextField grows
shrinkCurve Curve Curves.easeInCirc The curve to use in the shrink animation
shrinkDuration Duration Duration(milliseconds: 600) Duration to animate the container's width when TextField shrinks
Example
AnimatedFittedTextFieldContainer(
    growDuration: Duration(milliseconds: 300),
    shrinkDuration: Duration(milliseconds: 600),
    growCurve: Curves.easeOutCirc,
    shrinkCurve: Curves.easeInCirc,
    child: TextField(
        controller: someTextEditingController,
        textAlign: TextAlign.right,
        decoration: InputDecoration(
            prefixText: "£",
            labelText: "Amount",
        ),
    ),
)
FittedTextFieldMeasurer

A class that takes a TextField and TextStyle and returns the following measurements:

  • textField The fitted textfield
  • textStyle The fitted textfield's style or the default style
  • textWidth Width of the text content
  • labelWidth Width of the label
  • hintWidth Width of the hint text
  • fixedWidths Width of fixed elements (prefixText, suffixText, contentPadding and cursorWidth)
CalculateFunction

This function takes measured values and returns a width value.

Signature:
double CalculateFunction(FittedTextFieldMeasurer)
Example:
FittedTextFieldContainer(
    calculator: (m) =>
        m.fixedWidths +
        max(m.labelWidth, max(m.hintWidth, m.textWidth)),
    child: TextField(
        controller: someTextEditingController,
        textAlign: TextAlign.right,
        decoration: InputDecoration(
            hintText: "Width of hint text",
        ),
    ),
);
Predefined functions

A few calculators are provided for common use-cases

  • fitAll fits content, label and hint texts
  • fitVisible fits the visible content & decorations (i.e. it collapses to the content/label's width when hint text disappears)
  • fitVisibleWithPadding(double padding) in addition to fitting visible content it adds a fixed padding value
  • fitVisibleWithRange(double min, double max) in addition to fitting visible content it enforces a minimum and (optional) maximum width
Example:

Fit all

FittedTextFieldContainer(
    calculator: FittedTextFieldCalculator.fitAll,
    child: ...
);

Fit visible with minimum

FittedTextFieldContainer(
    calculator: FittedTextFieldCalculator.fitVisibleWithRange(120),
    child: ...
);

Fit visible with maximum

FittedTextFieldContainer(
    calculator: FittedTextFieldCalculator.fitVisibleWithRange(0, 180),
    child: ...
);
Using the builder
AnimatedFittedTextFieldContainer(
    child: TextField(
        controller: someTextEditingController,
        textAlign: TextAlign.right,
        decoration: InputDecoration(
            labelText: "Twinkle twinkle",
        ),
    ),
    builder: (context, child) => Container(
        child: Stack(
            overflow: Overflow.visible,
            children: <Widget>[
                child,
                Positioned(
                    top: 0,
                    right: -22,
                    child: _aniWholePoundCtl.text.isEmpty
                        ? Icon(Icons.star_border)
                        : Icon(Icons.star, color: Colors.amber),
                ),
            ],
        ),
    ),
)

Contribute

Pull requests are welcome!

Licence

MIT License

Copyright (c) 2020 Huy Dinh

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。