Professional Documents
Culture Documents
Lecture 6 - Interaction Interfaces in Flutter - ١١٥١٥٩
Lecture 6 - Interaction Interfaces in Flutter - ١١٥١٥٩
Interfaces in Flutter
T. Fatima Al-azazi
Agenda
2
The Form widget
5
Form Example
6
VALIDATE TEXT FIELDS USING THE FORM WIDGET
7
Form Example 2
8
Form with Children 1
3
4
9
State Class Data Model
State Class
Form Key
10
SubmitOrder Method
11
12
Standard and Material Interactive Widgets
Material Components
● Checkbox
● DropdownButton
Standard widgets ● TextButton
● Form ● FloatingActionButton
● FormField ● IconButton
● Radio
● ElevatedButton
● Slider
● Switch
● TextField
13
GestureDetector
14
Adding Interactivity Using Gestures
● Gestures are any kind of interaction event: taps, drags, pans, and more
● In a mobile application, gestures are the heart of listening to user
interaction.
● Making use of gestures can define an app with a great UX.
● Overusing gestures when they don’t add value or convey an action
creates a poor UX.
● Flutter allows you to add gesture detectors to any location of the
widget tree.
● All the button widgets that have onPressed and onTap are just
convenient wrappers around gesture detectors.
15
The GestureDetector widget
16
Gestures That You Can Listen for
17
Gesture Arguments and Callbacks
● All of these arguments will call the callback you pass, and some will
pass back “details.”
● onTapUp, for example, passes back an instance of the TapUpDetails
object, which exposes the globalPosition, or location on the screen
that was tapped.
● Some of the more complicated gestures, like the drag-related ones,
pass back more interesting properties.
● You can get details about the time a drag started, the position where
it started, the position where it ended, and the velocity of the drag.
● This lets you manipulate drags based on direction, speed, and so on.
18
19
SETTING UP GESTUREDETECTOR
20
Incorrect GestureDetector Arguments
● Using vertical drag, horizontal drag, and pan gestures at the same time,
causes an Incorrect GestureDetector Arguments error.
● However, if you use either vertical or horizontal drag with a pan
gesture, you’ll not receive any errors.
● The reason you receive the error is that simultaneously having a
vertical and horizontal drag gesture and a pan gesture results in the
pan gesture being ignored since the vertical and horizontal drag will
first catch all of the drags.
● You’ll get the same kind of error if you try to use vertical drag,
horizontal drag, and scale gestures at the same time.
21
DRAGGABLE AND DRAGTARGET
WIDGETS
22
DRAGGABLE AND DRAGTARGET WIDGETS
23
DRAGGABLE AND DRAGTARGET WIDGETS
● Once the user lifts their finger on top of the DragTarget, the target can
accept the data.
● To reject accepting the data, the user moves away from the DragTarget
without releasing touch.
● If you need to restrict the dragging vertically or horizontally, you
optionally set the Draggable axis property.
● To catch a single axis drag, you set the axis property to either
Axis.vertical or Axis.horizontal.
24
DRAGTARGET WIDGET
● The DragTarget widget listens for a Draggable widget and receives data
if dropped.
● The DragTarget builder property accepts three parameters: the
BuildContext, List acceptedData (candidateData), and List of
rejectedData.
● The acceptedData is the data passed from the Draggable widget, and it
expects it to be a List of values. The rejectedData contains the List of
data that will not be accepted.
25
Example Draggable
26
DragTarget
27
GESTUREDETECTOR FOR MOVING
AND SCALING
28
Gestures for Moving and Scaling
29
Transform widget
● The Transform widget applies a transformation before the child is
painted.
● Using the Transform default constructor, the transform argument is set
by using the Matrix4 (4D Matrix) class, and this transformation matrix
is applied to the child during painting.
● the default constructor uses the Matrix4 to execute multiple cascading
(..scale()..translate()) transformations.
● The double dots (..) are used to cascade multiple transformations.
● the cascade notation allows you to make a sequence of operations on
the same object.
30
The Transform widget constructors.
31
The Transform widget constructors.
35
Moving and Scaling Techniques
● You’ll take a look at two techniques to accomplish the same moving and
scaling results.
● The first technique involves nesting the scale and translate
constructors.
● The second technique uses the default constructor with the Matrix4 to
apply transformations.
36
The first technique
37
The second technique
38
39
40
onScaleStart
41
42
43
onLongPress
44
INKWELL AND INKRESPONSE
GESTURES
45
InkWell and InkResponse widgets
● Both the InkWell and InkResponse widgets are Material Components
that respond to touch gestures.
● The InkWell class extends (subclass) the InkResponse class.
● The InkResponse class extends a StatefulWidget class.
● For the InkWell, the area that responds to touch is rectangular in
shape and shows a “splash” effect—though it really looks like a ripple.
● The splash effect is clipped to the rectangular area of the widget (so as
not go outside it).
● If you need to expand the splash effect outside the rectangular area,
the InkResponse has a configurable shape.
● By default, the InkResponse shows a circular splash effect that can
expand outside its shape (Figure 11.3). 46
● The following are the InkWell and
InkResponse gestures that you can
listen for action.
● The gestures captured are taps on the
screen except for the
onHighlightChanged property, which is
called when part of the material starts or
stops being highlighted.
● The main benefits of using the InkWell
and InkResponse are to capture taps on
the screen and have a beautiful splash.
This kind of reaction makes for a good
UX, correlating an animation to a user’s
action. 47
Properties
48
49
50
HOW IT WORKS
● Both the InkWell and InkResponse widgets listen to the same gesture
callbacks.
● The widgets capture the onTap, onDoubleTap, and onLongPress
gestures (properties).
● When a single tap is captured, the onTap calls the _setScaleSmall()
method to scale the image to half the original size.
● When a double tap is captured, the onDoubleTap calls the
_setScaleBig() method to scale the image to 16 times the original size.
● When a long press is captured, the onLongPress calls the
_onLongPress() method to reset all values to the original positions and
sizes.
51
DISMISSIBLE WIDGET
52
USING THE DISMISSIBLE WIDGET
● The Dismissible widget is dismissed by a dragging gesture.
● The direction of the drag can be changed by using DismissDirection for
the direction property.
● The Dismissible child widget slides out of view and automatically
animates the height or width (depending on dismiss direction) down to
zero.
● This animation happens in two steps; first the Dismissible child slides
out of view, and second, the size animates down to zero.
53
onDismissed callback
54
Dismissible widget showing the swiped row
dismissed animation to complete the item
55
DismissDirection Dismiss Options
56
Example
57
Data Model
58
State class
59
60
buildRemoveTrip method
buildCompleteTrip method
61
markTripCompleted method
deleteTrip method
buildListTile method
62
63
Animation Widgets
64
ANIMATEDCONTAINER
65
ANIMATEDCONTAINER
67
USING ANIMATEDCROSSFADE
68
USING ANIMATEDOPACITY
69
Adding interactivity to your
Flutter app
70
Creating a stateful widget
71
Example
● When the app first launches, the star is solid red, indicating that this
lake has previously been favorited.
● The number next to the star indicates that 41 people have favorited
this lake.
● tapping the star removes its favorited status,
○ replacing the solid star with an outline and
○ decreasing the count.
● Tapping again favorites the lake,
○ drawing a solid star and
○ increasing the count.
72
Step 1: Decide which object manages the widget’s
state
73
Step 2: Subclass StatefulWidget
● The FavoriteWidget class manages its own state, so it overrides
createState() to create a State object.
● The framework calls createState() when it wants to build the
widget.
● In this example, createState() returns an instance of
_FavoriteWidgetState, which you’ll implement in the next step.
74
Step 3: Subclass State
75
76
The _toggleFavorite() method, which is called when the IconButton
is pressed, calls setState().
Calling setState() is critical, because this tells the framework that the
widget’s state has changed and that the widget should be redrawn.
77
Step 4: Plug the stateful widget into the widget tree
Add your custom stateful widget to the widget tree in the app’s build()
method. First, locate the code that creates the Icon and Text, and delete
it. In the same location, create the stateful widget:
78
Managing state
Who manages the stateful widget’s state? The widget itself? The parent
widget? Both? Another object? The answer is… it depends. There are several
valid ways to make your widget interactive. You, as the widget designer,
make the decision based on how you expect your widget to be used. Here
are the most common ways to manage state:
79
Decide which approach to use
How do you decide which approach to use? The following principles should
help you decide:
80
Assignment 4
● What are:
○ ANIMATIONCONTROLLER with example
○ Staggered Animations with example
● Design and build your app with Interactive Screens
81