TA的每日心情 | 奋斗 2023-7-10 09:34 |
---|
签到天数: 36 天 [LV.5]常住居民I
荣誉会员
- 积分
- 668
|
本站资源全部免费,回复即可查看下载地址!
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
背景
在开发中经常会遇到需要对输入的数字限制的需求,比如限制输入几位小数,或者只能输入整数,或者支持负数。。。
实现类
[JavaScript] 纯文本查看 复制代码 import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
/*
* @Author: zhudaihao
* @Date: 2022/2/25
* @Describe: 限制输入数字和小数后位数
*/
class NumberInputLimit extends TextInputFormatter {
///输入字符的范围
String inputScope;
///允许的小数位数
final int? digit;
///允许的最大值
final double? max;
///是否支持 false不支持负数(默认不支持)
final bool isNegative;
NumberInputLimit({
this.inputScope = '-.0123456789',
this.digit,
this.max,
this.isNegative = false,
});
///获取value小数点后有几位
static int getDecimalAfterLength(String value) {
if (value.contains(".")) {
return value.split(".")[1].length;
} else {
return 0;
}
}
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
//上次文本
String oldContent = oldValue.text;
//最新文本
String newContent = newValue.text;
//上次文本长度
int oldLength = oldContent.length;
//最新文本长度
int newLength = newContent.length;
//上次文本光标位置
int oldBaseOffset = oldValue.selection.baseOffset;
//最新文本光标位置
int newBaseOffset = newValue.selection.baseOffset;
//光标位置
int offset = newBaseOffset;
if (newLength > oldLength) {
//输入的字符
String inputContent = newContent.substring(oldBaseOffset, newBaseOffset);
if (!isNegative) {
inputScope = inputScope.replaceAll("-", "");
}
if (inputScope.contains(inputContent)) {
if (oldLength > 0) {
if ((max != null && double.parse(newContent) > max!) ||
(digit != null && getDecimalAfterLength(newContent) > digit!)) {
newContent = oldContent;
offset = oldBaseOffset;
} else if (oldContent.substring(0, 1) == "-") {
//上次文本首字符是-
if ((oldContent.contains(".") && inputContent == ".") ||
inputContent == "-" ||
(oldContent.contains(".") &&
newLength > 2 &&
newContent.substring(2, 3) != "." &&
newContent.substring(1, 2) == "0") ||
(newLength > 2 && newContent.substring(0, 3) == "-00") ||
(newLength > 2 &&
!newContent.contains(".") &&
newContent.substring(1, 2) == "0") ||
(oldContent.substring(0, 1) == "-" &&
newContent.substring(0, 1) != "-")) {
newContent = oldContent;
offset = oldBaseOffset;
}
} else if (oldContent.substring(0, 1) == "0") {
//上次文本首字符是0
if (newLength > 1 && newContent.substring(0, 2) == "00" ||
(newContent.contains("-") &&
newContent.substring(0, 1) != "-") ||
(oldContent.contains(".") && inputContent == ".") ||
(newContent.substring(0, 1) == "0" &&
newLength > 1 &&
newContent.substring(1, 2) != ".")) {
newContent = oldContent;
offset = oldBaseOffset;
}
} else if (newContent.contains(".")) {
//上次文本首字符是.
if ((oldLength > 1 &&
oldContent.substring(0, 2) == "0." &&
inputContent == ".") ||
(newContent.substring(0, 1) != "-" &&
newContent.contains("-")) ||
(oldContent.contains(".") && inputContent == ".") ||
(oldContent.contains(".") &&
oldContent.substring(0, 1) != "." &&
newContent.substring(0, 1) == "0")) {
newContent = oldContent;
offset = oldBaseOffset;
}
}
}
} else {
//输入限制范围外字符
newContent = oldContent;
offset = oldBaseOffset;
}
}
return TextEditingValue(
text: newContent,
selection: TextSelection.collapsed(offset: offset),
);
}
}
使用代码
[JavaScript] 纯文本查看 复制代码 Scaffold(
appBar: AppBar(
title: const Text("测试编辑限制"),
),
body: Material(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text("不限制"),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
decoration: const InputDecoration(hintText: "请输入"),
inputFormatters: [
NumberInputLimit(),
],
),
),
const Text("支持负数"),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
decoration: const InputDecoration(hintText: "请输入"),
inputFormatters: [
//限制小数位数
NumberInputLimit(
isNegative: true,
),
],
),
),
const Text("限制输入最大值100"),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
decoration: const InputDecoration(hintText: "请输入"),
inputFormatters: [
//限制小数位数
NumberInputLimit(
max: 100,
),
],
),
),
const Text("限制输入小数点2位"),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
decoration: const InputDecoration(hintText: "请输入"),
inputFormatters: [
//限制小数位数
NumberInputLimit(
digit: 2,
),
],
),
),
const Text("限制输入所有字符范围"),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
decoration: const InputDecoration(hintText: "请输入"),
inputFormatters: [
//限制小数位数
NumberInputLimit(
inputScope: "123456",
),
],
),
),
],
),
),
)
|
温馨提示:
1、本站所有内容均为互联网收集或网友分享或网络购买,本站不破解、不翻录任何视频!
2、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意!
3、本站资源仅供本站会员学习参考,不得传播及用于其他用途,学习完后请在24小时内自行删除.
4、本站资源质量虽均经精心审查,但也难保万无一失,若发现资源有问题影响学习请一定及时点此进行问题反馈,我们会第一时间改正!
5、若发现链接失效了请联系管理员,管理员会在2小时内修复
6、如果有任何疑问,请加客服QQ:1300822626 2小时内回复你!
|