Skip to content

newValue property of formatEditUpdate function into TextInputFormatter class is not as expected #67262

@giandifra

Description

@giandifra

Steps to Reproduce

  1. Run this code or simply try flutter_gallery example.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 20.0),
            child: TextField(
              inputFormatters: [DecimalTextInputFormatter(decimalRange: 2),],
            ),
          ),
        ),
      ),
    );
  }
}

class DecimalTextInputFormatter extends TextInputFormatter {
  DecimalTextInputFormatter({int decimalRange})
      : assert(decimalRange == null || decimalRange >= 0,
            'DecimalTextInputFormatter declaretion error') {
        
    String dp = (decimalRange != null && decimalRange > 0)
        ? "([.][0-9]{0,$decimalRange}){0,1}"
        : "";
    String num = "[0-9]*$dp";

    _exp = new RegExp("^($num){0,1}\$");
  }

  RegExp _exp;

  @override
  TextEditingValue formatEditUpdate(
    TextEditingValue oldValue,
    TextEditingValue newValue,
  ) {
    print('newValue: $newValue');
    print('oldValue: $newValue');
    
    if (_exp.hasMatch(newValue.text)) {
      return newValue;
    }
    return oldValue;
  }
}
  1. Type "1.." in TextField
  2. The TextField show "1." because "1.." not respect the right pattern
  3. If you try to delete one char nothing happens because the newValue in formatEditUpdate return "1.." and the delete action remove the last point so the user doesn't see any changes.

Antoher example

  1. Run the code..
  2. Type "1.."
  3. TextField show "1."
  4. Type "."
  5. TextField show "1."
  6. Type "4"
  7. TextField show "1." , it should show "1.4" but newValue of formatEditUpdate has "1..4" and not respect the pattern

Expected results:
newValue of formatEditUpdate should be the last value returned from formatEditUpdate function.

Actual results:
newValue of formatEditUpdate function retain all char inserted so formatEditUpdate process newValue that is different from that TextField is showing.

Flutter Doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 1.22.0, on Mac OS X 10.15.6 19G2021, locale it-IT)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 11.5)
[✓] Android Studio (version 4.0)
[!] IntelliJ IDEA Ultimate Edition (version 2019.3.3)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
[✓] VS Code (version 1.49.2)
[✓] Connected device (1 available)

! Doctor found issues in 1 category.

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: text inputEntering text in a text field or keyboard related problemsf: material designflutter/packages/flutter/material repository.found in release: 1.23Found to occur in 1.23frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions