To set up Flutter on a clean machine, follow these steps:
- Download Flutter SDK: Visit the Flutter Install page and download the Flutter SDK for your operating system.
- Add Flutter to Path: Extract the Flutter SDK and add the
flutter/bin
directory to your system's PATH environment variable. - Run Flutter Doctor: Open a terminal and run
flutter doctor
to check if any additional dependencies need to be installed. - Add Flutter/Dart Extensions to your IDE In Visual Studio Code there are extensions for Flutter/Dart that will add many benefits and provide a UI option for running, restarting and reloading your app without recompiling it. There are many other useful extensions that will speed up your development
(this is already done for you) To create a new Flutter project, run the following command in your terminal:
flutter create my_flutter_app
Replace my_flutter_app with your desired project name.
This Flutter project is structured into several key folders:
main.dart: The entry point of the app.
For more specifics on these folder structures, refer to the document located in the corresponding folder.
In Flutter, everything is a widget. The widget tree represents the hierarchical order of widgets in the app. Widgets can be stateful (with mutable state) or stateless (immutable).
Although in Flutter there is a very large built in Widget Library that you can use to build almost any sort of user interface, we also have access to an exhaustive library of online packages. Here you will find all the third party packages created by other developers for Dart and Flutter. It provides a rating system to help you make decisions about the most maintained and well documented packages.
To make things even easier, the Flutter team has a youtube channel which quickly highlights the use of different widgets and packages
When loading in a new project from Github or any other source. The first step you will want to take is to load these packages into your local machine. To do this we can take advantage of our Flutter extension by selecting the pubspec.yaml file, then saving any changes to it or by using the command
flutter pub get
In this project you will see a .vscode folder. It contains settings about how Visual Studio Code should behave in certain scenarios. The settings allow for autoformatting code on saves, quick-fixing errors on save and organising imports on save.
These settings will speed up development, One thing to note though is due to the quick-fixing on save, Flutter's quick-fix will add const to any declared object or assigned a fixed value to save on performance.
While this is a useful feature, if you would like to go back to change this to a rely on a variable state. You will need to remove this const to prevent an error showing
The main.dart file is the starting point of a Flutter app. It contains the main() function and the root widget, typically a MaterialApp or CupertinoApp.
Example: Creating a Simple Stateless Widget
import 'package:flutter/material.dart';
class MySimpleWidget extends StatelessWidget {
final String title;
MySimpleWidget({required this.title});
@override
Widget build(BuildContext context) {
return Container(
child: Text(title)
,);
}
}
Example: Creating a row of widgets using a layout widget that accepts a List as its parameter
There are also layout widgets which will help for displaying more components in a row or column.
import 'package:flutter/material.dart';
class MySimpleRowWidget extends StatelessWidget {
MySimpleRowWidget();
@override
Widget build(BuildContext context) {
return Row(
children: [
Container(),
Container()
]);
}
}
Dart uses null safety to avoid null reference errors. When declaring variables, use ? for nullable types and required for named parameters in constructors.
To add assets like images, edit the pubspec.yaml file:
flutter:
assets:
- assets/images/my_image.png
Then, use the asset in your widget:
Image.asset('assets/images/my_image.png', fit: BoxFit.cover)
To run the app, either use the play button on the extension interface (In VSCode this is visible while the main.dart is selected) OR use the following command:
flutter run
- Flutter Documentation: Official Flutter documentation, covering everything from setup to advanced topics.
- Dart Language Tour: A comprehensive guide to the Dart programming language, its features, and syntax.
- Flutter Widget Catalog: A catalog of Flutter widgets, providing an overview and examples of usage.
- Flutter API Reference: The detailed API reference for Flutter, including classes, functions, and libraries.