搜索插件、代码仓与 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

所属分类

源码仓库

使用此插件的项目

暂无相关数据。