Handle changes to a text field

We can do in 2Ways

1.using text editing controller

2. onChanged()

Example:

import 'package:flutter/material.dart';


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


class MyApp extends StatelessWidget {

 const MyApp({Key? key}) : super(key: key);


 @override

 Widget build(BuildContext context) {

   return const MaterialApp(

     title: 'Retrieve Text Input',

     home: MyCustomForm(),

   );

 }

}


// Define a custom Form widget.

class MyCustomForm extends StatefulWidget {

 const MyCustomForm({Key? key}) : super(key: key);


 @override

 _MyCustomFormState createState() => _MyCustomFormState();

}


// Define a corresponding State class.

// This class holds data related to the Form.

class _MyCustomFormState extends State<MyCustomForm> {

 // Create a text controller and use it to retrieve the current value

 // of the TextField.

 final myController = TextEditingController();


 @override

 void initState() {

   super.initState();


   // Start listening to changes.

   myController.addListener(_printLatestValue);

 }


 @override

 void dispose() {

   // Clean up the controller when the widget is removed from the widget tree.

   // This also removes the _printLatestValue listener.

   myController.dispose();

   super.dispose();

 }


 void _printLatestValue() {

   print('Second text field: ${myController.text}');

 }


 @override

 Widget build(BuildContext context) {

   return Scaffold(

     appBar: AppBar(

       title: const Text('Retrieve Text Input'),

     ),

     body: Padding(

       padding: const EdgeInsets.all(16.0),

       child: Column(

         children: [

           TextField(

             onChanged: (text) {

               print('First text field: $text');

             },

           ),

           TextField(

             controller: myController,

           ),

         ],

       ),

     ),

   );

 }

}

Sign In or Register to comment.