Flutter material TextField onEditingComplete property code example

20 min read

Here's an example of how you can use the onEditingComplete property of a TextField widget in Flutter:

class MyTextFieldWidget extends StatefulWidget {
  @override
  _MyTextFieldWidgetState createState() => _MyTextFieldWidgetState();
}

class _MyTextFieldWidgetState extends State<MyTextFieldWidget> {
  final _textEditingController = TextEditingController();

  @override
  void dispose() {
    _textEditingController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return TextField(
      controller: _textEditingController,
      decoration: InputDecoration(
        hintText: 'Enter some text',
      ),
      onEditingComplete: () {
        // Do something when the user presses the enter key or submits the input
        final text = _textEditingController.text;
        print('User submitted text: $text');
      },
    );
  }
}

In this example, we have created a simple TextField widget that allows the user to enter some text. We have also defined an onEditingComplete property for the TextField, which is called when the user presses the enter key or submits the input.

Inside the onEditingComplete function, we get the text from the TextEditingController that we created earlier and print it to the console. You can replace this code with any action that you want to perform when the user submits the input.