The Wayback Machine - https://web.archive.org/web/20240620235913/https://www.geeksforgeeks.org/what-is-widgets-in-flutter/
Open In App

What is Widgets in Flutter?

Last Updated : 01 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Flutter is Google’s UI toolkit for crafting beautiful, natively compiled iOS and Android apps from a single code base. To build any application we start with widgets – The building block of Flutter applications. Widgets describe what their view should look like given their current configuration and state. It includes a text widget, row widget, column widget, container widget, and many more. 

What are Widgets?

Each element on the screen of the Flutter app is a widget. The view of the screen completely depends upon the choice and sequence of the widgets used to build the apps. And the structure of the code of apps is a tree of widgets. 

Category of Widgets

There are mainly 14 categories into which the flutter widgets are divided. They are mainly segregated on the basis of the functionality they provide in a flutter application.

Widgets

Description

Accessibility

These are the set of widgets that make a Flutter app more easily accessible.

Animation and Motion

These widgets add animation to other widgets.

Assets, Images, and Icons

These widgets take charge of assets such as display images and show icons.

Async

These provide async functionality in the Flutter application.

Basics

These are the bundle of widgets that are absolutely necessary for the development of any Flutter application.

Cupertino

These are the iOS-designed widgets.

Input

This set of widgets provides input functionality in a Flutter application.

Interaction Models

These widgets are here to manage touch events and route users to different views in the application.

Layout

This bundle of widgets helps in placing the other widgets on the screen as needed.

Material Components

This is a set of widgets that mainly follow material design by Google.

Painting and effects

This is the set of widgets that apply visual changes to their child widgets without changing their layout or shape.

Scrolling

This provides scrollability of to a set of other widgets that are not scrollable by default.

Styling

This deals with the theme, responsiveness, and sizing of the app.

Text

This displays text.

Types of Widgets

There are broadly two types of widgets in the flutter: 

  • Stateless Widget
  • Stateful Widget

1. Stateless Widget

Stateless Widget is a type of widget which once built , then it’s properties and state can’t be changed. These widgets are immutable, once created can’t be modified.

Note: These are used for static content or UI content that don’t need a change after time.

Key Characterstics of Stateless Widgets are: Immutable , No State and Lightweight.

Examples: Display Text , Icons, Images, etc.

2. Stateful Widget

Stateful Widgets is a type of widget that can change state. It can maintain and update the appearance in the response to change in state.

Note: These are used for dynamic change in the properties and appearance over the time.

Key Characterstics of Stateful Widgets are: Mutable State , State Lifecycle and Dynamic Updates.

Examples: Buttons, Sliders, Text Fields, etc.

Implementation of Stateful and Stateless Widgets

Below is the Image with showing the Layout Tree:

Layout_Tree

Description of the widgets used are as follows: 

  • Scaffold – Implements the basic material design visual layout structure.
  • App-Bar – To create a bar at the top of the screen.
  • Text  To write anything on the screen.
  • Container – To contain any widget.
  • Center – To provide center alignment to other widgets.

Example: The Layout Tree of basic app screen using Stateless Widgets: 

Dart
import 'package:flutter/material.dart';

// function to trigger build process
void main() => runApp(const GeeksforGeeks());

class GeeksforGeeks extends StatelessWidget {
  const GeeksforGeeks({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.lightGreen,
        appBar: AppBar(
          backgroundColor: Colors.green,
          title: const Text("GeeksforGeeks"),
        ), // AppBar
        body: Container(
          child: const Center(
            child: Text("Hello Geeks!!"),
          ), // Center
        ), // Container
      ), // Scaffold
    ); // MaterialApp
  }
}

 
Example:  The Layout Tree of basic app screen using Stateful Widgets. This also produces the same results as the above code.

Dart
import 'package:flutter/material.dart';

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

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  // ignore: library_private_types_in_public_api
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.lightGreen,
        appBar: AppBar(
          backgroundColor: Colors.green,
          title: const Text("GeeksforGeeks"),
        ), // AppBar
        body: const Center(
          child: Text("Hello Geeks!!"),
        ), // Container
      ), // Scaffold
    );// MaterialApp
  }
}

Output: 

Widgets in Flutter Example

Previous Article
Next Article

Similar Reads

Flutter - Custom Widgets
We create Custom Widgets when we want a custom look and feel to our app, and we know that there will be a repetition of a particular widget. We can create the custom widget in a new dart file with all the codes and defining the parameters that we need in the constructor. For more on how to split widgets, you can visit the article on Flutter - Split
8 min read
Flutter - Row and Column Widgets
Row and Column are the two most important and powerful widgets in Flutter. These widgets let you align children horizontally and vertically as per the requirement. As we know that when we design any UI(User Interface) in a flutter, we need to arrange its content in the Row and Column manner so these Row and Column widgets are required when designin
6 min read
How to Add Space Between Widgets in Flutter?
In this article, we'll learn how to add space between widgets. There are many options available in flutter which you can use to provide space and make UI attractive. If you use Row and Column for arranging widgets, then by default limited options are available for alignment. There are many options available for the spacing of widgets like Padding,
4 min read
Flutter - Splitting App into Widgets
Splitting an app into widgets refers to the breaking up of large widgets into smaller, more manageable widgets which can be inferred as smaller chunks of codes. The principal thing here is to understand when to split a widget into smaller ones. We are going to discuss when to split a widget and methods of how to perform this. When to Split Widgets
5 min read
Flutter - Outputting Widgets Conditionally
Flutter is Google’s UI toolkit for building beautiful, natively compiled applications for mobile, web, and desktop from a single codebase. Flutter provides various ways to display Widgets conditionally and in this article, we are going to implement all the methods. Method 1: Using If condition This is flutter's way to display a widget if the condit
5 min read
Flutter - Concept of Visible and Invisible Widgets
In this article, we will see how we can change the visibility of widgets in flutter. We will learn about various methods for managing the visibility of flutter widgets. There are a couple of ways to manage the visibility of widgets in flutter. Method 1: Using Visibility class It has a visible property that manages whether the child is included in t
6 min read
Flutter - Concept of Key in Widgets
In this article, we will study Keys and when and where to use them. So you may ask what are keys? Well, Keys are the ones to preserve state when widgets move around the widget tree. It is used to preserve the user scroll location or keeping state when modifying a collection. Key's aren't needed if the entire widget subtree is stateless. Now let us
5 min read
How to Get Coordinates Widgets using rect_getter Package in Flutter?
Sometimes we need to get a widget's location or show some overlay widget or something similar over a widget whose location on the screen could be dynamic. In this article, we are going to discuss the way to get coordinates of a widget using the rect_getter package. Follow the steps below : 1. Create an App: Create a new flutter app by running the b
9 min read
Life Cycle of Flutter Widgets
Stateless Widgets: The widgets which remain constant throughout the lifetime of the app are called stateless widgets. We use them when we want structures to stay the same all over the app, for example, the AppBar, color scheme, i.e. generally the User Interface(UI). These widgets are immutable, i.e. they cannot be changed. Here Hot Reload can refle
2 min read
What are Widgets Available in Flutter?
Flutter is a mobile app development framework that allows developers to create beautiful and responsive user interfaces with ease. This is made possible through the use of Flutter widgets, which are the building blocks of every Flutter application. In Flutter, widgets are the fundamental building blocks of the user interface. They are a collection
4 min read