Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a mask in a TextField in Flutter?

I'm trying to add a date mask to a textField since I did not like the Date Picker because for date of birth, for example, it is not as nimble. After that, converting from string to datetime, I believe I can continue the project, Thanks in advance.

static final TextEditingController _birthDate = new TextEditingController();
    new TextFormField( 
            controller: _birthDate, 
            maxLength: 10,
            keyboardType: TextInputType.datetime, 
            validator: _validateDate
        ), String _validateDate(String value) { 
    if(value.isEmpty)
        return null;
    if(value.length != 10)
        return 'Enter date in DD / MM / YYYY format';
    return null; 
}
like image 212
Victor Henrique Avatar asked Apr 24 '18 18:04

Victor Henrique


People also ask

How do I hide text in TextField Flutter?

First, create a text field or text from field. Then declare a variable inside the class called _isvisible or anything you want and set the value false.

How do you customize a TextField in Flutter?

By default, a TextField is decorated with an underline. You can add a label, icon, inline hint text, and error text by supplying an InputDecoration as the decoration property of the TextField . To remove the decoration entirely (including the underline and the space reserved for the label), set the decoration to null.

How do you add a TextField style in Flutter?

You can use the decoration on TextField like this: decoration: InputDecoration( hintText: 'Type Text Here', enabledBorder: UnderlineInputBorder( borderSide: BorderSide(color: Colors. red), ), And this will change the line to color red.

How to add masked text input for flutter?

flutter_masked_text Masked text input for flutter. Install Follow this GUIDE Usage Import the library import 'package:flutter_masked_text/flutter_masked_text.dart'; MaskedText Create your mask controller: var controller = new MaskedTextController(mask: '000.000.000-00');

What is a text field in flutter?

Text fields allow users to type text into an app. They are used to build forms, send messages, create search experiences, and more. In this recipe, explore how to create and style text fields. Flutter provides two text fields: TextField and TextFormField. TextField is the most commonly used text input widget.

How to create a mask controller for a text field?

Create your mask controller: var controller = new MaskedTextController(mask: '000.000.000-00'); Set controller to your text field:

How to start a mask with initial value and update text?

Initial Value To start a mask with initial value, just use textproperty on constructor: var controller = new MaskedTextController(mask: '000-000', text: '123456'); Update text programaticaly If you want to set new text after controller initiatialization, use the updateTextmethod:


1 Answers

https://pub.dartlang.org/packages/masked_text

masked_text

A package for masked texts, so if you want a mask for phone, or zip code or any kind of mask, just use it :D

Getting Started

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

new MaskedTextField
(
    maskedTextFieldController: _textCPFController,
    mask: "xx/xx/xxxx",
    maxLength: 10,
    keyboardType: TextInputType.number,
    inputDecoration: new InputDecoration(
    hintText: "Digite a data do seu nascimento", labelText: "Data"),
);

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

this sample reproduces something like this in the end: 11/02/1995.

like image 87
Enzo Tiezzi Avatar answered Oct 01 '22 00:10

Enzo Tiezzi