Introduction
Flutter is an open-source mobile UI framework created by Google which allows developers to write a single code base and compile it to native ARM code for both Android and iOS. It is gaining popularity due to its beautiful UI, great performance and its ability to create applications for multiple platforms with a single codebase.
Here's a basic guide to creating a new Flutter project and setting up your first application.
Requirements:
- Install Flutter SDK from the official Flutter website (https://flutter.dev/).
- Install Dart SDK (it comes with Flutter SDK if you've installed Flutter).
- Install a code editor. Visual Studio Code and Android Studio are popular options.
- Install Flutter and Dart plugins in your editor if available.
Step 1: Create a New Flutter Project
Open your terminal and enter the following command to create a new Flutter project.
flutter create my_app
Replace 'my_app' with your preferred application name. This command creates a new directory called "my_app" with all the required files and directories.
Step 2: Navigate to Your Project Directory
Use the following command to navigate to your project directory:
cd my_app
Step 3: Run the Flutter App
Now, run the following command to run your app:
flutter run
You should see a counter app which is a default Flutter app.
Step 4: Open the Main Dart File
Open your Flutter project in your preferred editor and navigate to lib/main.dart
file. This is the main file where you'll write all of your Dart code for your Flutter application.
Step 5: Clean up the Default Flutter App
In lib/main.dart
, you will see a lot of code which creates a counter app. For the sake of simplicity, delete all the code in main.dart
and replace it with the following:
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('My First Flutter App'),
),
body: Center(
child: Text('Hello World!'),
),
),
);
}
}
Step 6: Understanding the code
void main() => runApp(MyApp());
This is the main function of the app, which calls therunApp()
function withMyApp
as an argument.MyApp
is a custom class that extends theStatelessWidget
class.StatelessWidget
is a Flutter class which describes part of the user interface which can't change over time.Widget build(BuildContext context)
This overridden method returns a new Widget which describes how this widget transforms the set of children widgets which follow it in the tree.MaterialApp
is a predefined class in Flutter which includes a number of components likeAppBar
,IconButton
, etc to provide a base of Material Design.Scaffold
provides a framework which is implemented according to the Material Design guidelines. It's like a drawer where you place yourAppBar
,BottomNavigationBar
,FloatingActionButton
, and many other Material Design widgets.AppBar
widget automatically creates a Material Design AppBar.Center
widget aligns its child widget to the center of available space on the screen.Text
widget is the simplest way to get text on the screen.
Step 7: Run Your Application Again
Save your changes and run the application again by typing flutter run
in the terminal. You should see an app with a title and a text saying 'Hello World!'
Conclusion
This is a very basic guide to start your journey in Flutter. Once you are comfortable with these steps, you can explore more complex widgets and APIs provided by Flutter and Dart. You can also explore state management techniques, animations, and more as you progress. Happy coding!