flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
164.33k stars 27.12k forks source link

TextField onChange method inconsistent when characters are spammed #116661

Open ChunKaiC opened 1 year ago

ChunKaiC commented 1 year ago

What the code sample does

I am trying to build a text editor on top of the TextField. The code sample format's the text within a text field, adding a line break at every 10th character.

Steps to Reproduce

  1. Execute flutter run on the code sample
  2. Simultaneously press multiple keys on the keyboard just before a line break occurs

Expected results: Text formatted correctly with a line break at every 10th character.

Actual results: Sometimes the formatting method within onChange does not apply correctly, meaning a line can exceed 10 characters before a line break. Occasionally some characters typed are deleted.

Code sample ```import 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData(primarySwatch: Colors.blue), home: const TestEditor(), ); } } class TestEditor extends StatefulWidget { const TestEditor({super.key}); @override State createState() => _TestEditorState(); } class _TestEditorState extends State { final contoller = TextEditingController(); void onChange(String text) { // Method 1 if (text.length % 10 == 0 && text.isNotEmpty) { final selection = contoller.selection.copyWith(); contoller.text += '\n'; contoller.selection = selection.copyWith( baseOffset: selection.baseOffset + 1, extentOffset: selection.extentOffset + 1, ); } } @override Widget build(BuildContext context) { return Scaffold( body: SafeArea( child: SizedBox( width: MediaQuery.of(context).size.width, height: MediaQuery.of(context).size.height, child: TextField( style: GoogleFonts.robotoMono(), cursorWidth: 2, controller: contoller, onChanged: onChange, showCursor: true, smartQuotesType: SmartQuotesType.disabled, smartDashesType: SmartDashesType.disabled, enableSuggestions: false, autocorrect: false, maxLines: 500, ), ), ), ); } } ```
Logs ``` N/A ``` ``` Analyzing bug_example... No issues found! (ran in 1.1s) ``` ``` [✓] Flutter (Channel stable, 3.3.6, on macOS 13.0 22A380 darwin-arm, locale en-CA) • Flutter version 3.3.6 on channel stable at /Users/chunkaic/Developement/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 6928314d50 (6 weeks ago), 2022-10-25 16:34:41 -0400 • Engine revision 3ad69d7be3 • Dart version 2.18.2 • DevTools version 2.15.0 [✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1) • Android SDK at /Users/chunkaic/Library/Android/sdk • Platform android-32, build-tools 32.1.0-rc1 • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 14.0.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 14A400 • CocoaPods version 1.11.3 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2021.2) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840) [✓] VS Code (version 1.72.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.52.0 [✓] Connected device (3 available) • iPhone 14 Pro (mobile) • A0687B48-A2A3-4A34-991D-A88F4F0ABF47 • ios • com.apple.CoreSimulator.SimRuntime.iOS-16-0 (simulator) • macOS (desktop) • macos • darwin-arm64 • macOS 13.0 22A380 darwin-arm • Chrome (web) • chrome • web-javascript • Google Chrome 108.0.5359.94 [✓] HTTP Host Availability • All required HTTP hosts are available • No issues found! ```
darshankawar commented 1 year ago

Thanks for the report @ChunKaiC After running your code and inputing random text in the textfield, I see that first line gets 10 chars but other lines shows 9 chars as below:

Screenshot 2022-12-08 at 1 11 11 PM

Is that the behavior you are seeing and reporting ? Or you see another different result ?

ChunKaiC commented 1 year ago

Yes, my mistake, the screen shot you sent is the correct behaviour. The problem I'm currently experiencing is that sometimes when you spam right before a line break is about to be inserted this happens:

image (This was done by pressing 'adsjkl' on the keyboard simultaneously)

No line break was inserted. Typing another character causes some characters to be deleted before being formatted:

image (This was done by pressing 'd' after the previous screen shot)

This was found on the iOS simulator, however the problem seems to occur on iOS release mode as well.

darshankawar commented 1 year ago

Thanks for the update. Based on my findings earlier and above report, keeping this issue open for team's attention.

stable, master flutter doctor -v ``` [✓] Flutter (Channel stable, 3.3.9, on macOS 12.2.1 21D62 darwin-x64, locale en-GB) • Flutter version 3.3.9 on channel stable at /Users/dhs/documents/fluttersdk/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision b8f7f1f986 (12 days ago), 2022-11-23 06:43:51 +0900 • Engine revision 8f2221fbef • Dart version 2.18.5 • DevTools version 2.15.0 [!] Xcode - develop for iOS and macOS (Xcode 12.3) • Xcode at /Applications/Xcode.app/Contents/Developer ! Flutter recommends a minimum Xcode version of 13. Download the latest version or update via the Mac App Store. • CocoaPods version 1.11.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] VS Code (version 1.62.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.21.0 [✓] Connected device (5 available) • SM G975F (mobile) • RZ8M802WY0X • android-arm64 • Android 11 (API 30) • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios • iOS 14.4.1 18D61 • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729 • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator) • macOS (desktop) • macos • darwin-x64 • Mac OS X 10.15.4 19E2269 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 98.0.4758.80 [✓] HTTP Host Availability • All required HTTP hosts are available ! Doctor found issues in 1 category. [!] Flutter (Channel master, 3.7.0-4.0.pre.48, on macOS 12.2.1 21D62 darwin-x64, locale en-GB) • Flutter version 3.7.0-4.0.pre.48 on channel master at /Users/dhs/documents/fluttersdk/flutter ! Warning: `flutter` on your path resolves to /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter. Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path. ! Warning: `dart` on your path resolves to /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter. Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path. • Upstream repository https://github.com/flutter/flutter.git • Framework revision eefbe85c8b (19 minutes ago), 2022-12-08 22:32:07 -0600 • Engine revision 8d83b98c55 • Dart version 3.0.0 (build 3.0.0-0.0.dev) • DevTools version 2.20.0 • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades. [!] Xcode - develop for iOS and macOS (Xcode 12.3) • Xcode at /Applications/Xcode.app/Contents/Developer ! Flutter recommends a minimum Xcode version of 13. Download the latest version or update via the Mac App Store. • CocoaPods version 1.11.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] VS Code (version 1.62.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.21.0 [✓] Connected device (5 available) • SM G975F (mobile) • RZ8M802WY0X • android-arm64 • Android 11 (API 30) • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios • iOS 14.4.1 18D61 • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729 • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator) • macOS (desktop) • macos • darwin-x64 • Mac OS X 10.15.4 19E2269 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 98.0.4758.80 [✓] HTTP Host Availability • All required HTTP hosts are available ! Doctor found issues in 1 category. ```
justinmc commented 1 year ago

CC @LongCatIsLooong