6. Widgets in Flutter

Página 72

Widgets are the fundamental elements of a user interface (UI) in Flutter. They are the building blocks from which Flutter builds its UI. They are immutable and described in terms of other lower-level widgets. They come in two varieties: StatelessWidget and StatefulWidget.

A StatelessWidget is a widget that describes part of the user interface that may be configuration dependent but not state dependent. In other words, it describes what the UI looks like given its current configuration. StatelessWidget is immutable, which means that once its values ​​are set, they cannot be changed.

On the other hand, a StatefulWidget is dynamic. State is information that (1) can be read synchronously when the widget is built and (2) can change during the lifetime of the widget. It is an implementation of a widget that may change over time. For example, if a widget has a counter that increments each time the user taps a button, the counter's value is the state.

In Flutter, almost everything is a widget. This includes alignment, padding, layout, etc. They follow a very strong compositional philosophy, which means that you can combine several small widgets together to form a more complex widget. Widgets are organized in a widget tree.

There are many types of widgets in Flutter, but they are commonly divided into two types: layout widgets and UI widgets. Layout widgets like Container, Padding, Column, Row, etc. do not provide a visually noticeable UI. Instead, they control how child widgets are arranged or rendered in the application.

UI widgets such as Text, RaisedButton, FlatButton, etc. provide a visually perceptible UI. They are often used in conjunction with layout widgets to create the application's structure and user interface.

The widgets in Flutter are very powerful and allow you to create complex and beautiful user interfaces with little code. They are the foundation of all Flutter apps and are the main way to build and organize the user interface.

To create a widget in Flutter, you need to define a new class that extends StatelessWidget or StatefulWidget. Next, you must implement the build method, which is called whenever Flutter needs to render the widget. The build method should return a new widget object that Flutter can render to the screen.

An example of a simple widget would be a text widget. To create a text widget you can do the following:

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

This is a very simple example, but it shows how widgets are built in Flutter. You define a new class that extends StatelessWidget, implement the build method, and return a new Text widget that Flutter can render to the screen.

In short, widgets are the foundation of all Flutter apps. They are powerful and allow you to create complex and beautiful user interfaces with little code. If you're just starting to learn Flutter, I recommend you spend some time understanding how widgets work, as they are a fundamental part of Flutter.

Now answer the exercise about the content:

What is a widget in Flutter and how is it created?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

736.1. Widgets in Flutter: Introduction to Flutter and Dart

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text