Chapter 2
Chapter 2
Chapter 2
Basic flutter widgets
Topics
Create a Simple Flutter Application
Run a Simple Flutter Application
Structure of the application
Flutter Architecture
Most Common Widgets
Understanding Flutter hot reload and hot restart
Stateful and Stateless Widget
Use a custom Font
Create a Restaurant Menu
Create a Simple Flutter Application
create a simple Flutter application to
understand the basics of creating a flutter
application in the Android Studio.
Step 1: Open Android Studio
Step 2: click on New Flutter Project from the
welcome screen or click on New and then
New Flutter Project from the File menu.
Android Studio will open the New Project
dialog.
Next
• Android Studio then prompts you
for the path to your Flutter SDK
directory.
• Verify the path is correct and then
hit the Next button.
Next
• The next step is to enter your
project name.
• In the New Project box, enter the
project name. No other changes
are required. Click on Create.
Next
• Android Studio creates the project
and prompts the user to open it.
Running Flutter Application
• Open your Flutter project in Android Studio.
• Click the green Run button (or use the shortcut Shift +
F10).
Running Flutter
Application
• Android Studio will build the app and install it on
the emulator. Once completed, the app will
launch on the emulator screen.
• Text Styling:
• TextStyle:
• Customize font size, weight, color, and style.
• Color:
• Change text color.
• Theme:
• Apply consistent styling across the app.
• Alignment:
• TextAlign:
• Align text (left, right, center).
• TextDirection:
• Set text direction (LTR or RTL).
Example – Text Widget
Example – Rich Text Widget
RichText(
text: TextSpan(
text: 'Hello ',
style: DefaultTextStyle.of(context).style,
children: const <TextSpan>[
TextSpan(text: 'bold', style: TextStyle(fontWeight:
FontWeight.bold)),
TextSpan(text: ' world!'),
],
),
)
Structure and Layout
• Basic Layout Widgets:
• Scaffold: Provides a basic layout structure for the app, including app bars, drawers, bottom navigation, and floating action
buttons.
• Row: Arranges children in a horizontal line.
• Column: Arranges children in a vertical line.
• Container: A box model widget for styling, padding, and positioning.
• Card: is used to create a material design card.
• Complex Layouts:
• Stack: Overlays children on top of each other.
• Expanded: Expands a child to fill available space in a Row or Column.
• Safe Area:
• SafeArea: Ensures widgets are not obscured by system UI elements (like notches and status bars).
Example - Scaffold Widget
• A Scaffold Widget provides a framework which
implements the basic material design visual
layout structure of the flutter app .The following
constructor parameters of the widget Scaffold.
1.appBar(It displays a horizontal bar which
mainly placed at the top of the Scaffold.)
2.Body (body: It will display the main or primary
content in the Scaffold. It is below the appBar )
3.Floating Action Button
4.Bottom Navigation Bar
5.More
Scaffold Widget
Example - Container
Example – Row Widget
Example – Column Widget
Example – Align Widget
Example – Stack Widget
Example – Card Widget
Image and Media Widgets
• Displaying Images:
• Image: Displays images from assets, network, or memory.
• Icon: Displays material design icons.
• Handling Media:
• VideoPlayer: Used for playing video content.
Image Widget
1.Image.asset - To display image from
assets bundle
2.Image.file - To display image from a file
3.Image.memory - To display image from
Uint8List
4.Image.network - To display image from
a URL
Displaying an Image From Assets
Add the image file to your project's assets folder.
Declare the asset in the pubspec.yaml file.
• flutter:
assets:
- assets/images/sample.png