diff --git a/packages/flutter/lib/src/material/text_field.dart b/packages/flutter/lib/src/material/text_field.dart index 40642369a0a..8be777d80b2 100644 --- a/packages/flutter/lib/src/material/text_field.dart +++ b/packages/flutter/lib/src/material/text_field.dart @@ -188,6 +188,71 @@ class _TextFieldSelectionGestureDetectorBuilder extends TextSelectionGestureDete /// ``` /// {@end-tool} /// +/// ## Reading values +/// +/// A common way to read a value from a TextField is to use the [onSubmitted] +/// callback. This callback is applied to the text field's current value when +/// the user finishes editing. +/// +/// {@tool dartpad --template=stateful_widget_material} +/// +/// This sample shows how to get a value from a TextField via the [onSubmitted] +/// callback. +/// +/// ```dart +/// TextEditingController _controller; +/// +/// void initState() { +/// super.initState(); +/// _controller = TextEditingController(); +/// } +/// +/// void dispose() { +/// _controller.dispose(); +/// super.dispose(); +/// } +/// +/// Widget build(BuildContext context) { +/// return Scaffold( +/// body: Center( +/// child: TextField( +/// controller: _controller, +/// onSubmitted: (String value) async { +/// await showDialog( +/// context: context, +/// builder: (BuildContext context) { +/// return AlertDialog( +/// title: const Text('Thanks!'), +/// content: Text ('You typed "$value".'), +/// actions: [ +/// FlatButton( +/// onPressed: () { Navigator.pop(context); }, +/// child: const Text('OK'), +/// ), +/// ], +/// ); +/// }, +/// ); +/// }, +/// ), +/// ), +/// ); +/// } +/// ``` +/// {@end-tool} +/// +/// For most applications the [onSubmitted] callback will be sufficient for +/// reacting to user input. +/// +/// The [onEditingComplete] callback also runs when the user finishes editing. +/// It's different from [onSubmitted] because it has a default value which +/// updates the text controller and yields the keyboard focus. Applications that +/// require different behavior can override the default [onEditingComplete] +/// callback. +/// +/// Keep in mind you can also always read the current string from a TextField's +/// [TextEditingController] using [TextEditingController.text]. +/// /// See also: /// /// * diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index d84f45b575a..279b1357553 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -761,6 +761,61 @@ class EditableText extends StatefulWidget { /// 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]. + /// + /// {@tool dartpad --template=stateful_widget_material} + /// + /// This example shows how onChanged could be used to check the TextField's + /// current value each time the user inserts or deletes a character. + /// + /// ```dart + /// TextEditingController _controller; + /// + /// void initState() { + /// super.initState(); + /// _controller = TextEditingController(); + /// } + /// + /// void dispose() { + /// _controller.dispose(); + /// super.dispose(); + /// } + /// + /// Widget build(BuildContext context) { + /// return Scaffold( + /// body: Column( + /// mainAxisAlignment: MainAxisAlignment.center, + /// children: [ + /// 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( + /// context: context, + /// builder: (BuildContext context) { + /// return AlertDialog( + /// title: const Text('Thats correct!'), + /// content: Text ('13 is the right answer.'), + /// actions: [ + /// FlatButton( + /// onPressed: () { Navigator.pop(context); }, + /// child: const Text('OK'), + /// ), + /// ], + /// ); + /// }, + /// ); + /// }, + /// ), + /// ], + /// ), + /// ); + /// } + /// ``` + /// {@end-tool} /// {@endtemplate} /// /// See also: