Step4 Write initial counter app from scratch
Goal of this step
- Write initial
lib/main.dart
code from scratch. - Understand initial code by writing.
Flutter from web developers
https://flutter.dev/docs/get-started/flutter-for/web-devs
Before we start writing code, let's check this table.
This is a table to compare Web world and Flutter world.
The purpose of this table is to use knowledge you already have to learn new thing.
Web | Flutter | What you need to learn |
---|---|---|
<div> , <table> | Container() ,Table() | Flutter Widgets, Flutter Widget Livebook |
<div style="font-size: 24px;"> | Text(style: TextStyle(fontSize: 24)) | Flutter for web developers |
Bootstrap | package:flutter/material.dart | Material Components widgets, Official Gallery, Flutter Widget Livebook |
javascript | dart | dart language tour |
- At first you may feel flutter code is difficult.
- But to see this table you can find the similarity of web and flutter
- I hope this table will reduce the difficulty of learning flutter!
- You need a time to get used to flutter code as it took time to remember HTML, CSS, Bootstrap and Javascript!
Let's get started!
This is initial lib/main.dart
code.
From now on, we will make this file step by step.
lib/main.dart
to lib/initial_main.dart
.
Rename I leave initial file for reference. There are a lot of useful comments.
And make empty lib/main.dart
.
Step 1 Hello World
main()
function
Step1-1 dart Every app must have a top-level main() function, which serves as the entrypoint to the app. The main() function returns void
https://dart.dev/guides/language/language-tour#the-main-function
Step1-2 StatelessWidget
- Make
MyApp
StatelessWidget class by VS code auto complete - Add
Text()
(Check Container() haschild
by hovering)
https://flutter.dev/docs/development/ui/widgets-intro#hello-world
If you are using a material widget component, like Scaffold, Material, you don't need to specify textDirection in your Text widget...
https://stackoverflow.com/questions/56122888/flutter-no-directionality-widget-found
MaterialApp()
Step2 Use MaterialApp()
:
https://api.flutter.dev/flutter/material/MaterialApp-class.html
As I mentioned at first table, material package is something like Bootstrap.
You can check widgets catalog here https://flutter.dev/docs/development/ui/widgets/material
theme
:
You can choose your app's primary color like this.
title
:
I think title
property is almost useless in mobile
https://stackoverflow.com/questions/50615006/flutter-where-is-the-title-of-material-app-used
MyHomePage
Step3 Make StatefulWidget: In this step, let's extract this Scaffold
part to another class.
- Make StatefulWidget by VS code auto completion
- Pass
MyHomePage()
tohome:
If you want to dive into stateful widget syntax, read the below links.
But I think it is ok not to understand perfectly right now. Just following code auto completion is enough.
- https://api.flutter.dev/flutter/widgets/StatefulWidget-class.html
- *Why are stateful widgets defined as two classes in flutter?
MyHomePage
Step4 Pass props to In this step, we are going to learn
- how to pass down props to
MyHomePage
final
Once assigned a value, a final variable's value cannot be changed
What is the difference between the “const” and “final” keywords in Dart?
Please focus on the flow now!
But If you want to dive into these code, check the below links!
- What are Keys in the Stateless widgets class?
- You can skip to declare keys but...
- Stakoverflow question about super
setState()
and Layout
Step5 In this step, we are going to learn
- How to change state by
setState()
- How to use
Row
andColumn
- Declare initial State
- Edit
floatingActionButton
- Learn how to change state by using
setState()
- Show state in body
Row
andColumn
_
underscore variable in dart
It's not just a naming convention. Underscore fields, classes and methods will only be available in the .dart file where they are defined.
What does Underscore “_” before variable name mean for Flutter
Privacy in Dart exists at the library, rather than the class level.
https://stackoverflow.com/questions/17488611/how-to-create-private-variables-in-dart/17488825
Row and Column
Image from: https://flutter.dev/docs/development/ui/layout#lay-out-multiple-widgets-vertically-and-horizontally
Align of Row and Column
Image from: https://flutter.dev/docs/development/ui/layout#aligning-widgets
String interpolation
$variableName
(or ${expression}
)
For example
https://dart.dev/guides/language/language-tour#strings
Theme
You can access theme by using
//https://flutter.dev/docs/cookbook/design/themes#using-a-theme
textTheme
https://api.flutter.dev/flutter/material/TextTheme-class.html
Finish!
Okay, we successfully re-created initial counter app!
I hope you've learned the fundamental of flutter!