Home > Software engineering >  Custom text input formatting on TextField not working
Custom text input formatting on TextField not working

Time:10-10

Any sort of custom TextInputFormatter is not working. Whenever I specify one, the TextField stops taking input.

class LowerCaseFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(
      TextEditingValue oldValue, TextEditingValue newValue) {
    return oldValue;
  }
}
@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          children: [
            TextField(
              controller: _inputController,
              inputFormatters: [
                LowerCaseFormatter()
              ],
            ),
            Text(_inputController.text),
          ],
        ),
      ),
    );
  }

CodePudding user response:

class CustomFormatter extends TextInputFormatter {
  CustomFormatter();

  @override
  TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
    return TextEditingValue(text: newValue.text.toLowerCase(), selection: newValue.selection);
  }
}
  • Related