onChanged property

ValueChanged<String>? onChanged
final

Called when the user initiates a change to the TextField's value: when they have inserted or deleted text.

This callback doesn't run when the TextField's text is changed programmatically, via the TextField's controller. Typically it isn't necessary to be notified of such changes, since they're initiated by the app itself.

To be notified of all changes to the TextField's text, cursor, and selection, one can add a listener to its controller with TextEditingController.addListener.

onChanged is called before onSubmitted when user indicates completion of editing, such as when pressing the "done" button on the keyboard. That default behavior can be overridden. See onEditingComplete for details.

This example shows how onChanged could be used to check the TextField's current value each time the user inserts or deletes a character.

To see it in action, copy and run this code snippet on DartPad.

import 'package:flutter/material.dart';

/// Flutter code sample for [EditableText.onChanged].

void main() => runApp(const OnChangedExampleApp());

class OnChangedExampleApp extends StatelessWidget {
  const OnChangedExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(home: OnChangedExample());
  }
}

class OnChangedExample extends StatefulWidget {
  const OnChangedExample({super.key});

  @override
  State<OnChangedExample> createState() => _OnChangedExampleState();
}

class _OnChangedExampleState extends State<OnChangedExample> {
  final TextEditingController _controller = TextEditingController();

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisAlignment: .center,
        children: <Widget>[
          const Text('What number comes next in the sequence?'),
          const Text('1, 1, 2, 3, 5, 8...?'),
          TextField(
            controller: _controller,
            onChanged: (String value) async {
              if (value != '13') {
                return;
              }
              await showDialog<void>(
                context: context,
                builder: (BuildContext context) {
                  return AlertDialog(
                    title: const Text('That is correct!'),
                    content: const Text('13 is the right answer.'),
                    actions: <Widget>[
                      TextButton(
                        onPressed: () {
                          Navigator.pop(context);
                        },
                        child: const Text('OK'),
                      ),
                    ],
                  );
                },
              );
            },
          ),
        ],
      ),
    );
  }
}

See also:

Implementation

final ValueChanged<String>? onChanged;