-
Notifications
You must be signed in to change notification settings - Fork 28.9k
Closed
Labels
a: text inputEntering text in a text field or keyboard related problemsEntering text in a text field or keyboard related problemsfound in release: 1.22Found to occur in 1.22Found to occur in 1.22frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onplatform-iosiOS applications specificallyiOS applications specificallyr: fixedIssue is closed as already fixed in a newer versionIssue is closed as already fixed in a newer version
Description
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)
Metadata
Metadata
Assignees
Labels
a: text inputEntering text in a text field or keyboard related problemsEntering text in a text field or keyboard related problemsfound in release: 1.22Found to occur in 1.22Found to occur in 1.22frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onplatform-iosiOS applications specificallyiOS applications specificallyr: fixedIssue is closed as already fixed in a newer versionIssue is closed as already fixed in a newer version