Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite loop in TextFormField with onChanged callback on backspace (iOS) #69082

Closed
WinnLeong opened this issue Oct 27, 2020 · 12 comments
Closed
Labels
a: text input Entering text in a text field or keyboard related problems found in release: 1.22 Found to occur in 1.22 framework flutter/packages/flutter repository. See also f: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on platform-ios iOS applications specifically r: fixed Issue is closed as already fixed in a newer version

Comments

@WinnLeong
Copy link

WinnLeong commented Oct 27, 2020

Steps to Reproduce

I implemented the following function to format my TextFormField input. The input field goes into infinite loop when backspace is pressed where it repeatedly removes and adds the last character. This issue only occurs on iOS and works fine on Android. I did not encounter this issue prior to upgrading to the latest flutter version. This issue is similar to #13961 and #12347

complete code sample
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

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

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
  return MaterialApp(
    title: 'Test',
    theme: ThemeData(
      primarySwatch: Colors.blue,
      visualDensity: VisualDensity.adaptivePlatformDensity,
    ),
    home: InputFormatTest(title: 'Test'),
  );
}
}

class InputFormatTest extends StatefulWidget {
InputFormatTest({Key key, this.title}) : super(key: key);

final String title;

@override
_InputFormatTestState createState() => _InputFormatTestState();
}

class _InputFormatTestState extends State<InputFormatTest> {
final controller = TextEditingController();
String fieldValue = '';

@override
void initState() {
  super.initState();

  controller.addListener(controllerValue);

  setState(() {
    controller.text = '0.00';
  });
}

controllerValue() {
  controller.selection = TextSelection.fromPosition(
      TextPosition(offset: controller.text.length));

  setState(() {
    fieldValue = controller.text;
  });
}

Future<String> formatInputValue({@required controller}) async {
  final formatter = NumberFormat('#,##0.00');
  List<String> amountList = [];
  String dot = '.';
  String amount = '';

  amountList = List<String>.from(controller.text.split(''));

  amountList.removeWhere((item) => item == dot);

  if (amountList.length > 2) {
    amountList.insert(controller.text.length - 3, dot);
  }
  if (amountList.length == 2) {
    amountList.insert(0, '0');

    amountList.insert(controller.text.length - 2, dot);
  } else if (amountList[0] == '0' && amountList.length > 2) {
    amountList.remove('0');
  }

  amount = amountList.join();
  controller.text =
      formatter.format(double.tryParse(amount.replaceAll(',', '')));

  return controller.text;
}

@override
Widget build(BuildContext context) {
  return GestureDetector(
    onTap: () {
      FocusScopeNode currentFocus = FocusScope.of(context);

      if (!currentFocus.hasPrimaryFocus) {
        currentFocus.unfocus();
      }
    },
    child: Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            Container(
              margin: EdgeInsets.symmetric(horizontal: 10),
              child: TextFormField(
                cursorWidth: 0,
                controller: controller,
                keyboardType: TextInputType.numberWithOptions(decimal: true),
                onChanged: (value) async {
                  // This will be triggered indefinitely when backspace is pressed
                  if (controller.text.isEmpty) {
                    controller.text = '0.00';
                  } else {
                    var result =
                        await formatInputValue(controller: controller);

                    controller.text = result;
                  }
                },
              ),
            ),
          ],
        ),
      ), 
    ),
  );
}
}

Flutter Doctor

[✓] Flutter (Channel stable, 1.22.2, on Mac OS X 10.15.7 19H2, locale en-MY)
    • Flutter version 1.22.2 at /Users/tbsdevp/flutter
    • Framework revision 84f3d28555 (11 days ago), 2020-10-15 16:26:19 -0700
    • Engine revision b8752bbfff
    • Dart version 2.10.2

 
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at /Users/tbsdevp/Library/Android/sdk
    • Platform android-30, build-tools 30.0.2
    • Java binary at: /Applications/Android
      Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 12.0.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.0.1, Build version 12A7300
    • CocoaPods version 1.9.3

[!] Android Studio (version 4.0)
    • Android Studio at /Applications/Android Studio.app/Contents
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
    • Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)

[✓] VS Code (version 1.50.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.15.1

[✓] Connected device (1 available)
    • iPhone SE (2nd generation) (mobile) • 1236BC34-CAAA-4872-9660-D3F25C97B845
      • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-0 (simulator)
@WinnLeong WinnLeong added the team-infra Owned by Infrastructure team label Oct 27, 2020
@pedromassangocode
Copy link

Hi @WinnLeong
I need you to provide a minimal complete reproducible code sample that I can just copy/paste to reproduce the issue.
Thank you

@pedromassangocode pedromassangocode added in triage Presently being triaged by the triage team waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds and removed team-infra Owned by Infrastructure team labels Oct 27, 2020
@WinnLeong
Copy link
Author

Hi @pedromassango

Apologies for that. I have modified my code sample, just let me know if there is anything else I can provide.

@no-response no-response bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Oct 28, 2020
@pedromassangocode
Copy link

Hi @WinnLeong
I was not to reproduce the issue.

gif

Can you provide clear steps to reproduce the issue?

@pedromassangocode pedromassangocode added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Oct 28, 2020
@WinnLeong
Copy link
Author

Hi @pedromassangocode

I'm surprised you didn't get the issue! Using the exact code sample this is what I got.

loop

@no-response no-response bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Oct 28, 2020
@pedromassangocode
Copy link

Hi @WinnLeong
I just tried to reproduce the same way you did and still can't reproduce.

gif

What device/OS are you using?

@pedromassangocode pedromassangocode added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Oct 28, 2020
@WinnLeong
Copy link
Author

@pedromassangocode

Simulator Screen Shot - iPhone SE (2nd generation) - 2020-10-30 at 08 32 36

I am using iPhone SE 2nd Generation simulator with iOS 14.0

@no-response no-response bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Oct 30, 2020
@pedromassangocode
Copy link

Looks like I was not using the stable channel (1.22.2).

I can confirm that the issue reproduces on latest stable channel however it does not reproduce on latest beta, dev and master channels.

stable beta, dev, master

@pedromassangocode pedromassangocode added a: text input Entering text in a text field or keyboard related problems found in release: 1.22 Found to occur in 1.22 framework flutter/packages/flutter repository. See also f: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on platform-ios iOS applications specifically and removed in triage Presently being triaged by the triage team labels Oct 30, 2020
@xster
Copy link
Member

xster commented Oct 30, 2020

Looks like #61282 and fixed in flutter/engine#20160 which first appeared in 1.23.0-13.0.pre.

@justinmc has there been a cherry-pick discussion for #61282?

@xster
Copy link
Member

xster commented Oct 30, 2020

From #61282 (comment), it does sound like the issue has been there since 1.17.

@justinmc
Copy link
Contributor

There has not been a cherry pick discussion for it as far as I'm aware. I didn't think that it was a regression when I was working on it, are we sure that it is? I'm on board with cherry picking if it's important enough.

@LongCatIsLooong
Copy link
Contributor

Closing since this issue seems no longer producible.

@pedromassangocode pedromassangocode added the r: fixed Issue is closed as already fixed in a newer version label Mar 23, 2021
@github-actions
Copy link

github-actions bot commented Aug 4, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a: text input Entering text in a text field or keyboard related problems found in release: 1.22 Found to occur in 1.22 framework flutter/packages/flutter repository. See also f: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on platform-ios iOS applications specifically r: fixed Issue is closed as already fixed in a newer version
Projects
None yet
Development

No branches or pull requests

5 participants