Chapter 05 Building Flutter UI with widgets - 1
Chapter 05 Building Flutter UI with widgets - 1
WITH WIDGETS
I CT425 MOBILE APPLICATION DEVELOPMENT
PRASHAYA FUSI RIPONG (PH.D.)
1.Text widget
2.Icon widget
3.Layout
OUTLINE
4.Image widget
5.Input widget
6.Button widget family
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
),
INPUT WIDGET
• Flutter framework provides widgets for entering data (Input widget)
• Input widgets are safer and give much control
• Input widgets do not maintain own state; we have to do it manually
• Input widgets don’t play well together until you group them with a Form widget
• Input widgets is to ‘text fields’, ‘checkbox’, ‘radio buttons’, ‘sliders’, and
‘dropdowns’
TEXTFIELDS WIDGET
• Flutter app probably want a TextField widget waiting the data from users entry
• TextField widget should have a Text label to represent the data meaning:
For example:
const Text(“Search terms”),
TextField(
onChanged: (String val) => _searchTerm = val,
)
• The ‘onChanged’ property is an event handler that fires after every keystroke
TextField widget will receive a single value – a “String” (the value come from user is
typing)
TEXTFIELDS WIDGET
• ‘TextFields’ widget as fancy by using the ‘InputDecoration’ widget
For example:
return TextField(
decoration: InputDecoration(
labelText: ‘Email’,
hintText: ‘[email protected]’,
icon: Icon(Icons.contact_mail),
),
),
TEXTFIELDS WIDGET
• “InputDecoration” widget provides many properties for
decorating the input widget as follow below:
Property Description
labelText appears above the TextField. Tells the user what this TextField is for
hintText light ghost text inside the TextField. disappears as the user begins typing
errorText error message that appears below the TextField. usually in red. It is set
automatically by validation (covered later)
prefixText Text in the TextField to the left of the stuff the user types in
suffixText same as prefixText but to the far right
Icon draws an icon to the left of the entire TextField
prefixIcon draws one inside the TextField to the left
suffixIcon same as prefixIcon but to the far right
TEXTFIELDS WIDGET
• ‘TextFields’ widget as fancy by using the ‘InputDecoration’ widget
For example:
return TextField(
decoration: InputDecoration(
labelText: ‘Password’,
hintText: ‘Password’,
icon: Icon(Icons.password),
),
),
TEXTFIELDS WIDGET
• ‘TextFields’ widget as fancy by using the ‘InputDecoration’ widget
For example:
return TextField(
keyboardType: TextInputType.number
decoration: InputDecoration(
labelText: ‘Phone number’,
hintText: “000 458 8524”,
icon: Icon(Icons.phone),
),
),
CHECKBOXES WIDGET
• ‘Checkboxes’ widget have a Boolean value property and an ‘onChanged’ method
with fires after every change.
• The ‘onChanged’ method receives the value that user set
• ‘Checkboxes’ value is a bool as result
• For example:
checkbox(
value: true,
onChanged: (bool val) => print(val)
),
• A flutter ‘switch’ widget serve the same purpose as a ‘checkbox’ widget
RADIO WIDGET
• The ‘radio button’ widget can be selected one, the others
in the same group are deselected within the same group.
So flutter framework provide the ‘RadioListTile’ widget to create
the group of radio button
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
BUTTON WIDGET FAMILY
• Buttons are material components that provide the user a single tap facility for taking
actions, making choices, submitting forms, saving data, opening a new page, etc
• Buttons are triggered when the user taps on them
• Special features of a button:
Easy addition of different child widgets for different purposes
Easy theming of the button
Easy addition of themed text and icons
Provide action functionalities
• Types of buttons
TextButton
ElevatedButton
OutlinedButton
IconButton
TEXTBUTTON WIDGET
• Text Button Class in flutter are material component button widgets with no border by default
• Text button is a regular button with some text written as the label
• Parameters must be required:
child: button’s label
For example:
child: const Text(“Text Button”),