Clear TextField in Flutter
Last Updated :
02 Nov, 2023
TextField and TextFormField are the two most common widgets to get input from the user. They can be used in making forms, login pages, etc. In order to make their implementation effective and accurate, we need to add certain functionalities. In this article, we’ll learn how to clear TextField on certain actions.
There can be certain scenarios in your app, where you might need clear Text Fields. Suppose, you are creating a form and after submitting you are redirected to a new page, if you go back now then the entered text would still be there and this is not a sign of a good UI. So, in this scenario, you would be required to clear the TextField after submitting.
Using a button to clear TextField:
Step 1: Create a new project and launch main.dart file. Clear the existing code.
Step 2: Import material.dart file
import 'package:flutter/material.dart';
Step 3: Create the main method which would call runApp() method and pass the name of your class to this method.
void main() => runApp(MyApp());
Step 4: Create the root class MyApp() that extends the Stateless Widget and add TextField widget as a child.
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('GeeksforGeeks'),
backgroundColor: Colors.green,
),
body: Center(
child: TextField()
)
)
);
}
}
Step 5 : Create a final variable of type TextEditingController(). This variable is used to access various properties and methods of TextField.
final fieldText = TextEditingController();
Step 6 : Create a function clearText(). You can name it accordingly. Inside this function add fieldText.clear();
As mentioned above there are various properties of TextEditingController(). One of them is clear(), and we will use it to clear the TextField.
void clearText() {
fieldText.clear();
}
Step 7: Create a button and link that to clearText() method that we created in the above step. Now, whenever you would press this button TextField would be cleared.
Example:
Dart
import 'package:flutter/material.dart' ;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
final fieldText = TextEditingController();
MyApp({Key? key}) : super(key: key);
void clearText() {
fieldText.clear();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text( 'GeeksforGeeks' ),
backgroundColor: Colors.green,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: 250,
child: TextField(
decoration: const InputDecoration(
hintText: 'Enter Something' ,
focusColor: Colors.green,
),
controller: fieldText,
),
),
const SizedBox(
height: 15,
),
ElevatedButton(
onPressed: clearText,
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.green)),
child: const Text( 'Clear' ),
),
],
),
),
),
);
}
}
|
The above code creates a TextField with a RaisedButton. Whenever you enter something in the TextField and press the button, TextField would be cleared. Below is the output of the above implementation.
Output:
It is really necessary to add this functionality because if you are pushing a new screen above a screen that contains a form and then if you go back to the form then the values would still be there.
Clearing TextField using clear icon
Also, if you want to add a clear icon in the text field itself, then we would see it also.
Below is the image of the above-mentioned scenario:

Almost all steps, in this case, would be similar to the above-mentioned steps, the only difference would be to add the clear icon instead of the Raised Button.
TextField(
decoration: InputDecoration(
hintText: 'Enter Something',
focusColor: Colors.green,
suffixIcon: IconButton( // Icon to
icon: Icon(Icons.clear), // clear text
onPressed: clearText,
),
),
controller: fieldText,
),
In the above-mentioned code, there is a TextField and suffixIcon is added to it as a decoration. The clearText method is the same as used in the above code.
Example:
Dart
import 'package:flutter/material.dart' ;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
final fieldText = TextEditingController();
MyApp({Key? key}) : super(key: key);
void clearText() {
fieldText.clear();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text( 'GeeksforGeeks' ),
backgroundColor: Colors.green,
),
body: Center(
child: SizedBox(
width: 250,
child: TextField(
decoration: InputDecoration(
hintText: 'Enter Something' ,
focusColor: Colors.green,
suffixIcon: IconButton(
icon: const Icon(Icons.clear),
onPressed: clearText,
),
),
controller: fieldText,
),
),
),
),
);
}
}
|
Output:
Similar Reads
Flutter - Gradient TextFields
Decorating text fields can be a big task if you have a large application. There is a package gradient_textfield that could make this time-consuming task pretty simple and fast. Although in Flutter we can create text fields using TextField, for beginners especially, it takes time to understand decora
3 min read
Multiline TextField in Flutter
Multiline TextField is the input TextField which takes the input in more than one line, This type of TextField is used in the scenario like taking Feedback from the user, User Comments, and Description, etc., We can achieve multiline TextField by setting the property keyBoardType and maxLines of the
2 min read
Listview.builder in Flutter
ListView is a very important widget in a flutter. It is used to create the list of children But when we want to create a list recursively without writing code again and again then ListView.builder is used instead of ListView. ListView.builder creates a scrollable, linear array of widgets. ListView.b
3 min read
Retrieve Data From TextFields in Flutter
In this article, we'll learn how to retrieve data from TextFields. TextField() widget is the most common widget used in flutter apps to take user input. We'll talk about two major methods used to extract text from TextField. Using VariablesThe TextField widget has various callback properties through
4 min read
Flutter - Card Widget
Card is a built-in widget in Flutter which derives its design from Google's Material Design Library. The functionality of this widget on screen is, that it is a bland space or panel with round corners and a slight elevation on the lower side. It comes with many properties like color, shape, shadow c
4 min read
Flutter - Dynamic Textfield Autocompletion
AutoComplete Text fields are Useful UI Components in mobile app development that provide users with real-time suggestions based on their input. Flutter, offers the easiest way to implement dynamic AutoComplete Text using the flutter_typeahead package. This package simplifies the process of creating
4 min read
Flutter - Add Clear Button to TextField Widget
Adding the Clear icon button in the TextField and using the clear the text field. A sample video is given below to get an idea about what we are going to do in this article. [video loading="lazy" mp4="https://media.geeksforgeeks.org/wp-content/uploads/20221127111541/clearbuttonintextfield-2022-11-27
3 min read
ClipRect Widget in Flutter
The ClipRect widget is used to clips its child using a rectangle. It associates with the Clippers family. The main use of clippers is to clip out any portion of the widget as required. It behaves similar to that of ClipRRect and is used to Clip a Rectangle portion of the child widget but without the
2 min read
Flutter - Implementing Calendar
Nowadays in most of the apps, we see Calendar in most of the apps for displaying birth dates or for any appointment application. Displaying the date in the app with the help of the Calendar view gives a better user experience. In this article, we are going to see how to implement Calendar in our Flu
3 min read
endDrawer Widget in Flutter
The endDrawer is the panel displayed to the side of the body (Scaffold Widget). It is generally hidden in mobile devices. We can open it by swiping in from right to left, or we can customise it to open on-press of an icon or a button. This widget is similar to the already present Drawer widget in fl
2 min read