Lab: Introduction to Flutter Development
📌 Overview
This lab introduces the Flutter framework, compares different mobile development approaches, and guides students through setting up and coding their first Flutter application using Android Studio. The content is based on the foundational concepts of Native, Hybrid, and Cross-Platform development.
🎯 Learning Objectives
By the end of this lab, students should be able to:
- Understand the difference between Native, Hybrid, and Cross-Platform (Flutter) development.
- Explain the core pillars of the course: UI, State Management, Networking, and Animation.
- Set up a Flutter project in Android Studio.
- Write a basic Flutter application using
MaterialApp,Center, andContainerwidgets.
1. Introduction to Flutter
Flutter is a platform that allows developers to build high-quality mobile applications for iOS and Android. It is increasingly popular due to its future potential and the strong backing from Google, which ensures long-term support.
Why Learn Flutter?
- Future-Proofing: The job market is trending towards technologies like Flutter, which allow rapid development for multiple platforms.
- Efficiency: It enables building apps that look and perform like native applications, sometimes even surpassing them in UI consistency.
- Prerequisites: Students should have a solid understanding of Object-Oriented Programming (OOP) before diving deep into Flutter.
2. Mobile Development Architectures
To understand how Flutter works, it is essential to compare it with other development methodologies.
A. Native Development
- Mechanism: You build separate apps using the specific SDKs provided by Apple (iOS) or Google (Android).
- Pros: Best performance, direct access to latest device features immediately upon release.
- Cons: Expensive and time-consuming as it requires maintaining two separate codebases (e.g., Java/Kotlin for Android and Swift/Obj-C for iOS).
B. Hybrid Development
- Mechanism: Uses web technologies (HTML/CSS/JS) wrapped in a WebView. The app runs essentially as a website inside a native container. It uses a "Bridge" to communicate with native hardware (like Bluetooth).
- Pros: Code reusability across platforms.
- Cons: Performance issues, harder debugging, and reliance on plugins for native features. If a native feature is new, you must wait for a plugin wrapper to be created.
C. Cross-Platform (Flutter)
- Mechanism: Flutter does not use a WebView or native OEM widgets. Instead, it asks the system for a blank Canvas and uses its own rendering engine to draw every pixel of the UI.
- Language: Uses Dart, which compiles to native code.
- Pros: High performance (60 FPS), consistent UI across devices, and the Hot Reload feature, which allows you to see code changes instantly without restarting the app.
📊 Architecture Comparison
graph TD
subgraph Native
A[App Code] -->|Direct Access| B[OEM Widgets]
end
subgraph Hybrid
C[Web App Code] -->|Runs In| D[WebView]
D -->|Bridge| E[Native Features]
end
subgraph Flutter
F[Dart Code] -->|Compiled| G[Flutter Engine]
G -->|Draws Pixels| H[System Canvas]
end3. Course Methodology
This course focuses on building maintainable, high-quality code. The curriculum is structured around four main pillars:
- User Interface (UI): Building the visual layout of the application.
- State Management: Handling data changes and app states efficiently.
- Networking: Connecting to APIs and handling server data.
- Animation: Adding visual flair to the user experience.
4. Hands-On: Your First Flutter App
Step 1: Project Setup
- Open Android Studio.
- Select "Start a new Flutter project".
- Choose Flutter Application and click Next.
- Project Name: Must be lowercase (e.g.,
test_appormy_first_app). - Company Domain: Used to generate the package name (e.g.,
com.example.myapp). - Click Finish and wait for the project to index.
Step 2: Understanding the Environment
Once the project opens, you will see the default counter app.
- Emulator: You can run the app on an Android Emulator, iOS Simulator, or even a web browser.
- Hot Reload: A key feature where changes in the code are reflected almost instantly in the app without losing state.
Step 3: Writing Code
We will clear the default code and write a minimal app from scratch.
-
Main Entry Point: Every Flutter app starts with the
main()function which callsrunApp(). -
The Widget Tree: Flutter uses Widgets for everything. The root widget is often
MaterialApp.import 'package:flutter/material.dart'; void main() { runApp( MaterialApp( home: Center( child: Container( color: Colors.red, // Sets the container color to red width: 40.0, // Sets width height: 40.0, // Sets height child: Text('Hi'), // Text inside the container ), ), ), ); } -
Code Analysis:
MaterialApp: The core widget that sets up the material design style.Center: A layout widget that centers its child within the screen.Container: A versatile widget used for styling (colors, dimensions).Text: Displays a string of text.
🛠️ Troubleshooting Tip
If the layout looks strange (e.g., text appears with yellow underlines or red formatting), it is often because the widget lacks default text styling provided by a Scaffold or Material widget. For this basic example, MaterialApp provides enough context for the Container to render.
📝 Lab Task
- Create a new Flutter project named
lab_one_intro. - Replace the default code with a
MaterialApp. - Use a
Centerwidget to position aContainerin the middle of the screen. - Set the container's color to blue and its size to
100.0by100.0. - Add a child
Textwidget inside the container with your initials.