Flutter Widgets

 

In this section, we are going to learn the concept of a widget, how to create it, and their different types available in the Flutter framework. We have learned earlier that everything in Flutter is a widget.

If you are familiar with React or Vue.js, then it is easy to understand the Flutter.

Whenever you are going to code for building anything in Flutter, it will be inside a widget. The central purpose is to build the app out of widgets. It describes how your app view should look like with their current configuration and state. When you made any alteration in the code, the widget rebuilds its description by calculating the difference of previous and current widget to determine the minimal changes for rendering in UI of the app.

Widgets are nested with each other to build the app. It means the root of your app is itself a widget, and all the way down is a widget also. For example, a widget can display something, can define design, can handle interaction, etc.

The below image is a simple visual representation of the widget tree.

Flutter Widgets

We can create the Flutter widget like this:

  1. Class ImageWidget extends StatelessWidget {  
  2.          // Class Stuff  
  3. }  

Hello World Example

  1. import 'package:flutter/material.dart';  
  2.   
  3. class MyHomePage extends StatelessWidget {    
  4.   MyHomePage({Key key, this.title}) : super(key: key);    
  5.   // This widget is the home page of your application.    
  6.   final String title;    
  7.     
  8.   @override    
  9.   Widget build(BuildContext context) {    
  10.     return Scaffold(    
  11.       appBar: AppBar(    
  12.         title: Text(this.title),    
  13.       ),    
  14.       body: Center(    
  15.         child: Text('Hello World'),    
  16.       ),    
  17.     );    
  18.   }    
  19. }    

Types of Widget

We can split the Flutter widget into two categories:

  1. Visible (Output and Input)
  2. Invisible (Layout and Control)

Visible widget

The visible widgets are related to the user input and output data. Some of the important types of this widget are:

Text

A Text widget holds some text to display on the screen. We can align the text widget by using textAlign property, and style property allow the customization of Text that includes font, font weight, font style, letter spacing, color, and many more. We can use it as like below code snippets.

  1. new Text(     
  2. 'Hello, Javatpoint!',     
  3. textAlign: TextAlign.center,       
  4. style: new TextStyle(fontWeight: FontWeight.bold),   
  5. )  

Button

This widget allows you to perform some action on click. Flutter does not allow you to use the Button widget directly; instead, it uses a type of buttons like a FlatButton and a RaisedButton. We can use it as like below code snippets.

  1. //FlatButton Example  
  2. new FlatButton(  
  3.   child: Text("Click here"),  
  4.   onPressed: () {  
  5.     // Do something here  
  6.   },  
  7. ),  
  8.   
  9. //RaisedButton Example  
  10. new RaisedButton(  
  11.   child: Text("Click here"),  
  12.   elevation: 5.0,  
  13.   onPressed: () {  
  14.     // Do something here  
  15.   },  
  16. ),  

In the above example, the onPressed property allows us to perform an action when you click the button, and elevation property is used to change how much it stands out.

Image

This widget holds the image which can fetch it from multiple sources like from the asset folder or directly from the URL. It provides many constructors for loading image, which are given below:

  • Image: It is a generic image loader, which is used by ImageProvider.
  • asset: It load image from your project asset folder.
  • file: It loads images from the system folder.
  • memory: It load image from memory.
  • network: It loads images from the network.

To add an image in the project, you need first to create an assets folder where you keep your images and then add the below line in pubspec.yaml file.

  1. assets:  
  2.   - assets/  

Now, add the following line in the dart file.

  1. Image.asset('assets/computer.png')  

The complete source code for adding an image is shown below in the hello world example.

  1. class MyHomePage extends StatelessWidget {  
  2.   MyHomePage({Key key, this.title}) : super(key: key);  
  3.   // This widget is the home page of your application.  
  4.   final String title;  
  5.   
  6.   @override  
  7.   Widget build(BuildContext context) {  
  8.     return Scaffold(  
  9.       appBar: AppBar(  
  10.         title: Text(this.title),  
  11.       ),  
  12.       body: Center(  
  13.         child: Image.asset('assets/computer.png'),  
  14.       ),  
  15.     );  
  16.   }  
  17. }  

When you run the app, it will give the following output.

Flutter Widgets

Icon

This widget acts as a container for storing the Icon in the Flutter. The following code explains it more clearly.

  1. new Icon(  
  2.   Icons.add,  
  3.   size: 34.0,  
  4. )  

Invisible widget

The invisible widgets are related to the layout and control of widgets. It provides controlling how the widgets actually behave and how they will look onto the screen. Some of the important types of these widgets are:

Column

A column widget is a type of widget that arranges all its children's widgets in a vertical alignment. It provides spacing between the widgets by using the mainAxisAlignment and crossAxisAlignment properties. In these properties, the main axis is the vertical axis, and the cross axis is the horizontal axis.

Example

The below code snippets construct two widget elements vertically.

  1. new Column(  
  2.   mainAxisAlignment: MainAxisAlignment.center,  
  3.   children: <Widget>[  
  4.     new Text(  
  5.       "VegElement",  
  6.     ),  
  7.     new Text(  
  8.       "Non-vegElement"  
  9.     ),  
  10.   ],  
  11. ),  

Row

The row widget is similar to the column widget, but it constructs a widget horizontally rather than vertically. Here, the main axis is the horizontal axis, and the cross axis is the vertical axis.

Example

The below code snippets construct two widget elements horizontally.

  1. new Row(  
  2.   mainAxisAlignment: MainAxisAlignment.spaceEvenly,  
  3.   children: <Widget>[  
  4.     new Text(  
  5.       "VegElement",  
  6.     ),  
  7.     new Text(  
  8.       "Non-vegElement"  
  9.     ),  
  10.   ],  
  11. ),  

Center

This widget is used to center the child widget, which comes inside it. All the previous examples contain inside the center widget.

Example

  1. Center(  
  2.   child: new clumn(  
  3.     mainAxisAlignment: MainAxisAlignment.spaceEvenly,  
  4.     children: <Widget>[  
  5.       new Text(  
  6.         "VegElement",  
  7.       ),  
  8.       new Text(  
  9.         "Non-vegElement"  
  10.       ),  
  11.     ],  
  12.   ),  
  13. ),  

Padding

This widget wraps other widgets to give them padding in specified directions. You can also provide padding in all directions. We can understand it from the below example that gives the text widget padding of 6.0 in all directions.

Example

  1. Padding(  
  2.   padding: const EdgeInsets.all(6.0),  
  3.   child: new Text(  
  4.     "Element 1",  
  5.   ),  
  6. ),  

Scaffold

This widget provides a framework that allows you to add common material design elements like AppBar, Floating Action Buttons, Drawers, etc.

Stack

It is an essential widget, which is mainly used for overlapping a widget, such as a button on a background gradient.

State Management Widget

In Flutter, there are mainly two types of widget:

  • StatelessWidget
  • StatefulWidget

StatefulWidget

A StatefulWidget has state information. It contains mainly two classes: the state object and the widget. It is dynamic because it can change the inner data during the widget lifetime. This widget does not have a build() method. It has createState() method, which returns a class that extends the Flutters State Class. The examples of the StatefulWidget are Checkbox, Radio, Slider, InkWell, Form, and TextField.

Example

  1. class Car extends StatefulWidget {  
  2.   const Car({ Key key, this.title }) : super(key: key);   
  3.   
  4.   @override  
  5.   _CarState createState() => _CarState();  
  6. }  
  7.   
  8. class _CarState extends State<Car> {  
  9.   @override  
  10.   Widget build(BuildContext context) {  
  11.     return Container(  
  12.       color: const Color(0xFEEFE),  
  13.            child: Container(  
  14.             child: Container( //child: Container() )  
  15.         )  
  16.     );  
  17.   }  
  18. }  

StatelessWidget

The StatelessWidget does not have any state information. It remains static throughout its lifecycle. The examples of the StatelessWidget are Text, Row, Column, Container, etc.

Example

  1. class MyStatelessCarWidget extends StatelessWidget {  
  2.   const MyStatelessCarWidget ({ Key key }) : super(key: key);  
  3.   
  4.   @override  
  5.   Widget build(BuildContext context) {  
  6.     return Container(color: const Color(0x0xFEEFE));  
  7.   }  
  8. }  


Post a Comment

Previous Post Next Post