06 Adding Interactivity and Assets To Your Flutter App
06 Adding Interactivity and Assets To Your Flutter App
Learning Outcomes
After completing this lesson you should be able to explain
When the widget’s state changes, the state object calls setState(), telling
the framework to redraw the widget
Creating a stateful widget
A stateful widget is implemented by two classes
a subclass of State
The state class contains the widget’s mutable state and the widget’s build()
method
Creating a stateful widget
Let us see how to build a stateful widget, called FavoriteWidget
In this example, toggling the star is an isolated action that doesn’t affect
the parent widget or the rest of the UI
@override
_FavoriteWidgetState createState() =>
_FavoriteWidgetState();
}
Creating a stateful widget
Step 3: Subclass State
The _FavoriteWidgetState class stores the mutable data that can change
over the lifetime of the widget
Creating a stateful widget
Step 3: Subclass State
Both?
Another object?
Managing state
Keep in mind the following principles while deciding which approach to use
The parent should manage the state If the state in question is user data,
for example the checked or unchecked mode of a checkbox, or the
position of a slider
The widget itself should manage the state If the state in question is
aesthetic, for example an animation
@override
Widget build(BuildContext context) {
return IconButton(
iconSize: 100.0,
icon: _favorite ? Icon(Icons.star) : Icon(Icons.star_border),
onPressed: _handlePress,
);
}
The widget manages its own state
The _handlePress method
_handlePress() {
setState(() {
_favorite = !_favorite;
});
}
The parent widget manages the widget’s state
The parent stateful widget class
@override
Widget build(BuildContext context) {
return ToggleFavorite(
favorite: _favorite,
onChanged: _handleToggle,
);
}
Notice the highlighted child widget
The state and a callback function are passed as a constructor
argument to the child widget ToggleFavorite
The parent widget manages the widget’s state
The _handleToggle method
_handleToggle(bool newValue) {
setState(() {
_favorite = newValue;
});
}
_handlePress() {
onChanged(!favorite);
}
Exercise:
Add a color state for the icon which changes together with the icon types
configuration files,
icons, and
images (JPEG, WebP, GIF, animated WebP/GIF, PNG, BMP, and WBMP)
Specifying assets
Flutter uses the pubspec.yaml file, located at the
root of your project, to identify assets required by
an app
flutter:
assets:
- assets/my_icon.png
- assets/background.png
Specifying assets
To include all assets under a directory, specify the
directory name with the / character at the end:
flutter:
Assets:
- directory/
- directory/subdirectory/
Asset bundling
The assets subsection of the flutter section specifies files that should be
included with the app
During a build, Flutter places assets into a special archive called the asset
bundle that apps read from at runtime
Asset variants
Refers to different versions of an asset that might be displayed in different
contexts
.../pubspec.yaml
.../graphics/my_icon.png
.../graphics/background.png
.../graphics/dark/background.png
...etc
Asset variants
And your pubspec.yaml file contains the following
flutter:
assets:
Main Asset
- graphics/background.png
Variant
Asset variants
If your pubspec.yaml file contains the following
flutter:
assets:
- graphics/
Then the graphics/my_icon.png, graphics/background.png and
graphics/dark/background.png files are included in the asset bundle
Loading assets
Your app can access its assets through an AssetBundle object
Below are the two main methods that are available in AssetBundle for
loading string/text asset or an image/binary asset
loadString()
load()
You can us a logical key to access the assets
The logical key maps to the path to the asset specified in the pubspec.yaml
file at build time
Loading text assets
Each Flutter app has a rootBundle object for easy access to the main asset
bundle
It is possible to load assets directly using the rootBundle global static from
package:flutter/services.dart