搜索插件、代码仓与 APP 分类

masked_text_field 封面
插件详情v1.0.2

masked_text_field

一个用于带掩码的文本字段的包,可格式化您的文本。如果您需要电话号码、IP 地址或任何其他格式的掩码,请使用此包。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
656
喜欢数
29
Pub 分数
150

安装插件

flutter pub add masked_text_field

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Flutter 包,可实现带有可自定义掩码的格式化文本输入,适用于电话号码、IP 地址或其他结构化数据字段。用户输入时会自动应用格式,确保输入模式一致。该包支持实时验证,并兼容所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux。通过强制格式,简化表单处理并减少用户错误。

适合场景

  • 电话号码输入
  • IP地址输入
  • 信用卡号码格式化
  • 日期和时间字段屏蔽
  • 自定义格式输入
  • 表单验证协助

主要特点

  • 实时遮罩应用
  • 可配置的掩码模式
  • 跨平台支持
  • 自动光标定位
  • 内置验证
  • 与 TextFormField 的轻松集成

适合对象

  • 格式要求严格的表单输入
  • 带有数据录入表单的移动和网络应用程序
  • 需要可重用输入掩码的开发人员
  • 支持国际号码格式的应用程序
  • 以用户体验为中心的输入体验

使用建议

  • 可能需要仔细设计遮罩模式
  • 限制为文本输入
  • 复杂遮罩可能带来的性能影响
  • 不适用于非文本输入类型
  • 需要手动处理焦点和选择

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Masked Text Field

Masked Text Field

pub package pub points LinkedIn Instagram Facebook Youtube

Features

A package for masked text field for format your text (eg. Date, phone number, IP address etc.), so if you want a mask for phone number, or IP address or any kind of format. Use this package.
Image

Getting started

Add the dependency in pubsec.yaml file.

dart
  dependencies:
    flutter:
      sdk: flutter
    masked_text_field: any

Usage Example

import flutter_social_button.dart


import 'package:masked_text_field/masked_text_field.dart';

for add Text Field

It's very simple, it's a Widget as all the other ones.


MaskedTextField
(
    maskedTextFieldController: textfieldController,
    mask: "xxx.xxx.xxx.xxx",
    maxLength: 15,
    keyboardType: TextInputType.number,
    inputDecoration: new InputDecoration(
    hintText: "192.192.192.192", 
    counterText: "",
    labelText: "IP Address"),
);

'x' is the normal char that your text will have.

this sample reproduces something like this in the end: 192.192.192.192.

You can also change the escape character you want to use.

If you want to change it, you just need to fill the property escapeCharacter with the character you want.


MaskedTextField
(
    maskedTextFieldController: textfieldController,
    escapeCharacter: '#',
    mask: "###.###.###.###",
    maxLength: 15,
    keyboardType: TextInputType.number,
    inputDecoration: new InputDecoration(
    counterText: "",
    hintText: "192.192.192.192", 
    labelText: "IP Address"),
);

'x' is the default escape character.

Example


import 'package:flutter/material.dart';
import 'package:masked_text_field/masked_text_field.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomePage(title: 'Masked Text Field'),
    );
  }
}

class HomePage extends StatefulWidget {
  final String title;
  const HomePage({Key? key, required this.title}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  var ipController = TextEditingController();
  var dateController = TextEditingController();
  var phoneController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text('Enter IP Address'),
            const SizedBox(height: 5,),
            MaskedTextField(
              textFieldController: ipController,
              inputDecoration: const InputDecoration(
                hintText: '192.192.192.192',
                  counterText: ""
              ),
              autofocus: true,
              mask: 'xxx.xxx.xxx.xxx',
              maxLength: 15,
              keyboardType: TextInputType.number,
              onChange: (String value) {
                print(value);
              },
            ),
            const SizedBox(height: 10,),
            const Text('Enter Date'),
            const SizedBox(height: 5,),
            MaskedTextField(
              textFieldController: dateController,
              inputDecoration: const InputDecoration(
                  hintText: 'DD/MM/YYYY',
                  counterText: ""
              ),
              autofocus: true,
              mask: 'xx/xx/xxxx',
              maxLength: 10,
              keyboardType: TextInputType.number,
              onChange: (String value) {
                print(value);
              },
            ),
            const SizedBox(height: 10,),
            const Text('Enter Phone Number'),
            const SizedBox(height: 5,),
            MaskedTextField(
              textFieldController: phoneController,
              inputDecoration: const InputDecoration(
                  hintText: '98765-43210',
                  counterText: "",
                prefixText: '+91 ',
              ),
              autofocus: true,
              mask: 'xxxxx-xxxxx',
              maxLength: 11,
              keyboardType: TextInputType.number,
              onChange: (String value) {
                print(value);
              },
            ),
          ],
        ),
      ),
    );
  }
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

所属分类

源码仓库

使用此插件的项目

暂无相关数据。