How to remove 0 numbers without loosing your cursor position.
TextField(
controller: controller,
onChanged: (value) {
var selection = controller.selection;
final RegExp regexp = new RegExp(r'^0+(?=.)');
var match = regexp.firstMatch(value);
var matchLengh = match?.group(0)?.length ?? 0;
if (matchLengh != 0) {
controller.text = value.replaceAll(regexp, '');
controller.selection = TextSelection.fromPosition(
TextPosition(
offset: math.min(matchLengh, selection.extent.offset),
),
);
}
},
// keyboardType and inputFormatters not realy needed in this case but maybe usefull
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
],
...
)