Flutter Widgets

Flutter Widgets

Master the art of Flutter

What are widgets?

Widgets are the building blocks of Flutter apps. A widget is a reusable piece of UI that can be displayed on the screen. Widgets can be combined to create complex and interactive user interfaces.

Types of widgets

There are many different types of widgets in Flutter, including:

  • Layout widgets: These widgets are used to arrange other widgets on the screen. Examples include Container, Row, and Column.

  • Text widgets: These widgets are used to display text on the screen. Examples include Text and RichText.

  • Image widgets: These widgets are used to display images on the screen. Examples include Image and AssetImage.

  • Button widgets: These widgets are used to allow users to interact with the app. Examples include RaisedButton and FlatButton.

  • Input widgets: These widgets allow users to input text or other data into the app. Examples include TextField and Checkbox.

Common widgets

Some of the most common widgets in Flutter include:

  • AppBar: Displays a bar at the top of the screen with a title, actions, and a leading widget.

  • BottomNavigationBar: Displays a navigation bar at the bottom of the screen with items that can be tapped to navigate to different screens.

  • Scaffold: Provides a basic layout for screens with an app bar, body, and bottom navigation bar.

  • Text: Displays text on the screen.

  • Image: Displays an image on the screen.

  • RaisedButton: Displays a raised button that can be tapped to perform an action.

  • TextField: Allows users to input text into the app.

Creating your own widgets

You can also create your own custom widgets in Flutter. To do this, you need to create a class that extends the Widget class. You then need to implement the build() method, which is responsible for rendering the widget.

Here is an example of a custom widget that displays a greeting message:

import 'package:flutter/material.dart';

class GreetingWidget extends StatelessWidget {
  final String name;

  GreetingWidget(this.name);

  @override
  Widget build(BuildContext context) {
    return Text('Hello, $name!');
  }
}

To use this widget, you would simply add it to your widget tree like any other widget:

Widget build(BuildContext context) {
  return Scaffold(
    body: Center(
      child: GreetingWidget('John Doe'),
    ),
  );
}

Styling widgets

You can style widgets in Flutter using the style property. The style property can be used to change the appearance of a widget, such as its color, font, and size.

Here is an example of how to style a Text widget:

Widget build(BuildContext context) {
  return Scaffold(
    body: Center(
      child: Text(
        'Hello, John Doe!',
        style: TextStyle(
          fontSize: 24,
          color: Colors.red,
        ),
      ),
    ),
  );
}

Conclusion

Widgets are the building blocks of Flutter apps. By understanding widgets, you can create beautiful and engaging user interfaces.

Here are some additional code examples of common widgets in Flutter:

// Display an AppBar with a title and a leading icon
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text('My App'),
      leading: Icon(Icons.menu),
    ),
  );
}

// Display a BottomNavigationBar with two items
Widget build(BuildContext context) {
  return Scaffold(
    bottomNavigationBar: BottomNavigationBar(
      items: [
        BottomNavigationBarItem(
          icon: Icon(Icons.home),
          label: 'Home',
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.settings),
          label: 'Settings',
        ),
      ],
    ),
  );
}

// Display a TextField where the user can input their name
Widget build(BuildContext context) {
  final nameController = TextEditingController();

  return Scaffold(
    body: Center(
      child: TextField(
        controller: nameController,
        decoration: InputDecoration(
          labelText: 'Enter your name:',
        ),
      ),
    ),
  );
}

In the next article, we will learn one of the most important concepts in Flutter development: State Management.