Flutter - Add Clear Button to TextField Widget
Last Updated :
28 Apr, 2025
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.
How to use?
Using the Text Editing Controller we can clear the text field using the Clear parameter.
Text Editing Controller:
final TextEditingController _controller = new TextEditingController();
Clear the Text Field:
_controller.clear
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Add the Material package that gives us the important method to use and call the runApp method in the main function that will call our application.
import 'package:flutter/material.dart';
void main() {
runApp(RunMyApp());
}
Step 3: Now we have to make a Stateful widget RunMyApp Because our application does change its state and then return the MaterialApp widget which allows us the set the title and theme and has many more properties. Also, create the TextEditingController.
class RunMyApp extends StatefulWidget {
const RunMyApp({super.key});
@override
State<RunMyApp> createState() => _RunMyAppState();
}
class _RunMyAppState extends State<RunMyApp> {
final TextEditingController _controller = new TextEditingController();
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.green),
home: Scaffold(),
);
}
}
Step 4: Give the home property and there can be a scaffold widget that has the property of AppBar and body. AppBar has the title property to set the title of the application. Body contains a Center widget that has Text Field as a child. Text Field has the following property.
- controller - It takes the Text editing controller
- decoration - It takes the Input Decoration and has many parameters, We use the suffix Icon button that further takes the Icon Button, on the Pressed parameter used to clear the text field and have the clear icon.
Center(
child: Padding(
padding: const EdgeInsets.all(30.0),
child: TextField(
controller: _controller,
decoration: InputDecoration(
hintText: 'Enter a message',
suffixIcon: IconButton(
onPressed: _controller.clear,
icon: Icon(Icons.clear),
),
),
),
),
),
),
Final Code
Dart
import 'package:flutter/material.dart';
void main() {
runApp(RunMyApp());
}
class RunMyApp extends StatefulWidget {
const RunMyApp({super.key});
@override
State<RunMyApp> createState() => _RunMyAppState();
}
class _RunMyAppState extends State<RunMyApp> {
final TextEditingController _controller = new TextEditingController();
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.green),
home: Scaffold(
appBar: AppBar(
title: Text('Clear Button in Text Field'),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(30.0),
child: TextField(
controller: _controller,
decoration: InputDecoration(
hintText: 'Enter a message',
suffixIcon: IconButton(
onPressed: _controller.clear,
icon: Icon(Icons.clear),
),
),
),
),
),
),
);
}
}
Output
Similar Reads
Flutter Material Widget - Outlined Button Class Outlined Widgets are material design components that are used to give outlines to buttons. It is in no way different from text buttons except for the special feature of the border this class provides. These contain nonprimary actions for the apps. It is introduced in version 1.22 of flutter. Outline
4 min read
FlatButton Widget in Flutter FlatButton is the material design widget in a flutter. It is a text label material widget that performs an action when the button is tapped. Let's understand with the help of examples. Disclaimer: As of May 2021 the FlatButton class in flutter is deprecated. TextButton class should be used instead.
3 min read
Raised Button widget in Flutter RaisedButton is the material design button based on a Material widget that elevates when pressed upon in flutter. It is one of the most widely used buttons in the flutter library. Let's understand this button with the help of an example. Disclaimer: As of May 2021 the RaisedButton class in flutter i
5 min read
Flutter - Material Banner Widget In this article, we will learn about a new feature called Material Banner which is a new feature in flutter and got released in flutter version 2.5.0. What is Material Banner? A banner displays an important message and asks the user to perform some action. Banner is displayed at top of the screen be
4 min read
Flutter - Change Text When Button Pressed In this article, you will see how you can change the text in Flutter on a single screen after clicking the button. Let's start so you first start your vs code or android studio and you just delete the whole code and paste the code which we have given in below and run it then you can get the solution
4 min read
Flutter - DropDownButton Widget In this article, we will learn how to use a DropDownButton and learn various properties of it in flutter. We will use the Flutter DropDownButton widget to display a dropdown list in our application. So first letâs see what is DropDownButton.DropDownButton Widget FlutterIn Flutter, A DropDownButton i
3 min read
Flutter - ElevatedButton Widget Elevated Button is a Flutter component included inside the material package, i.e., "package:flutter/material.dart". The main characteristic of these buttons is the slight elevation in their surface towards the screen when tapped by the user. In simple language, elevated buttons are un-deprecated rai
5 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
How to Disable a Button in Flutter? Sometimes we need to make the button disabled, let us consider, you created a form to take the user input and a form submit button. Then you want that, the button must be disabled until the user fills all the fields of the form then immediately enable the button. Sample Output is given below what we
3 min read
Flutter - Working with Material Button Buttons are material components that provide the user a single tap facility for taking actions, making choices, submitting forms, saving data, opening a new page, etc. Buttons are triggered when the user taps on them. They are the most commonly used feature provided in almost all Flutter apps. For u
4 min read