这个章节讲解的是如何获取文本框的输入值。

步骤

  • 创建一个 TextEditingController

  • TextEditingController 应用到 TextField

  • 展示文本框当前值

1. 创建 TextEditingController

为了获取文本框输入值,需要创建一个 TextEditingController。在后续步骤中,这个 TextEditingController 将会被应用到 TextField 上。

TextEditingController 被应用于 TextField 或者 TextFormField 后,就可以使用它来获取文本框输入值。

记住:当不再使用 TextEditingController 时,请销毁它以确保相关的资源得到释放。

  1. // Define a Custom Form Widget
  2. class MyCustomForm extends StatefulWidget {
  3. @override
  4. _MyCustomFormState createState() => _MyCustomFormState();
  5. }
  6. // Define a corresponding State class. This class will hold the data related to
  7. // our Form.
  8. class _MyCustomFormState extends State<MyCustomForm> {
  9. // Create a text controller. We will use it to retrieve the current value
  10. // of the TextField!
  11. final myController = TextEditingController();
  12. @override
  13. void dispose() {
  14. // Clean up the controller when the Widget is disposed
  15. myController.dispose();
  16. super.dispose();
  17. }
  18. @override
  19. Widget build(BuildContext context) {
  20. // We will fill this out in the next step!
  21. }
  22. }

2. 把 TextEditingController 应用到 TextField 上

创建完 TextEditingController,就可以把它作为 TextField 或者 TextFormFieldcontroller 属性完成绑定。

  1. TextField(
  2. controller: myController,
  3. );

3. 展示文本框当前值

TextEditingController 作用于文本框后,就可以开始取值了。通过 TextEditingController 提供的 text 方法,就能够获取到文本框输入值了。

在下面的示例中,用户点击浮层按钮,将会触发弹出一个对话框,对话框获取并显示文本框的当前值。

  1. FloatingActionButton(
  2. // When the user presses the button, show an alert dialog with the
  3. // text the user has typed into our text field.
  4. onPressed: () {
  5. return showDialog(
  6. context: context,
  7. builder: (context) {
  8. return AlertDialog(
  9. // Retrieve the text the user has typed in using our
  10. // TextEditingController
  11. content: Text(myController.text),
  12. );
  13. },
  14. );
  15. },
  16. tooltip: 'Show me the value!',
  17. child: Icon(Icons.text_fields),
  18. );

完整示例

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MyApp());
  3. class MyApp extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return MaterialApp(
  7. title: 'Retrieve Text Input',
  8. home: MyCustomForm(),
  9. );
  10. }
  11. }
  12. // Define a Custom Form Widget
  13. class MyCustomForm extends StatefulWidget {
  14. @override
  15. _MyCustomFormState createState() => _MyCustomFormState();
  16. }
  17. // Define a corresponding State class. This class will hold the data related to
  18. // our Form.
  19. class _MyCustomFormState extends State<MyCustomForm> {
  20. // Create a text controller. We will use it to retrieve the current value
  21. // of the TextField!
  22. final myController = TextEditingController();
  23. @override
  24. void dispose() {
  25. // Clean up the controller when the Widget is disposed
  26. myController.dispose();
  27. super.dispose();
  28. }
  29. @override
  30. Widget build(BuildContext context) {
  31. return Scaffold(
  32. appBar: AppBar(
  33. title: Text('Retrieve Text Input'),
  34. ),
  35. body: Padding(
  36. padding: const EdgeInsets.all(16.0),
  37. child: TextField(
  38. controller: myController,
  39. ),
  40. ),
  41. floatingActionButton: FloatingActionButton(
  42. // When the user presses the button, show an alert dialog with the
  43. // text the user has typed into our text field.
  44. onPressed: () {
  45. return showDialog(
  46. context: context,
  47. builder: (context) {
  48. return AlertDialog(
  49. // Retrieve the text the user has typed in using our
  50. // TextEditingController
  51. content: Text(myController.text),
  52. );
  53. },
  54. );
  55. },
  56. tooltip: 'Show me the value!',
  57. child: Icon(Icons.text_fields),
  58. ),
  59. );
  60. }
  61. }

获取文本输入示例