Created
July 16, 2018 21:18
-
-
Save manujbahl/82ddf859c60700a697fecb58114a0b2f to your computer and use it in GitHub Desktop.
TextField formatter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter/services.dart'; | |
main() { | |
runApp(new Home()); | |
} | |
class MyFormatter extends TextInputFormatter { | |
MyFormatter(); | |
@override | |
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) { | |
var val = newValue.text + "-"; | |
return new TextEditingValue(text: val, composing: newValue.composing, selection: newValue.selection); | |
} | |
} | |
class Home extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
var widget = new MaterialApp( | |
home: new Scaffold( | |
body: new Center( | |
child: new Container( | |
width: 300.0, | |
child: new TextField( | |
keyboardType:TextInputType.number, | |
inputFormatters: [ | |
new MyFormatter() | |
], | |
) | |
) | |
) | |
), | |
); | |
return widget; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment