Obafemi Emmanuel

Understanding Flutter Basics

Published 3 months ago

Flutter is Google's UI toolkit for building natively compiled applications for mobile, web, and desktop from a single codebase. In this blog, we will explore the fundamentals of Flutter, covering the project structure, the main.dart file, widgets, BuildContext, and the difference between hot reload and hot restart.


2.1 Flutter Project Structure

When you create a new Flutter project, the default structure looks like this:

my_flutter_app/
|-- android/       # Native Android code
|-- ios/           # Native iOS code
|-- lib/           # Main application code (Dart files)
|-- test/          # Unit and widget tests
|-- web/           # Web-related files (if enabled)
|-- pubspec.yaml   # Project dependencies and metadata
|-- README.md      # Project description

Key Directories and Files:

  • lib/: This is the most important folder where the Dart source code resides. The main.dart file is located here.
  • pubspec.yaml: This file contains dependencies, assets, and metadata about the project.
  • android/ and ios/: These folders contain platform-specific native code.
  • test/: This directory contains test cases to ensure app functionality.

2.2 Understanding main.dart & MaterialApp

The main.dart file serves as the entry point of a Flutter application. A basic Flutter app structure looks like this:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomeScreen(),
    );
  }
}

Explanation:

  • main(): This is the entry point of the app.
  • runApp(MyApp()): It launches the Flutter application.
  • MaterialApp: This is a wrapper that provides Material Design styling.
  • home: HomeScreen(): Specifies the default screen of the app.

2.3 Widgets: Stateless vs Stateful

Widgets are the building blocks of a Flutter application. They define how the UI looks and behaves.


StatelessWidget

A StatelessWidget does not maintain any state and remains immutable after being created.

class MyStatelessWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Stateless Widget')),
      body: Center(child: Text('I am a stateless widget')),
    );
  }
}

StatefulWidget

A StatefulWidget maintains state, meaning its properties can change dynamically.

class MyStatefulWidget extends StatefulWidget {
  @override
  _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  int counter = 0;

  void _incrementCounter() {
    setState(() {
      counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Stateful Widget')),
      body: Center(child: Text('Counter: \$counter')),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        child: Icon(Icons.add),
      ),
    );
  }
}

2.4 Understanding BuildContext

BuildContext is a reference to the location of a widget in the widget tree. It is used to obtain theme data, navigate between screens, and access other widgets.

Example usage in navigation:

Navigator.push(
  context,
  MaterialPageRoute(builder: (context) => SecondScreen()),
);

Key points:

  • Each widget has a BuildContext.
  • It is useful for accessing the widget tree hierarchy.
  • It should not be stored beyond the lifecycle of a widget.

2.5 Hot Reload vs Hot Restart

Flutter offers two powerful features for developers: Hot Reload and Hot Restart.


Hot Reload

  • Updates the UI while preserving the app state.
  • Faster development as it doesn’t restart the whole app.
  • Useful for UI adjustments and minor logic changes.

Hot Restart

  • Restarts the app and clears the state.
  • Necessary when changing global configurations or dependencies.

Feature Preserves State Use Case Hot Reload Yes UI changes, debugging Hot Restart No Dependency changes, state resets Conclusion

In this guide, we covered:

  1. The Flutter project structure.
  2. The importance of main.dart and MaterialApp.
  3. The difference between StatelessWidget and StatefulWidget.
  4. Understanding BuildContext and its role in widget trees.
  5. The distinction between Hot Reload and Hot Restart.

By mastering these basics, you’ll have a solid foundation for building Flutter applications. Stay tuned for more advanced Flutter topics!


Leave a Comment


Choose Colour