Open In App

Flutter – Prefix and Suffix Icon in TextField

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will implement how to add prefix and Suffix icons in the TextField. A sample image is given below to get an idea about what we are going to do in this article.

Flutter - Prefix and Suffix Icon in TextField

 

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 methods and then 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 stateless widget RunMyApp Because our application does not go to change its state and then return the materialApp widget which allows us the set the title and theme and many more.

class RunMyApp extends StatelessWidget {
 const RunMyApp({super.key});

 @override
 Widget build(BuildContext context) {
   return MaterialApp(home:);
 }
}

Step 4: Give the home property and there can be a scaffold widget that has the property of AppBar and body. AppBar allows us to give the title of AppBar, color, leading, and trailing icon. Body contains the Container and has a child Text Field. We can decorate the text field using the Input Decoration, Also we can give the prefix and suffix icons in the decoration.

Scaffold(
       appBar: AppBar(
         title: Text('Prefix and Suffix Icon in TextField'),
       ),
       body: Container(
         padding: EdgeInsets.all(20.0),
         child: TextField(
           decoration: InputDecoration(
             border: UnderlineInputBorder(),
             hintText: "Email",
             labelText: "Email",
             prefixIcon:
                 IconButton(onPressed: () {}, icon: Icon(Icons.email_rounded)),
             suffixIcon: IconButton(
               icon: Icon(Icons.arrow_drop_down_circle),
               onPressed: () {},
             ),
           ),
           keyboardType: TextInputType.emailAddress,
           textInputAction: TextInputAction.done,
         ),
       ),
     ),

Final Code

Dart




import 'package:flutter/material.dart';
  
void main() {
  runApp(RunMyApp());
}
  
class RunMyApp extends StatelessWidget {
  const RunMyApp({super.key});
  
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(primarySwatch: Colors.green),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Prefix and Suffix Icon in TextField'),
        ),
        body: Container(
          padding: EdgeInsets.all(20.0),
          child: TextField(
            decoration: InputDecoration(
              border: UnderlineInputBorder(),
              hintText: "Email",
              labelText: "Email",
              prefixIcon:
                  IconButton(onPressed: () {}, icon: Icon(Icons.email_rounded)),
              suffixIcon: IconButton(
                icon: Icon(Icons.arrow_drop_down_circle),
                onPressed: () {},
              ),
            ),
            keyboardType: TextInputType.emailAddress,
            textInputAction: TextInputAction.done,
          ),
        ),
      ),
    );
  }
}


Output

Flutter - Prefix and Suffix Icon in TextField

 



Last Updated : 23 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads