Skip to main content

What is Widget ? Types of Widget

What is Widget?

In Flutter, we interact with the UI so the whole UI is made up of Widgets. If you are known to Android and Ios so in that we are using Views and UIViews same as in flutter it's called Widget but 

- The widget is not only about view it is more than that. Widgets describe what their view should look like given their current configuration and state.

Types of Widgets : 

Before we are discussing types of widgets first will know about State and what is the function we can use in that.

What is State?

- According to Flutter, State is information that can be read synchronously when the widget is built and might change during the lifetime of the widget. 

- It is the responsibility of the widget implementer to ensure that the State is promptly notified when such state changes with the use of function State.setState. State objects are created by the framework by calling the StatefulWidget.createState.

A widget can be either a stateless widget or a stateful widget

Now, We discuss Types of Widget in Detail :

1. Stateless Widget

- A stateless Widget means a widget that has an immutable state. A stateless widget can only be drawn once when the Widget is loaded/built and cannot be redrawn based on any events or user actions. Their state depends on the information provided by the parent.

- Any change in the variables, icons, buttons, or retrieving data can not change the state of the app. the basic structure of a stateless widget overrides the build() method and returns a widget.

For example, we use Text or the Icon in our flutter application where the state of the widget does not change in the runtime. It is used when the UI depends on the information within the object itself. Other examples can be Text, RaisedButton, IconButtons.

 Let's create one example of a Stateless widget :

 import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return Container();

  }

}


So, let's discuss the code what I have done and what is the meaning of this. The stateless widget name is MyApp which is being called from the runApp() and extends a stateless widget. Inside this MyApp a build function is overridden and takes BuildContext as a parameter. This BuildContext is unique to each and every widget as it is used to locate the widget inside the widget tree.

- The widgets of a Flutter application are displayed in the form of a Widget Tree where we connect the parent widget and child widgets to show a relationship between them which then combines to form the state of your app.

The build function contains a container which is a widget of Flutter inside which we will design the UI of the app.  In the stateless widget, the build function is called only once, making the UI of the screen.

2. Stateful Widget

A stateful Widget means a widget with a mutable state that changes its state multiple times in its lifetime. It is the responsibility of the widget that the State is promptly notified when the state changes and it will use the method SetStste(). This simply means the state of an app can change multiple times with different sets of variables, inputs, data. and it is used when the UI can change dynamically.

- Stateful widgets are CheckBox, RadioButton, Form, TextField, Slider, InkWell, and many more.

Let's create one example of a Stateful widget :

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {

  @override

  _MyAppState createState() => _MyAppState();

}

class _MyAppState extends State<MyApp> {

  @override

  Widget build(BuildContext context) {

    return Container();

  }

}


So let's discuss the code what I have done and what is the meaning of this. The Stateful Widget name is MyApp which is called from the runApp() and extends a stateful widget. In the MyApp class, we override the create state function. This createState() function is used to create a mutable state for this widget at a given location in the tree. This method returns an instance for their belonging state subclass.

- The other class which is _MyAppState extends the state, which manages all the changes in the widget. Inside this class, the build function is overridden which takes the BuildContext as the parameter. This build function returns a widget where we design the UI of the app. Since it is a stateful widget, the build function is called many times, creating the entire UI once again with all the changes.

Comments

Post a Comment

Popular posts from this blog

Flutter BottomNavigationBar

BottomNavigationBar A material widget that's displayed at the bottom of an application. Which shows around three to five items and it includes labels and icons. BottomNavigationBar allows you to select one item at a time and quickly navigate to a given page. Properties of the BottomNavigationBar : items: It defines the list of BottomNavigationBar Items to display the bottom navigation bar. currentIndex: It determines the current active bottom navigation bar item on the screen. onTap: It is called when we tapped one of the items on the screen. iconSize: It is used to specify the size of all bottom navigation item icons. fixedColor: It is used to set the color of the selected item. If we have not set a color to the icon or title, it will be shown. type: It determines the layout and behavior of a bottom navigation bar. It behaves in two different ways that are:  1. fixed: Default it is fixed and If it is null, it will use fixed. 2. shifting: It will use shifting where w...

MaterialApp and Scaffold in Flutter with Example

What is MaterialApp Material design is for Google Android material design which is used to create UI for Android.MaterialApp is a predefined class in a flutter that extends the Material Design class. It is likely the main or core component of flutter. We can access all the other components and widgets provided by Flutter SDK. It is a class that creates an instance of [WidgetsApp] . Text widget, AppBar widget, Scaffold widget, ListView widget, StatelessWidget, StatefulWidget, IconButton widget, TextField widget, and many more widgets that are accessed using MaterialApp class.  For iOS, there is the Cupertino widgets class which I'll describe or discuss or explain in another article. What is Scaffold In Flutter Scaffold is a very useful and highly flexible class. Scaffold implements the basic material design layout structure. A Scaffold Widget provides a framework that implements the flutter app's basic material design visual layout structure.  The developer can implement a wide...

Flutter TextField with Examples

Introduction to TextField TextField in Flutter is the most commonly used text input widget that allows users to collect inputs from the keyboard into an app. We can use the TextField widget in building forms, sending messages, creating search experiences, and many more. By default, Flutter decorated the TextField with an underline. We can also add several attributes with TextField, such as label, icon, inline hint text, and error text using an InputDecoration as the decoration. If we want to remove the decoration properties entirely, it is required to set the decoration to null. 1. decoration It is used to show the decoration around TextField.The most common attributes in decoration are as follows: border : It is used to create a default rounded rectangle border around TextField. labelText : It shows the label text on the selection of TextField. hintText : It shows the hint text inside TextField when there is no text entered.   TextField(             ...