Flutter Introduction to widgets

Flutter is an open-source framework developed by Google for building high-performance, cross-platform mobile applications for iOS, Android, and the web. One of the key features of Flutter is its use of widgets, which are the building blocks of any user interface in a Flutter app.

Widgets in Flutter are the basic visual elements that are combined to create the user interface of the app. Widgets can be categorized into two types: stateful and stateless. Stateless widgets are those that don’t change during the lifetime of the app, while stateful widgets are those that can change dynamically based on user interactions or other factors.

Let’s take a closer look at some of the most common widgets in Flutter:

  1. Text: The Text widget is used to display text on the screen. You can customize the font, size, color, and other properties of the text using the various properties of the Text widget.
  2. Image: The Image widget is used to display images in the app. You can load images from various sources such as the asset bundle, network, or file system.
  3. Container: The Container widget is a versatile widget that can be used to create boxes with specific dimensions and properties such as padding, margin, and border.
  4. Row and Column: These widgets are used to create horizontal and vertical layouts, respectively. They allow you to combine multiple widgets and arrange them in a specific order.
  5. Button: The Button widget is used to create interactive elements that allow users to trigger actions in the app. There are various types of buttons such as the TextButton, ElevatedButton, and OutlinedButton, each with its own style and behavior.
  6. TextField: The TextField widget is used to capture user input such as text or numbers. You can customize the appearance and behavior of the text field using various properties.

These are just a few examples of the many widgets available in Flutter. Widgets can be combined and nested to create complex user interfaces that are optimized for performance and flexibility.

In this article, we will explore some of the most commonly used Flutter widgets and see how they can be used to build a simple app.

Text Widget

The Text widget is used to display text in a Flutter app. It allows you to specify the text content, font style, color, alignment, and other properties. Here’s an example of how to use the Text widget:

Text(
  'Hello, world!',
  style: TextStyle(fontSize: 20),
),

In this example, we’re displaying the text “Hello, world!” using a font size of 20.

Image Widget

The Image widget is used to display images in a Flutter app. It allows you to load images from a local file or from the network. Here’s an example of how to use the Image widget:

Image.network(
  'https://codezi.pro/tutorial/wp-content/uploads/2023/02/flutter-setup.jpg',
),

In this example, we’re loading an image from the Picsum Photos API and displaying it in our app.

Container Widget

The Container widget is used to create a box in which other widgets can be placed. It allows you to specify properties like padding, margin, border, and background color. Here’s an example of how to use the Container widget:

Container(
  width: 200,
  height: 200,
  padding: EdgeInsets.all(16),
  margin: EdgeInsets.all(16),
  decoration: BoxDecoration(
    color: Colors.blue,
    border: Border.all(color: Colors.black),
  ),
  child: Text('Hello, world!'),
),

In this example, we’re creating a blue box with a black border, padding, and margin, and displaying the text “Hello, world!” inside the box.

Row and Column Widgets

The Row and Column widgets are used to lay out other widgets in a horizontal or vertical direction. They allow you to specify properties like alignment, spacing, and cross-axis alignment. Here’s an example of how to use the Row and Column widgets:

Column(
  crossAxisAlignment: CrossAxisAlignment.start,
  children: [
    Text('First item'),
    Text('Second item'),
    Text('Third item'),
  ],
),

In this example, we’re creating a column with three text widgets, each on a new line.

ListView Widget

The ListView widget is used to display a list of items in a scrollable view. It allows you to specify properties like item count, item builder, and scroll direction. Here’s an example of how to use the ListView widget:

ListView.builder(
  itemCount: 10,
  itemBuilder: (BuildContext context, int index) {
    return Text('Item $index');
  },
),

TextField

TextField is the most commonly used text input widget.

By default, a TextField is decorated with an underline. You can add a label, icon, inline hint text, and error text by supplying an InputDecoration as the decoration property of the TextField. To remove the decoration entirely (including the underline and the space reserved for the label), set the decoration to null.

TextField(
  decoration: InputDecoration(
    border: OutlineInputBorder(),
    hintText: 'Enter a search term',
  ),
),

In conclusion, widgets are a fundamental part of building a Flutter app. They allow developers to create rich, interactive user interfaces that are optimized for performance and flexibility. By understanding the different types of widgets available in Flutter, you can create beautiful and functional apps that work seamlessly across multiple platforms.

In this article, we’ve explored some of the most commonly used Flutter widgets and seen how they can be used to build a simple app. There are many more widgets available in Flutter, each with their own set of properties and capabilities. By mastering the use of these widgets, you can create beautiful and functional user interfaces for your mobile app.

Related posts:

  1. Futter Get started For Beginner
  2. Introduction to Flutter Architecture
  3. Introduction to Dart language