如何限制颤动中的两个点? [英] How to restrict two dots in flutter?
本文介绍了如何限制颤动中的两个点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要限制用户键入1 ..这样的时间。我正在使用文本输入表单字段。我需要使用小数点输入文本格式化程序的类似1.23的输入
I need to restrict when the user types 1.. like this. i'm using text input form field. I need input like 1.23 with decimal input text formatter
推荐答案
我们可以创建自己的 TextInputFormatter
。
检查此项目
import 'package:flutter/services.dart';
class DecimalTextInputFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
final regEx = RegExp(r"^\d*\.?\d*");
String newString = regEx.stringMatch(newValue.text) ?? "";
return newString == newValue.text ? newValue : oldValue;
}
}
用法:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: FirstPage(),
debugShowCheckedModeBanner: false,
);
}
}
class FirstPage extends StatefulWidget {
@override
_FirstPageState createState() => _FirstPageState();
}
class _FirstPageState extends State<FirstPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: TextField(
inputFormatters: [DecimalTextInputFormatter()],
),
),
);
}
}
这篇关于如何限制颤动中的两个点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文