TextField widget of Flutter has a inbuilt method TextEditingController.clear() which is used to clear or empty the typed text inside Text Input widget. The clear() method would empty the entered string text and set its default value as empty. So in this tutorial we would Flutter Clear TextField Text Input Entered Text on Button Click Android iOS Example Tutorial.
Contents in this project Flutter Clear TextField Text Input Entered Text on Button Click Android iOS Example Tutorial:
1. Import material.dart package in your project’s main.dart file.
1 |
import 'package:flutter/material.dart'; |
2. Create void main runApp() method and we would call our default MyApp class.
1 |
void main() => runApp(MyApp()); |
3. Creating our main Root class named as MyApp extends with State less widget. Here we would call the TextFieldCustom() class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('Clear TextField Entered Text')), body: Center( child: TextFieldCustom() ) ) ); } } |
4. Creating a class named as TextFieldCustom extends with State full widget. Inside the class we would make the createState() method with TextFieldWidget class name. Using this method we would enable the state management in given class name.
1 2 3 4 5 |
class TextFieldCustom extends StatefulWidget { TextFieldWidget createState() => TextFieldWidget(); } |
5. Creating our main Child view class named as TextFieldWidget extends with State.
1 2 3 4 |
class TextFieldWidget extends State { } |
6. Creating a final type TextEditingController named as nameHolder. TextEditingController is used to create and control various operations on TextField widget like getting entered text or clearing it.
1 |
final nameHolder = TextEditingController(); |
7. Creating a function named as clearTextInput() in TextFieldWidget class. Inside this function we would call the nameHolder.clear() method to clear all the entered text from Text Input widget.
1 2 3 4 5 |
clearTextInput(){ nameHolder.clear(); } |
8. Creating a TextField widget and 1 Raised button widget in Widget build area of TextFieldWidget class. We would call the clearTextInput() function on button onPress event.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
Widget build(BuildContext context) { return Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( width: 280, padding: EdgeInsets.all(10.0), child: TextField( controller: nameHolder, autocorrect: true, decoration: InputDecoration(hintText: 'Enter Your Name Here'), ) ), RaisedButton( onPressed: clearTextInput, color: Colors.green, textColor: Colors.white, padding: EdgeInsets.fromLTRB(10, 10, 10, 10), child: Text('Click Here To Clear Text Field Entered Data'), ), ], ), )); } |
9. Complete source code for main.dart file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('Clear TextField Entered Text')), body: Center( child: TextFieldCustom() ) ) ); } } class TextFieldCustom extends StatefulWidget { TextFieldWidget createState() => TextFieldWidget(); } class TextFieldWidget extends State { final nameHolder = TextEditingController(); clearTextInput(){ nameHolder.clear(); } @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( width: 280, padding: EdgeInsets.all(10.0), child: TextField( controller: nameHolder, autocorrect: true, decoration: InputDecoration(hintText: 'Enter Your Name Here'), ) ), RaisedButton( onPressed: clearTextInput, color: Colors.green, textColor: Colors.white, padding: EdgeInsets.fromLTRB(10, 10, 10, 10), child: Text('Click Here To Clear Text Field Entered Data'), ), ], ), )); } } |
Screenshots:

Why its not working on TextFormField ??
Danish I have to check it, Will update you soon.