Flutter Course: 6.3 Understanding Basic Flutter Code 1

Hello! In this course, we will understand the basic code structure of Flutter and take a deep dive into the essential contents needed to create a Flutter application.

1. What is Flutter?

Flutter is an open-source UI software development kit (SDK) developed by Google, which allows you to create applications that can run on both iOS and Android platforms with a single codebase. Flutter provides fast performance and high productivity, offering the necessary tools to design attractive user interfaces through intuitive UI widgets.

2. Structure of a Flutter Application

A Flutter application consists of several files and directories. It typically has the following structure:

my_flutter_app/
├── android/
├── ios/
├── lib/
│   └── main.dart
├── test/
└── pubspec.yaml
  • android/: Contains files related to the Android platform.
  • ios/: Contains files related to the iOS platform.
  • lib/: The directory where the main code of the Flutter application is located. Typically, the ‘main.dart’ file is found here.
  • test/: Contains test codes.
  • pubspec.yaml: A file that defines the application’s metadata, dependencies, and resources.

3. Analyzing the main.dart File

The ‘main.dart’ file inside the ‘lib’ directory is the entry point of the Flutter application. In this file, you can find the code where the application starts. For example, here is a simple ‘main.dart’ file for a Flutter application:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My Flutter App',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Home Screen'),
        ),
        body: Center(
          child: Text('Hello, Flutter!'),
        ),
      ),
    );
  }
}

3.1. runApp() Function

The runApp() function is used to run the Flutter application. This function takes the top-level widget, the ‘MyApp’ class, as an argument to start the application.

3.2. MyApp Class

The MyApp class extends StatelessWidget. StatelessWidget is a widget without state, meaning it doesn’t change after being created. The build method of MyApp returns a MaterialApp widget.

3.3. MaterialApp Widget

The MaterialApp widget is used to set the style and navigation of the application. Here, the title property sets the application’s title, and the home property defines the application’s home screen.

3.4. Scaffold Widget

The Scaffold widget provides the basic UI layout structure for Flutter. Using the Scaffold widget, you can easily include various UI elements such as AppBar, Body, and Floating Action Button.

3.5. AppBar and Center Widgets

The AppBar widget creates a fixed app bar at the top and allows you to set a title. The Center widget is used to position the child widget in the center and displays the message “Hello, Flutter!” through the Text widget.

4. The Concept of Widgets

Everything in Flutter is made up of widgets. Widgets are components of the user interface, including text, images, buttons, etc. In Flutter, you can create complex UIs by combining widgets. Widgets are broadly divided into two types:

  • StatelessWidget: A widget without state that does not change after being created.
  • StatefulWidget: A widget that has internal state and rebuilds the UI whenever the state changes.

5. Basic UI Components

Let’s look at some basic UI components frequently used in Flutter.

5.1. Text Widget

Text('Hello, Flutter!')

The Text widget is used to display a string. It provides basic text styling, and you can adjust the font size, color, and more through various properties.

5.2. Container Widget

Container(
  width: 200,
  height: 200,
  color: Colors.blue,
)

The Container widget creates a rectangular box and allows you to set properties such as size, color, margin, and padding. It is very useful for laying out and styling UI elements.

5.3. Row and Column Widgets

Row(
  children: [
    Icon(Icons.star),
    Text('Star'),
  ],
)

Column(
  children: [
    Text('First Item'),
    Text('Second Item'),
  ],
)

The Row widget arranges its child widgets in a horizontal line, while the Column widget arranges them vertically. These layout widgets are essential for UI design.

5.4. Button Widgets

ElevatedButton(
  onPressed: () {
    // Code to be executed when the button is clicked
  },
  child: Text('Click Here'),
)

There are various widgets for creating buttons, providing different styles such as ElevatedButton, TextButton, and OutlinedButton.

6. State Management

State management is a very important concept in Flutter. It is a method of effectively managing the state of widgets to update the UI. There are various methods for state management, and here are some key approaches:

  • setState(): Used to change the state within a StatefulWidget.
  • InheritedWidget: A method for sharing data across the widget tree.
  • Provider Package: A widely used package for more complex state management.

7. Application Design Best Practices

When designing a Flutter application, it’s good to follow some best practices:

  • Modularize your code to enhance reusability and maintainability.
  • Break widgets down into smaller units to reduce complexity.
  • Clearly define your state management approach to improve code readability.

8. Conclusion

In this course, we explored the basic code structure of a Flutter application and looked into foundational concepts regarding widgets, application design, and state management. In the next course, we will discuss how to create more complex Flutter applications. Thank you!