mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
TextField docs for getting value (#40695)
Added docs examples of getting the string from a TextField
This commit is contained in:
parent
5aaac71f98
commit
431b82fda8
@ -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<void>(
|
||||
/// context: context,
|
||||
/// builder: (BuildContext context) {
|
||||
/// return AlertDialog(
|
||||
/// title: const Text('Thanks!'),
|
||||
/// content: Text ('You typed "$value".'),
|
||||
/// actions: <Widget>[
|
||||
/// 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:
|
||||
///
|
||||
/// * <https://material.io/design/components/text-fields.html>
|
||||
|
||||
@ -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: <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('Thats correct!'),
|
||||
/// content: Text ('13 is the right answer.'),
|
||||
/// actions: <Widget>[
|
||||
/// FlatButton(
|
||||
/// onPressed: () { Navigator.pop(context); },
|
||||
/// child: const Text('OK'),
|
||||
/// ),
|
||||
/// ],
|
||||
/// );
|
||||
/// },
|
||||
/// );
|
||||
/// },
|
||||
/// ),
|
||||
/// ],
|
||||
/// ),
|
||||
/// );
|
||||
/// }
|
||||
/// ```
|
||||
/// {@end-tool}
|
||||
/// {@endtemplate}
|
||||
///
|
||||
/// See also:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user