Skip to content

Text Field backspace does not delete the last character when maxLength is specified #67895

@vinceramcesoliveros

Description

@vinceramcesoliveros

Note: I ran this in the Dartpad. there's no need to create a new flutter project.

  1. Run flutter create bug.
  2. Update the files as follows: gist.github.com/b6409e10de32b280b8938aa75364fa7b
///main.dart
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  final TextEditingController _controller = TextEditingController();
  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }
  
  @override
  void dispose(){
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextField(
            decoration: InputDecoration(
            labelText: "Type more than 9 Characters, then try to use the backspace key",
            ),
            controller: _controller,
            onChanged: (value){
              
              print(_controller.text);
            },
            maxLength: 9,
            ),
          ],
        ),
      ),
    );
  }
}
  1. After entering more than specified maximum length characters in the textfield. the backspace won't delete the last character.

Expected results:
Should Delete the last Character based on the specified maxLength property
Actual results:
Did not delete the last character.

Logs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions