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
166.32k stars 27.53k forks source link

Quickly entering text in a textfield with a slow TextInputFormatter may ignore keypresses #37390

Open goderbauer opened 5 years ago

goderbauer commented 5 years ago
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

// Identity formatter, with some slowness.
class SimpleTextFormatter extends TextInputFormatter {
  SimpleTextFormatter();

  static final instance = SimpleTextFormatter();

  @override
  TextEditingValue formatEditUpdate(
    TextEditingValue oldValue,
    TextEditingValue newValue,
  ) {
    var result = TextEditingValue(text: newValue.text.replaceAll('2', ''), selection: newValue.selection);

    // Some slow calculation!
    int sum = 0;
    for (int i = 0; i < 1000000000; ++i) sum += i;
    if (sum % 2 == 1) return null;

    return result;
  }
}

void main() => runApp(TextFieldTest());

class TextFieldTest extends StatefulWidget {
  @override
  _TextFieldTestState createState() => _TextFieldTestState();
}

class _TextFieldTestState extends State<TextFieldTest> {
  final _controller = TextEditingController();
  final _controller2 = TextEditingController();
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Text Editing Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Text Editing Demo'),
        ),
        body: Column(
          children: <Widget>[
            SizedBox(
              width: 100,
              child: TextField(
                controller: _controller,
                inputFormatters: [SimpleTextFormatter.instance],
              ),
            ),
            SizedBox(
              width: 100,
              child: TextField(
                controller: _controller2,
//                inputFormatters: [SimpleTextFormatter.instance],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

In the upper textfield quickly enter a sequence like "2266226622662266".

Expected behavior: the 2's get filtered out and all 6's end up in the text field. Observed behavior: Some 6s are omitted as well.

goderbauer commented 5 years ago

I suspect that the result send back from the TextInputFormatter to the engine overwrites not-yet-processed user inputs, which are lost then.

Guillaume-Fortin commented 4 years ago

same problems with zebran enterprise keyboard

darshankawar commented 4 years ago

I am unable to verify this issue on latest master. Tried the input sequence as suggested in original description, but don't see the text being input in the textfield. The screen becomes unresponsive and finally quits by itself. Unsure how to verify this if it still exists.

37390.mp4.zip 37390