Programming models: Flutter?
Last updated
Last updated
(By Sergio Guzmán)
This is based on the .
Flutter has one core principle: everything is a widget. There is a readily available widget for pretty much everything you will use in your app, including:
Stylistic elements like fonts and colors
Layout elements like margins and paddings
Graphic components like buttons, lists, images, menus, etc
And... animations, user interaction, etc
Lets take a look at a really simple example using the MaterialDesign dart library (just to have a runApp
method implemented). This is our main
method, that just renders a text:
We can encapsulate our text inside a layout widget (like MaterialApp for instance) to recognize that we have an available space on the screen and thusly change the style of the component:
Now let´s make it more beautiful by using a default Scaffold widget from the material design lib and adding a FloatingActionButton of course.
Finally, we´ll give it some layout order too by centering the text in a Container:
The downside of using widgets is the nesting hell generated by encapsulating a widget, inside another widget, inside another widget, and so on... To make your code look a little bit nicer during this "widget composition", class-oriented programming comes to the rescue
There are two types of widgets you should look out for: StatelessWidget and StatefulWidget. When you define your own widgets, you will extend from one or the other, depending on your needs.
You want something static? The StatelessWidget is meant for static things, like text, images, fonts, paddings and so on.
Your screen will be changing over time? Go with Stateful
The StatefulWidget will likely be calling the setState method to repaint the widget tree, depending on the state of your widget. These widgets are used a lot for handling user interaction.
When creating a custom widget, always override the build method, which returns a widget tree to be painted in your app.
So how would this look for our app? Let´s put the body of the MaterialApp widget inside a StatefulWidget and add a counter for the state.
If you notice, whenever we press the FAB button a new funtion is called (_incrementCounter
), that modifies the counter variable.
State management can be tricky, and that´s why we have a whole new section dedicated for this coming up!
Much like in JavaScript, Flutter has an implementation for creating asynchronous tasks and waiting for them: a future. A future is the result of an asynch task that has two possible states: uncompleted (the task is still in progress) and completed (the task ended). For a future to be fulfilled there are two possibilities:
Completed with a value: Future<T>
will return a value of type T
Completed with error
Notice the use of await
to wait for the result of a future and async
to declare a function that will have awaits
in its code. Without these two, when you call a future your code won´t wait for a result.
Also... for error handling use a try-catch
Futures are important when doing blocking operations (I/O mostly) and making requests to external services (APIs)
So yeah,everything is handled with a widget. Look at this to get an idea of all the widgets you can use with Flutter.
Nested hell is real, it's the reason why some devs dislike Flutter. Take a look at , and to get an idea about it
Here´s an example from :