TextField widget of Flutter has its own prefixIcon property which is used to automatically set Image Icon at starting of Text Input widget. Icon properties is automatically set by flutter library and there is no need to add padding or margin to the icon. By default the Icon has 48 Pixels wide in widget. In our current tutorial we are using Flutter’s own Icon library to import the icon because flutter already gives us hundred of PNG and SVG icons. So in this tutorial we would Flutter Add Image Icon Inside TextField Text Input Android iOS Example.
Contents in this project Flutter Add Image Icon Inside TextField Text Input Android iOS Example Tutorial:
1. Import material.dart package in your app’s main.dart file.
1 |
import 'package:flutter/material.dart'; |
2. Create our void main runApp() method and here we would call our main MyApp class.
1 |
void main() => runApp(MyApp()); |
3. Create our main MyApp class extends with State less widget.
1 2 3 4 |
class MyApp extends StatelessWidget { } |
4. Create Widget Build area in MyApp class and now we would make Scaffold widget and inside the Scaffold widget body area we would define the Center widget.
1 2 3 4 5 6 7 8 |
Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child : ) ) ); } |
5. Now we would make a Container widget and Root parent and as Child we would create the TextField widget. We are using prefixIcon: Icon(Icons.email) to set the Email icon from Icon library. You can call here your own icon as per your requirement.
- width : Width of TextField Text Input widget on screen.
- padding : Used to set padding.
- autocorrect : To enable Auto correction in TextField widget.
- hintText : Used to set the Hint text inside TextField widget.
- prefixIcon : Used to set the Icon at starting of TextField widget.
- enabledBorder : To set default border style around TextField widget.
- focusedBorder : To change the border style on TextField selection.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Container( width: 320, padding: EdgeInsets.all(10.0), child: TextField( autocorrect: true, decoration: InputDecoration( hintText: 'Enter Your Email Here...', prefixIcon: Icon(Icons.email), hintStyle: TextStyle(color: Colors.grey), filled: true, fillColor: Colors.white70, enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.all(Radius.circular(12.0)), borderSide: BorderSide(color: Colors.green, width: 2), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.all(Radius.circular(10.0)), borderSide: BorderSide(color: Colors.green, width: 2), ), ),) ) |
6. 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 |
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child : Container( width: 320, padding: EdgeInsets.all(10.0), child: TextField( autocorrect: true, decoration: InputDecoration( hintText: 'Enter Your Email Here...', prefixIcon: Icon(Icons.email), hintStyle: TextStyle(color: Colors.grey), filled: true, fillColor: Colors.white70, enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.all(Radius.circular(12.0)), borderSide: BorderSide(color: Colors.green, width: 2), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.all(Radius.circular(10.0)), borderSide: BorderSide(color: Colors.green, width: 2), ), ),) )) ) ); } } |
Screenshots:
