In this section, we will guide you through the process of creating your first Flutter app. By the end of this lesson, you will have a basic understanding of how to set up a new Flutter project, run it on an emulator or physical device, and understand the structure of a Flutter application.
Step 1: Setting Up Your Flutter Project
- Open your terminal or command prompt.
- Navigate to the directory where you want to create your project.
cd path/to/your/projects - Create a new Flutter project using the
flutter createcommand.flutter create my_first_app - Navigate into your new project directory.
cd my_first_app
Step 2: Understanding the Project Structure
When you create a new Flutter project, it generates a number of files and directories. Here’s a brief overview of the most important ones:
lib/: This directory contains the main code for your application. Themain.dartfile is the entry point of your app.pubspec.yaml: This file manages the dependencies and metadata for your project.android/: Contains the Android-specific code and configurations.ios/: Contains the iOS-specific code and configurations.test/: Contains the test files for your application.
Step 3: Running Your App
- Open your project in your preferred IDE (e.g., Visual Studio Code, Android Studio).
- Ensure you have an emulator running or a physical device connected.
- Run the app using the following command in the terminal:
flutter run
Alternatively, you can use the run/debug options provided by your IDE.
Step 4: Exploring the Default Flutter App
When you run the app, you will see a default Flutter application with a counter that increments every time you press the floating action button. Let’s take a look at the main.dart file to understand how this works.
main.dart File
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: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}Explanation
main()function: The entry point of the application. It callsrunApp()with an instance ofMyApp.MyAppclass: A stateless widget that sets up theMaterialAppand its theme.MyHomePageclass: A stateful widget that represents the home page of the app._MyHomePageStateclass: Manages the state ofMyHomePage, including the counter and the method to increment it.
Step 5: Modifying the App
Let’s make a simple modification to the app. We will change the text displayed in the center of the screen.
- Open
lib/main.dart. - Find the
Textwidget inside theColumnwidget. - Change the text from
'You have pushed the button this many times:'to'Button pressed:'.
- Save the file and observe the changes in your running app.
Practical Exercise
Task
- Create a new Flutter project named
hello_flutter. - Modify the
main.dartfile to display a text "Hello, Flutter!" in the center of the screen.
Solution
-
Create the project:
flutter create hello_flutter cd hello_flutter -
Modify
lib/main.dart:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Hello Flutter App'),
),
body: Center(
child: Text('Hello, Flutter!'),
),
),
);
}
}- Run the app:
flutter run
Conclusion
Congratulations! You have successfully created and modified your first Flutter app. You now have a basic understanding of the Flutter project structure and how to run and modify a Flutter application. In the next module, we will dive into Dart programming basics, which is essential for building Flutter applications.
Flutter Development Course
Module 1: Introduction to Flutter
- What is Flutter?
- Setting Up the Development Environment
- Understanding Flutter Architecture
- Creating Your First Flutter App
Module 2: Dart Programming Basics
- Introduction to Dart
- Variables and Data Types
- Control Flow Statements
- Functions and Methods
- Object-Oriented Programming in Dart
Module 3: Flutter Widgets
- Introduction to Widgets
- Stateless vs Stateful Widgets
- Basic Widgets
- Layout Widgets
- Input and Form Widgets
Module 4: State Management
Module 5: Navigation and Routing
Module 6: Networking and APIs
- Fetching Data from the Internet
- Parsing JSON Data
- Handling Network Errors
- Using REST APIs
- GraphQL Integration
Module 7: Persistence and Storage
- Introduction to Persistence
- Shared Preferences
- File Storage
- SQLite Database
- Using Hive for Local Storage
Module 8: Advanced Flutter Concepts
- Animations in Flutter
- Custom Paint and Canvas
- Platform Channels
- Isolates and Concurrency
- Performance Optimization
Module 9: Testing and Debugging
Module 10: Deployment and Maintenance
- Preparing for Release
- Building for iOS
- Building for Android
- Continuous Integration/Continuous Deployment (CI/CD)
- Maintaining and Updating Your App
