Flutter is an open-source UI toolkit from Google for crafting beautiful, natively compiled applications for desktop, web, and mobile from a single codebase. Flutter apps are built using the Dart programming language.
There are two types of widgets:
- StatelessWidget : A widget that does not require a mutable state.
- StatefulWidget: A widget that has a mutable state.
- The Flutter tooling supports three modes when compiling your app, and a headless mode for testing.
- You choose a compilation mode depending on where you are in the development cycle.
- The modes are: - Debug - Profile - Release
Dart is an object-oriented, garbage-collected programming language that you use to develop Flutter apps. It was also created by Google, but is open-source, and has community inside and outside Google. Dart was chosen as the language of Flutter for the following reason:
- Dart is AOT (Ahead Of Time) compiled to fast, predictable, native code, which allows almost all of Flutter to be written in Dart. This not only makes Flutter fast, virtually everything (including all the widgets) can be customized.
- Dart can also be JIT (Just In Time) compiled for exceptionally fast development cycles and game-changing workflow (including Flutter’s popular sub-second stateful hot reload).
- Dart allows Flutter to avoid the need for a separate declarative layout language like JSX or XML, or separate visual interface builders, because Dart’s declarative, programmatic layout is easy to read and visualize. And with all the layout in one language and in one place, it is easy for Flutter to provide advanced tooling that makes layout a snap.
- Widgets are basically the UI components in Flutter.
- It is a way to describe the configuration of an Element.
- They are inspired from components in React.
Widgets are important in Flutter because everything within a Flutter application is a Widget , from a simple “Text” to “Buttons” to “Screen Layouts”.
- State that is not ephemeral, that you want to share across many parts of your app, and that you want to keep between user sessions, is what we call application state (sometimes also called shared state).
- Examples of application state: - User preferences - Login info - Notifications in a social networking app - The shopping cart in an e-commerce app - Read/unread state of articles in a news app
main ()
function came from Java-like languages so it's where all program started, without it, you can't write any program on Flutter even without UI.runApp()
function should return Widget that would be attached to the screen as a root of the Widget Tree that will be rendered.
Stateless : Widget state creates ONLY ONCE, then it can update values but not state explicitly. That's why it has only one class which extends with StatelessWidget
. They can never re-run build()
method again.
Stateful : Widgets can update their STATE (locally) & values multiple times upon event triggered. That's the reason, the implementation is also different. In this, we have 2 classes, one is StatefulWidget
& the other is it's State implementation handler i.e. State<YourWidget>
. So if I say, they can re-run build()
method again & again based on events triggered.
- A
StatelessWidget
will never rebuild by itself (but can from external events). AStatefulWidget
can. - A
StatelessWidget
is static wheres aStatefulWidget
is dynamic.
See the diagram below:
Hot Reload
- Flutter hot reload features works with combination of Small r key on command prompt or Terminal.
- Hot reload feature quickly compile the newly added code in our file and sent the code to Dart Virtual Machine. After done updating the Code Dart Virtual Machine update the app UI with widgets.
- Hot Reload takes less time then Hot restart.
- There is also a draw back in Hot Reload, If you are using States in your application then Hot Reload preservers the States so they will not update on Hot Reload our set to their default values.
Hot Restart
- Hot restart is much different than hot reload.
- In Hot restart it destroys the preserves State value and set them to their default. So if you are using States value in your application then after every hot restart the developer gets fully compiled application and all the states will be set to their defaults.
- The app widget tree is completely rebuilt with new typed code.
- Hot Restart takes much higher time than Hot reload.
- A
Key
is an identifier forWidgets
,Elements
andSemanticsNodes
. - A new widget will only be used to update an existing element if its key is the same as the key of the current widget associated with the element.
- Keys must be unique amongst the Elements with the same parent.
- Subclasses of
Key
should either subclassLocalKey
orGlobalKey
. - Keys are useful when manipulating collections of widgets of the same type.
- If you find yourself adding, removing, or reordering a collection of widgets of the same type that hold some state, then, you should use a key.