Flutter Icons

 


Flutter Icons

An icon is a graphic image representing an application or any specific entity containing meaning for the user. It can be selectable and non-selectable. For example, the company's logo is non-selectable. Sometimes it also contains a hyperlink to go to another page. It also acts as a sign in place of a detailed explanation of the actual entity.

Flutter provides an Icon Widget to create icons in our applications. We can create icons in Flutter, either using inbuilt icons or with the custom icons. Flutter provides the list of all icons in the Icons class. In this article, we are going to learn how to use Flutter icons in the application.

Icon Widget Properties

Flutter icons widget has different properties for customizing the icons. These properties are explained below:

PropertyDescriptions
iconIt is used to specify the icon name to display in the application. Generally, Flutter uses material design icons that are symbols for common actions and items.
colorIt is used to specify the color of the icon.
sizeIt is used to specify the size of the icon in pixels. Usually, icons have equal height and width.
textDirectionIt is used to specify to which direction the icon will be rendered.

Let us understand Flutter icons using different examples.

Example 1:

In this example, we will see the basic icon widget that has default values. First, create a project in the IDE, navigate to the lib folder, and then open the main.dart file. Now, replace the below code in the main.dart file:

  1. import 'package:flutter/material.dart';  
  2.   
  3. void main() => runApp(MyApp());  
  4.   
  5. class MyApp extends StatelessWidget {  
  6.   // This widget is the root of your application.  
  7.   @override  
  8.   Widget build(BuildContext context) {  
  9.     return MaterialApp(  
  10.       theme: ThemeData(  
  11.         primarySwatch: Colors.blue,  
  12.       ),  
  13.       home: MyIconPage(),  
  14.     );  
  15.   }  
  16. }  
  17.   
  18. class MyIconPage extends StatefulWidget {  
  19.   @override  
  20.   _MyIconPageState createState() => _MyIconPageState();  
  21. }  
  22.   
  23. class _MyIconPageState extends State<MyIconPage> {  
  24.   @override  
  25.   Widget build(BuildContext context) {  
  26.     return Scaffold(  
  27.       appBar: AppBar(  
  28.         title: Text('Flutter Icon Tutorial'),  
  29.       ),  
  30.       body: Row(  
  31.         mainAxisAlignment: MainAxisAlignment.spaceAround,  
  32.           children: <Widget>[  
  33.             Icon(Icons.camera_enhance),  
  34.             Icon(Icons.camera_front),  
  35.             Icon(Icons.camera_rear),  
  36.       ]),  
  37.     );  
  38.   }  
  39. }  

Output:

When we run this project, it will show the UI similar to the following screenshot in the emulator or device we are using:

Flutter Icons

Example 2:

In this example, we will see how to customize the icons. Here, we will use the size attribute to adjust the icon size according to our needs. We will also see the color property to change the icon default color. So, open the main.dart file and replace it with the below code:

  1. import 'package:flutter/material.dart';  
  2.   
  3. void main() => runApp(MyApp());  
  4.   
  5. class MyApp extends StatelessWidget {  
  6.   // This widget is the root of your application.  
  7.   @override  
  8.   Widget build(BuildContext context) {  
  9.     return MaterialApp(  
  10.       theme: ThemeData(  
  11.         primarySwatch: Colors.blue,  
  12.       ),  
  13.       home: MyIconPage(),  
  14.     );  
  15.   }  
  16. }  
  17.   
  18. class MyIconPage extends StatefulWidget {  
  19.   @override  
  20.   _MyIconPageState createState() => _MyIconPageState();  
  21. }  
  22.   
  23. class _MyIconPageState extends State<MyIconPage> {  
  24.   @override  
  25.   Widget build(BuildContext context) {  
  26.     return Scaffold(  
  27.       appBar: AppBar(  
  28.         title: Text('Flutter Icon Tutorial'),  
  29.       ),  
  30.       body: Row(  
  31.         mainAxisAlignment: MainAxisAlignment.spaceAround,  
  32.           children: <Widget>[  
  33.             Icon(  
  34.               Icons.camera_enhance,  
  35.               size: 70,  
  36.               color:Colors.green  
  37.             ),  
  38.             Icon(  
  39.               Icons.camera_front,  
  40.               size: 70,  
  41.               color:Colors.orange  
  42.             ),  
  43.             Icon(  
  44.               Icons.camera_rear,  
  45.               size: 70,  
  46.               color:Colors.black  
  47.             ),  
  48.       ]),  
  49.     );  
  50.   }  
  51. }  

Output:

When we run this project, it will show the UI similar to the following screenshot in the emulator or device we are using:

Flutter Icons

Example 3:

Most of the time, we have seen that the icons always contains a text below it in our application. In this example, we will see how to use the Text widget with the Icons widget. So, open the main.dart file and replace it with the below code:

  1. import 'package:flutter/material.dart';  
  2.   
  3. void main() => runApp(MyApp());  
  4.   
  5. class MyApp extends StatelessWidget {  
  6.   @override  
  7.   Widget build(BuildContext context) {  
  8.     return MaterialApp(  
  9.       theme: ThemeData(  
  10.         primarySwatch: Colors.blue,  
  11.       ),  
  12.       home: MyIconPage(),  
  13.     );  
  14.   }  
  15. }  
  16.   
  17. class MyIconPage extends StatefulWidget {  
  18.   @override  
  19.   _MyIconPageState createState() => _MyIconPageState();  
  20. }  
  21.   
  22. class _MyIconPageState extends State<MyIconPage> {  
  23.   @override  
  24.   Widget build(BuildContext context) {  
  25.     return Scaffold(  
  26.       appBar: AppBar(  
  27.         title: Text('Flutter Icon Tutorial'),  
  28.       ),  
  29.       body: Column(children: <Widget>[  
  30.         //icon with label below it  
  31.         Container(  
  32.           padding: EdgeInsets.all(30),  
  33.           child: Row(  
  34.               mainAxisAlignment: MainAxisAlignment.spaceAround,  
  35.               children: <Widget>[  
  36.                 Column(children: <Widget>[  
  37.                   Icon(  
  38.                     Icons.camera_front,  
  39.                     size: 70  
  40.                   ),  
  41.                   Text('Front Camera'),  
  42.                 ]),  
  43.                 Column(children: <Widget>[  
  44.                   Icon(  
  45.                       Icons.camera_enhance,  
  46.                       size: 70  
  47.                   ),  
  48.                   Text('Camera'),  
  49.                 ]),  
  50.                 Column(children: <Widget>[  
  51.                   Icon(  
  52.                       Icons.camera_rear,  
  53.                       size: 70  
  54.                   ),  
  55.                   Text('Rear Camera'),  
  56.                 ]),  
  57.               ]  
  58.             ),  
  59.           )  
  60.         ],  
  61.       )  
  62.     );  
  63.   }  
  64. }  

Output:

When we run this project, it will show the UI similar to the following screenshot in the emulator or device we are using:

Flutter Icons

Post a Comment

Previous Post Next Post