The below code is the main entry point of this Flutter app. as you learned before, main() method is the entry point of dart programming language. and It’s same with Java programming language.
1 2 3 4 5
// main.dart
void main() { runApp(const MyApp()); }
MyApp class inherit StatelessWidget. What i am saying is MyApp is the root Widget in our Flutter app. and Flutter concept is all of components of flutter are consisted of Widgets. and Widget is represented by a class syntax of Dart language.
Scaffold widget is going to allow us to implement a basic layout for our app. otherwords, Scaffold widget basically like a wrapper to a few different layout widgets things like the app bar, the body and also floating action button.
let’s create new folder named ‘fonts’ in your app root directory and please move your font files downloaded from fonts.google.com site in there.
pubspec.yaml file is a configuration file about your flutter app. You should know one thing that the format in this file is really important.
if you don’t keep the two spaces between each code then it will be not operated well.
7. Stateless Widgets & Hot Reload
We should consider when we create new custom widget whether is stateless widget or stateful widget.
Stateless Widgets
the state of the widget cannot change over time
ex) Layout, Color
Stateful Widgets
the state of the widget can change over time
ex) data
the one of the reason why the widgets are separated as Stateless widget or Stateful widget is hot reload.
The flutter should catch the change of the widgets for implementing hot-reload function. so that defining whether your widget is stateful or not is needed.
The build() function in StatelessWidget is what is responsible for building up the widget tree inside the stateless home widgets so all of the stuff in build() function whenever we make a change to the code inside this widget tree flutter is going to detect that when we save it and it’s going to cause the build() function to rerun. that’s is hot reload in action.
so if you create the widget inherited Stateless thing then the flutter will re run build() function when you save your code.
8. Images & Assets
The principle of Assets is same with font. You should change the pubspec.yaml file in your flutter app.
the container is only the same size as the widget inside it. if we don’t have a child which inside it then the container takes up the whole room available. but it has a child widget like a text widget then the container restricts itself to the size of that child widget.
if you only need a padding option then you can type the code like the below. but you should know Padding widget doesn’t have another properties like margins, colors and so on.
Padding widget is a padding itself. so You should don’t be misunderstanding. if you need to use all of the properties then you can use Container widget as you did it.
we stil have second and third containers the same size which is the size of the content inside it but the first container now because it’s inside the expanded widget over here it’s taking up all the available space left over.
flex property in Expanded widget is a portion of the width that we want it to take up. they’re basically fractions of the whole width that they take up.
setState() function takes in as an argument a function itself. this is a trigger of the build function sp then it rebuilt it with the new state. whenever we want to change the state or the data inside a state for Widget what we have to do is use setState() function. this is only way when we use set state that triggers the build function to rerun.
List<String> quotes = [ 'Be yourself; everyone else is already taken', 'I have nothing to declare except my genius', 'The truth is rarely pure and never simple' ];
Navigator.pushNamed() is named because we’re going to supply a named round we’re going to use the name of one o the routes to that router and then it’s called push because essentially what we do is push another screen on top of the screen. the screen is still going to exist underneath it’s just that we’re pushing another one on top of it.
It is important concept to understand and we are going to be doing more routing as we go through the rest of this course so that we can see different ways to push and pop routes on and off this kind of stack and manage our routes in an efficient way.
24. Widget Lifecycle
Stateless
State does not change over time
build function only runs once
Stateful
State Can change over time
setState() triggers the build function
initState()
called only once when the widget is created
subscribe to streams or any object that could change our widget data
// simulate network request for a username String username = await Future.delayed(Duration(seconds: 3), () { return'yoshi'; });
// simulate network request to get bio of the username String bio = await Future.delayed(Duration(seconds: 2), () { return'vegan, musician & egg collector'; });
the funcion with async is going to take some time to do it might take two seconds today because we have to go out and reach the data and bring it back.
you should remember if we run an asynchronous function this doesn’t stop the code from carrying on. it just does this in the background and then the rest of the code carries on.
to solve what it would be nice to do is maybe put await keyword in front of an asynchronous function. then we’re waiting for this to finish before it carries on.
for that we should return specific type that we need in an asynchronous function if we want to use the await keyword in front of a cuson aynchronous function we have to place the Future keyword with return type.
String location; // location name for the UI lateString time; // the time in that location String flag; // url to an asset flag icon String url; // location url for api endpoint