flutter
  1. flutter-widgets

Flutter Widgets

Widgets are the building blocks of Flutter applications. Flutter provides a rich set of pre-built widgets that can be used to create almost any type of user interface.

Syntax

The following syntax is used to create a widget in Flutter:

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      // Widget code here
    );
  }
}

Example

Let's create a simple widget that displays text on the screen.

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text('Hello, Flutter!');
  }
}

Output

The output of the above widget will be:

Hello, Flutter!

Explanation

In the above example, we have created a widget named MyWidget that displays text on the screen using the Text widget. The build method is used to define the structure and layout of the widget.

Use

Widgets can be used to build complex UIs by assembling multiple widgets together. Various types of widgets can be used such as Container, Row, Column, ListView, AppBar, and many more.

Important Points

  • Widgets are the building blocks of Flutter applications.
  • Widgets can be divided into two categories; Stateful widgets and Stateless widgets.
  • Stateful widgets are mutable and can be updated over time.
  • StatelessWidget objects are immutable and their properties don’t change (all values are final).

Summary

In this tutorial, we learned about Flutter widgets, their syntax, how to create a simple widget, how to use them to build UI, the difference between stateful and stateless widgets, and some important points to keep in mind while using widgets.

Published on: