Chapter 05 Building Flutter UI with widgets
Chapter 05 Building Flutter UI with widgets
WITH WIDGETS
ICT425 MOBILE APPLICATION DEVELOPMENT
PRASHAYA FUSIRIPONG (PH.D.)
1.Text widget
2.Icon widget
3.Image widget
OUTLINE
4.Layout
5.Input widget
6.Styling the widget
TEXT WIDGET
Save the “pubspec.yaml” file and run “flutter pub get” to have the flutter project
process the flie
IMAGE WIDGET
• Embedded images
The flutter app get the images to render on the screen by calling the “asset”
constructor
For example:- Image.asset(“assets/images/photo1.jpg”,),
Tip……
‘pubspec.yaml’ file hold all kind of great information about the flutter project.
The file holds project metadata like the name, description, repository location, and
version number.
The file holds the lists library dependencies and fonts
IMAGE WIDGET
• Network images
Network images are much more like what web developers might be
accustomed to
Network images is simply fetching an image over the Internet via http
Using ‘network’ constructor and pass in a URL as a ‘string’ type
For example:- Image.network(imageUrl),
Network images are slower than embedded image
Network images are live; any image can be loaded dynamically by simply
changing the image URL
IMAGE WIDGET
• Sizing an image
Flutter framework will shrink the image
to fit its container as called ‘BoxFit’
property
For example:
Image.asset(
‘assets/images/woman.jpg’,
fit: BoxFit.contain
),
LAYOUTS AND WIDGET HIERARCHY
• What is a Layout?
A layout in Flutter refers to how widget are arranged in relation to one another on the
screen. It involves position, spacing, and alignment to create the desired UI structure
• Widget tree:
In flutter, the user interface is built as a tree of widgets, where each widget is a node. The
parent-child relationship among widgets defines the hierarchy and layout of the UI
• Types of Layout widgets:
Single-child layout widget
These widgets can have only one child widget. Example include Container, Center, Padding
Multi-child layout widget
These widget can have multiple children. Example include Column, Row, Stack, ListView
TOP 20 MOST USED SINGLE-CHILD WIDGET IN FLUTTER
TOP 20 MOST USED MULTI-CHILD WIDGET IN FLUTTER
COMMONLY USED CONTAINER WIDGET
• A container class in flutter is a convenience widget that combines common painting,
positioning, and sizing of widgets
• A container class can be used to store one or more widgets and position them on the
screen
• A basic container element that stores a widget has a margin, border and padding
Margin:- separates the present container from other contents
Border:- Define a border of different shapes
for example, rounded rectangles
Padding:- applies additional constraints to the padded extent
(incorporating the width and height as constraints, if either is non-null)
STYLING THE WIDGETS
• Container decorations
The decoration property support the BoxDecoration class
- The BoxDecoration class provides a variety of way to draw a box
The BoxDecoration has a border
The BoxDecoration has a boxShadow
The BoxDecoration has a borderRadius
The BoxDecoration has a shape
• Border class: a border of a box comprised of four sides: top, right, bottom, and left
• BoxShadow class: cast non-rectangular shadow if the box is non-rectangular (e.g., has a border
radius or a circular shape)
• BorderRadius class: specific offset in terms of visual corners e.g., properties as topLeft, topRight and
so on
• BoxShape enum: provide the normal two values as rectangle and circle shape, however, it can
modifies to many shape rely on the ‘painter’ property
COMMONLY USED ROW AND COLUMN WIDGET
• Row and Column widgets align children horizontally and vertically as per the
requirement
• Properties of Row and Column widgets:
children: This property takes in List<Widget>, that is a list of widgets to display inside the Row or
the Column widget
crossAxisAlignment: CrossAxisAlignment enum as the object to how the children’s widgets should
be places in crossAxisAlignment. For Row it is vertical and for Column it is horizontal
mainAxisAlignment: MainAxisAlignment enum as the object to decide how the children widgets
should be place in mainAxisAlignment. For Row it is horizontal and for Column it is vertical
COMMONLY USED ROW WIDGET
• Row can create a horizontal array of children
• Alignment properties: using mainAxisAlignment and crossAxisAlignment
Row’s mainAxis is horizontal
cross Axis to Row’s main Axis is vertical
COMMONLY USED ROW WIDGET
• Row can create a vertical array of children
• Alignment properties: using mainAxisAlignment and crossAxisAlignment
Row’s mainAxis is vertical
cross Axis to Row’s main Axis is horizontal
COMMONLY USED EXPANDED WIDGET
• Expanded widget can be taken as the
child of Row, Column, and Flex
if we don’t want to give equal spaces to
our children widgets we can distribute the
available space as our will using flex
parameter
Property Description
We use this property to define various
items that are to be defined in our
items
dropdown menu/list. It is a list of items
that users can select
value Value is the currently selected item
We use elevation property to elevate
Elevation On clicking the ‘dropdownbutton’ widget,
the dropdown menu/list
This property is used to display an it open a list of items
icon
icon to the dropdown button
We use the style property to style our
style text in the dropdown menu/list like
color, fontsize, fontweight, etc
STYLING THE WIDGETS
• Stacking widget: useful if you want to overlap several children in a simple way,
For example having some text and an image, overlaid with a gradient and a button
attached to the bottom
• Positioned widget: the path from the Positioned widget to its enclosing Stack
It can identify the different location of each content (e.g. text, image, and so on)
- top, left, bottom and right is to point the location within ‘Stack’ widget
Thank you
Prashaya Fusiripong (Ph.D.)
Email: [email protected]
Department of Information and communication technology (ICT), International college