Container(
width: double.infinity,
height: 54.w,
padding: EdgeInsets.symmetric(horizontal: 20.w),
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5),
border: Border.all(width: 1.w,color: JadeColors.grey)
),
child: TextField(
textAlign: TextAlign.start,
style: TextStyle(fontSize: 24.sp, color: JadeColors.grey_2),
keyboardType: TextInputType.number,
textInputAction: TextInputAction.done,
inputFormatters: [
NumberTextInputFormatter(2),
FilteringTextInputFormatter(RegExp("[0-9.]"), allow: true),
],
maxLines: 1,
maxLength: 40,
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
isCollapsed: true,
isDense: true,
contentPadding: EdgeInsets.zero,
hintText: '请输入原价',
hintStyle: TextStyle(
fontSize: 24.sp,
color: JadeColors.grey,
),
disabledBorder: OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.circular(8.w)
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide.none),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide.none),
border: OutlineInputBorder(
borderSide: BorderSide.none),
counterText: ''
),
onChanged: (value) {},
cursorColor: JadeColors.grey,
)),
import 'package:flutter/services.dart';
class NumberTextInputFormatter extends TextInputFormatter {
int _scale;
NumberTextInputFormatter(this._scale);
RegExp exp = new RegExp("[0-9.]");
static const String POINTER = ".";
static const String DOUBLE_ZERO = "00";
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
if (newValue.text.isEmpty) {
return TextEditingValue();
}
if (!exp.hasMatch(newValue.text)) {
return oldValue;
}
if (newValue.text.contains(POINTER)) {
if (newValue.text.indexOf(POINTER) !=
newValue.text.lastIndexOf(POINTER)) {
return oldValue;
}
String input = newValue.text;
int index = input.indexOf(POINTER);
int lengthAfterPointer = input.substring(index, input.length).length - 1;
if (lengthAfterPointer > _scale) {
return oldValue;
}
}
return newValue;
}
}