Unit III - Chapter 6 - Using Common Widgets
Unit III - Chapter 6 - Using Common Widgets
2. How to nest the Column and Row widgets together to create different UI
layouts.
4. How to use text field widgets to retrieve, validate, and manipulate data
1. Scaffold
Leading Icon
App Bar
Body
Bottom Navigation
2. AppBar
An AppBar appears at the top of the screen in a Flutter app. It helps you
show information like the screen title, buttons for actions, and even a
navigation icon (like a back arrow).
Title:
The title is the text that we see in the center of the AppBar. It's a simple
Text widget. You can also replace it with other widgets, like a
DropdownButton if you need more interactive options.
Leading:
The leading property shows something on the left side, before the title.
It’s often an icon, like a back button or a menu button (like the three-line
"hamburger" icon for a navigation drawer). If you don’t define leading,
Flutter automatically shows a back arrow when there’s a previous
screen.
Actions:
The actions are a list of buttons or icons that appear beside the title.
These are usually interactive, such as search buttons, menu options, or
settings. You can have multiple icons or buttons here for different
actions.
FlexibleSpace:
FlexibleSpace in Flutter is a special area behind the AppBar that you can use to
add background elements, like images or other widgets, to enhance the visual
design of your app.
Think of it as a background layer for the AppBar, where you can place things
like:
4. Container
A Container acts like a box that can hold one child widget, and you can
change its appearance or position by applying various properties:
- Add padding (space inside the container) or margin (space outside the
container).
6. RichText
The RichText widget in Flutter allows you to display text with multiple
styles in the same block of text. It’s useful when you want to apply
different formatting (like bold, color, size) to different parts of a
sentence or paragraph.
Normally, with the Text widget, we can only apply one style to the entire
text. But with RichText, we can apply different styles to different parts of
the text, all within a single widget. This is done using TextSpans (text
spans), which lets us break up the text and style each section
individually.
7. Column
8. Row
RaisedButton:
Icon Button:
PopupMenu Button:
How It Works:
Displaying Menu Items:
o The menu items are created as a list of objects (like options with
text and icons).
o When the user taps the PopupMenuButton, the menu appears.
Selecting an Item:
o When an item is selected, its value is passed to the onSelected
property to handle what happens next.
Default Icon:
o If you don't specify an icon, it will show a default three-dot menu
icon.
ButtonBar:
Key Features:
Alignment: Buttons can be aligned to the start, center, or end of the ButtonBar.
Spacing: You can set the space between buttons using the buttonPadding property.
Properties:
1. children: A list of widgets (typically buttons) that will be displayed in the ButtonBar.
2. alignment: Controls the alignment of the buttons within the ButtonBar. It can be set to:
o MainAxisAlignment.start (left alignment)
o MainAxisAlignment.center (center alignment)
o MainAxisAlignment.end (right alignment)
o MainAxisAlignment.spaceEvenly, spaceBetween, spaceAround (to adjust
the spacing between buttons).
3. buttonPadding: Adds padding between each button to control the spacing between them.
Example Program:
import 'package:flutter/material.dart';
AssetBundle is a class in Flutter that allows you to access and manage files that
are bundled with your app. These files can include images, fonts, JSON data, or
other assets that your app might need to work with.
In Flutter, assets are files that are bundled with the app and are included
during the build process. For example, you might include images that are used
within your app interface or configuration files like JSON that define app
settings.
How it Works:
You declare your assets in the pubspec.yaml file of your Flutter project.
The AssetBundle provides methods like load() to retrieve these assets
when needed.
Before using assets in your app, you need to declare them in the pubspec.yaml
file. This ensures that the assets are bundled with your app during the build
process.
Example:
Image Widget:
Icon Widget:
The Icon widget in Flutter is used to display a graphical icon, usually as part of a
button, app bar, or within any UI component where you want to represent an
action or concept with a visual symbol.
Icons in Flutter come from the Material Design Icons library, and the Icon
widget is part of the flutter/material.dart package. You can use built-in icons or
customize the icon’s size, color, and style.
Decorators:
Input Decoration:
Using of formKey:
Helps us to keep track of the form's state and lets you control what happens
inside the form, such as checking if all fields are valid or saving the data.
What is a formKey?
The formKey is a unique identifier for a form in Flutter. It’s created using a
GlobalKey<FormState> object, which can be used to manage and control what
happens in the form.
When you have a form with multiple fields, such as text fields for name, email,
or phone number, it’s important to know:
1. If all fields are filled out correctly (for example, the email field contains
a valid email).
2. When to save or submit the form after validation.
Usage:
Assign formKey to the Form widget: Inside the Form widget, set the key
property to this formKey.
Use formKey to validate or save: When the user clicks a submit button, we
can use the formKey to validate all fields and save the data.
2. Saving Data: The formKey lets to save data from all fields in one go with
formKey.currentState.save(). This is particularly useful for complex
forms, allowing us to gather all input values and store them or process
them further.
3. Accessing Form State: The formKey gives access to the form’s internal
state (like knowing if a form is valid, or if data should be saved).
When creating a mobile app, adapting the layout based on the device’s
orientation, portrait (vertical) or landscape (horizontal), can improve usability.
There are two main ways in Flutter to detect and adapt to the orientation:
In the following example, we’ll build a widget that displays different layouts
based on orientation:
MediaQuery.
A Flutter tool that provides information about the device's display and its
current settings, such as screen size, pixel density, and—important for this
case—the orientation (portrait or landscape). Here’s how it works:
MediaQuery: This is like a "context inspector" that tells us about the
device’s current layout information.