UI DESIGN-FLUTTER Lab - Week 2
UI DESIGN-FLUTTER Lab - Week 2
Lab Program 2
2. a) Explore various Flutter widgets (Text, Image, Container, etc.).
Flutter Text
A Text is a widget in Flutter that allows us to display a string of text with a
single line in our application.
We can align the text widget by using textAlign property, and style
property allow the customization of Text that includes font, font weight,
font style, letter spacing, color, and many more.
Depending on the layout constraints, we can break the string across
multiple lines or might all be displayed on the same line.
If we do not specify any styling to the text widget, it will use the
closest DefaultTextStyle class style. This class does not have any explicit
style.
We can use it as like below code snippets.
1. new Text(
2. 'Hello, Brecw!',
3. textAlign: TextAlign.center,
4. style: new TextStyle(fontWeight: FontWeight.bold),
5. )
Key key,
TextStyle style,
StrutStyle strutStyle,
TextAlign textAlign,
TextDirection textDirection,
TextOverflow overflow,
bool softWrap,
double textScaleFactor,
int maxLines,
String semanticsLabel,
TextWidthBasis textWidthBasis,
TextHeightBehavior textHeightBehavior
The following are the essential properties of the Text widget used in our
application:
TextAlign: It is used to specify how our text is aligned horizontally. It also controls
the text location.
Overflow: It is used to determine when the text will not fit in the available space.
It means we have specified more text than the available space.
SoftWrap: It is used to determine whether or not to show all text widget content
when there is not enough space available. If it is true, it will show all content.
Otherwise, it will not show all content.
Style: It is the most common property of this widget that allows developers to
styling their text. It can do styling by specifying the foreground and background
color, font size, font weight, letter and word spacing, locale, shadows, etc. See
the table to understand it more easily:
Attributes Descriptions
fontFamily It is used to specify the typeface for the font. For this, we need
to download a typeface file in our project, and then keep
this file into the assets/font folder. Finally, config the
pubspec.yaml file to use it in the project.
// main.dart
import 'package:flutter/material.dart';
void main() {
runApp(const HelloWorldApp());
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Hello World App'),
),
body : const Center(
child: Text('Hello, World!!!'),
),
)
);
}
}
Output:
Flutter Images
When you create an app in Flutter, it includes both code and assets
(resources).
An asset is a file, which is bundled and deployed with the app and is
accessible at runtime.
The asset can include static data, configuration files, icons, and images.
The Flutter supports many image formats, such as JPEG, WebP, PNG, GIF,
animated WebP/GIF, BMP, and WBMP.
Displaying images is the fundamental concept of most of the mobile
apps.
Flutter has an Image widget that allows displaying different types of
images in the mobile application.
Prepared by: G SUDHAKAR RAJU Page 4 of 27
UI DESIGN-FLUTTER LAB – AY – 2024-25 B.Tech IV Year I Semester - IT- A & B
Step 1: First, we need to create a new folder inside the root of the Flutter project
and named it assets. We can also give it any other name if you want.
1. assets:
2. - assets/logo.png
3. - assets/background.png
If the assets folder contains more than one image, we can include it by
specifying the directory name with the slash (/) character at the end.
1. flutter:
2. assets:
3. - assets/
// main.dart
1. import 'package:flutter/material.dart';
Output:
Step 1: Create a folder in your project directory with the name images and
copy the image into that directory. For example, here in this program copied
the brecwlogo.png file and the same is used in Line 14.
Step 2: open the pubspec.yaml and add the following code under flutter
section:
flutter:
assets:
- images/
# assets:
# - images/
Save the file and debug main.dart with google chrome in the
VSCode/Android Studio and you can see the following output:
Flutter Container
The container in Flutter is a parent widget that can contain multiple child
widgets and manage them efficiently through width, height, padding,
background color, etc. It is a widget that combines common painting,
positioning, and sizing of the child widgets. It is also a class to store one or more
widgets and position them on the screen according to our needs. Generally, it
is similar to a box for storing contents. It allows many attributes to the user for
decorating its child widgets, such as using margin, which separates the
container with other contents.
A container widget is same as <div> tag in html. If this widget does not contain
any child widget, it will fill the whole area on the screen automatically.
Otherwise, it will wrap the child widget according to the specified height &
width. It is to note that this widget cannot render directly without any parent
widget. We can use Scaffold widget, Center widget, Padding widget, Row
widget, or Column widget as its parent widget.
1. Container({Key key,
2. AlignmentGeometry alignment,
3. EdgeInsetsGeometry padding,
4. Color color,
5. double width,
6. double height,
7. Decoration decoration,
8. Decoration foregroundDecoration,
9. BoxConstraints constraints,
10. Widget child,
11. Clip clipBehavior: Clip.none
12. });
1. child: This property is used to store the child widget of the container. Suppose
we have taken a Text widget as its child widget that can be shown in the below
example:
1. Container(
2. child: Text("Hello! I am in the container widget", style: TextStyle(fontSize: 25)),
3. )
2. color: This property is used to set the background color of the text. It also
changes the background color of the entire container. See the below
example:
1. Container(
2. color: Colors.green,
3. child: Text("Hello! I am in the container widget", style: TextStyle(fontSize: 25)),
4. )
3. height and width: This property is used to set the container's height and width
according to our needs. By default, the container always takes the space
based on its child widget. See the below code:
1. Container(
2. width: 200.0,
3. height: 100.0,
4. color: Colors.green,
5. child: Text("Hello! I am in the container widget", style: TextStyle(fontSize: 25)),
6. )
4. margin: This property is used to surround the empty space around the
container. We can observe this by seeing white space around the container.
Suppose we have used the EdgeInsets.all(25) that set the equal margin in all
four directions, as shown in the below example:
1. Container(
2. width: 200.0,
3. height: 100.0,
4. color: Colors.green,
5. margin: EdgeInsets.all(20),
6. child: Text("Hello! I am in the container widget", style: TextStyle(fontSize: 25)),
7. )
5. padding: This property is used to set the distance between the border of the
container (all four directions) and its child widget. We can observe this by
seeing the space between the container and the child widget. Here, we have
used an EdgeInsets.all(35) that set the space between text and all four
container directions:
1. Container(
2. width: 200.0,
3. height: 100.0,
4. color: Colors.green,
5. padding: EdgeInsets.all(35),
6. margin: EdgeInsets.all(20),
7. child: Text("Hello! I am in the container widget", style: TextStyle(fontSize: 25)),
8. )
6. alignment: This property is used to set the position of the child within the
container. Flutter allows the user to align its element in various ways such as
center, bottom, bottom center, topLeft, centerRight, left, right, and many
more. In the below example, we are going to align its child into the bottom
right position.
1. Container(
2. width: 200.0,
3. height: 100.0,
4. color: Colors.green,
5. padding: EdgeInsets.all(35),
6. margin: EdgeInsets.all(20),
7. alignment: Alignment.bottomRight,
8. child: Text("Hello! I am in the container widget", style: TextStyle(fontSize: 25)),
9. )
7. decoration:
// main.dart
import 'package:flutter/material.dart';
@override
return MaterialApp(
home: MyStackWidget(),
);
@override
return MaterialApp(
home: Scaffold(
appBar: AppBar(
),
body: Center(
child: IndexedStack(
index: 0,
// index: 1,
children: <Widget>[
Container(
height: 250,
width: 250,
color: Colors.green,
child: Center(
child: Text(
'First Widget',
),
),
),
Container(
height: 200,
width: 200,
color: Colors.blue,
child: Center(
child: Text(
'Second Widget',
),
),
),
],
),
),
);
Output:
index: 0, index: 1,
Flutter Scaffold
The Scaffold is a widget in Flutter used to implements the basic material design visual
layout structure. It is quick enough to create a general-purpose mobile application
and contains almost everything we need to create a functional and
responsive Flutter apps. This widget is able to occupy the whole device screen. In other
words, we can say that it is mainly responsible for creating a base to the app screen
on which the child widgets hold on and render on the screen. It provides many widgets
or APIs for showing Drawer, SnackBar, BottomNavigationBar, AppBar,
FloatingActionButton, and many more.
The Scaffold class is a shortcut to set up the look and design of our app that allows us
not to build the individual visual elements manually. It saves our time to write more
code for the look and feel of the app. The following are the constructor and
properties of the Scaffold widget class.
1. const Scaffold({
2. Key key,
3. this.appBar,
4. this.body,
5. this.floatingActionButton,
6. this.floatingActionButtonLocation,
7. this.persistentFooterButtons,
8. this.drawer,
9. this.endDrawer,
10. this.bottomNavigationBar,
11. this.bottomSheet,
12. this.floatingActionButtonAnimator,
13. this.backgroundColor,
14. this.resizeToAvoidBottomPadding = true,
15. this.primary = true,
16. })
1. appBar: It is a horizontal bar that is mainly displayed at the top of the Scaffold widget.
It is the main part of the Scaffold widget and displays at the top of the screen. Without
this property, the Scaffold widget is incomplete. It uses the appBar widget that itself
contains various properties like elevation, title, brightness, etc.
2. body: It is the other primary and required property of this widget, which will display
the main content in the Scaffold. It signifies the place below the appBar and behind
the floatingActionButton & drawer. The widgets inside the body are positioned at the
6. primary: It is used to tell whether the Scaffold will be displayed at the top of
the screen or not. Its default value is true that means the height of the appBar
extended by the height of the screen's status bar.
primary: true/false,
Key Points
1. Row and Column widgets are the most commonly used layout patterns in the Flutter
application.
2. Both may take several child widgets.
3. A child widget can also be a row or column widget.
4. We can stretch or constrain a particular children's widget.
5. Flutter also allows developers to specify how child widgets can use row and column
widgets' available space.
Row Widget
This widget arranges its children in a horizontal direction on the screen. In
other words, it will expect child widgets in a horizontal array. If the child widgets
need to fill the available horizontal space, we must wrap the children widgets in
an Expanded widget.
A row widget does not appear scrollable because it displays the widgets within
the visible view. So it is considered wrong if we have more children in a row
which will not fit in the available space. If we want to make a scrollable list of
row widgets, we need to use the ListView widget.
We can control how a row widget aligns its children based on our choice using
the property crossAxisAlignment and mainAxisAlignment. The row's cross-
axis will run vertically, and the main axis will run horizontally. See the below
visual representation to understand it more clearly.
We can align the row's children widget with the help of the following properties:
o start: It will place the children from the starting of the main axis.
o end: It will place the children at the end of the main axis.
o center: It will place the children in the middle of the main axis.
o spaceBetween: It will place the free space between the children evenly.
o spaceAround: It will place the free space between the children evenly and half
of that space before and after the first and last children widget.
o spaceEvenly: It will place the free space between the children evenly and before
and after the first and last children widget.
Column
This widget arranges its children in a vertical direction on the screen. In
other words, it will expect a vertical array of children widgets. If the child widgets
need to fill the available vertical space, we must wrap the children widgets in an
Expanded widget.
A column widget does not appear scrollable because it displays the widgets
within the visible view. So it is considered wrong if we have more children in a
column which will not fit in the available space. If we want to make a scrollable
list of column widgets, we need to use the ListView Widget.
We can also control how a column widget aligns its children using the property
mainAxisAlignment and crossAxisAlignment. The column's cross-axis will
run horizontally, and the main axis will run vertically. The below visual
representation explains it more clearly.
o Row widget in Flutter does not have horizontal scrolling. So if we have inserted
a large number of children in a single row that cannot be fit in that row, we will
see the Overflow message.
o Column widget in Flutter does not have vertical scrolling. So if we have inserted
a large number of children in a single column whose total children size is not
equal to the height of the screen, we will see the Overflow message.
Flutter Stack
The stack is a widget in Flutter that contains a list of widgets and positions them on
top of the other. In other words, the stack allows developers to overlap multiple
widgets into a single screen and renders them from bottom to top. Hence, the first
widget is the bottommost item, and the last widget is the topmost item
1. Stack(
2. children: <Widget>[
3. // Max Size
4. Container(
5. color: Colors.green,
6. ),
7. Container(
8. color: Colors.blue,
9. ),
10. Container(
11. color: Colors.yellow,
12. )
13. ],
14. ),
alignment: It determines how the children widgets are positioned in the stack. It can
be top, bottom, center, center-right, etc.
1. Stack(
2. alignment: Alignment.topCenter, // Center of Top
3. children: <Widget>[ ]
4. )
textDirection: It determines the text direction. It can draw the text either ltr (left to
right) or rtl (right to the left).
1. Stack(
2. textDirection: TextDirection.rtl, // Right to Left
3. children: <Widget>[ ]
4. )
fit: It will control the size of non-positioned children widgets in the stack. It has three
types: loose, expand and passthrough. The loose used to set the child widget small,
the expand attribute makes the child widget as large as possible, and
the passthrough set the child widget depending on its parent widget.
1. Stack(
2. fit: StackFit.passthrough,
3. children: <Widget>[ ]
4. )
overflow: It controls the children widgets, whether visible or clipped, when it's content
overflowing outside the stack.
1. Stack(
2. overflow: Overflow.clip, // Clip the Content
3. children: <Widget>[ ]
4. )
Positioned
It is not the stack parameter but can be used in the stack to locate the children widgets.
The following are the constructor of the positioned stack:
1. const Positioned({
2. Key key,
3. this.left,
4. this.top,
5. this.right,
6. this.bottom,
7. this.width,
8. this.height,
9. @required Widget child,
10. })
// main.dart
import 'package:flutter/material.dart';
decoration:BoxDecoration(
borderRadius:BorderRadius.circular(8),
color:Colors.green
),
child: Text("CSE",style:
TextStyle(color:Colors.deepPurpleAccent,fontSize:25),),
),
Container(
margin: EdgeInsets.all(12.0),
padding: EdgeInsets.all(8.0),
decoration:BoxDecoration(
borderRadius:BorderRadius.circular(8),
color:Colors.green
),
child: Text("CSM",style:
TextStyle(color:Colors.redAccent,fontSize:25),),
)
]
),
);
}
}
Output:
// main.dart
import 'package:flutter/material.dart';
@override
return MaterialApp(
home: MyHomePage()
);
@override
@override
return Scaffold(
appBar: AppBar(
),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children:<Widget>[
Container(
margin: EdgeInsets.all(20.0),
padding: EdgeInsets.all(12.0),
decoration:BoxDecoration(
borderRadius:BorderRadius.circular(8),
color:Colors.red
),
),
Container(
margin: EdgeInsets.all(20.0),
padding: EdgeInsets.all(12.0),
decoration:BoxDecoration(
borderRadius:BorderRadius.circular(8),
color:Colors.red
),
child: Text("CSE",style:
TextStyle(color:Colors.yellowAccent,fontSize:20),),
),
Container(
margin: EdgeInsets.all(20.0),
padding: EdgeInsets.all(12.0),
decoration:BoxDecoration(
borderRadius:BorderRadius.circular(8),
color:Colors.red
),
child: Text("CSM",style:
TextStyle(color:Colors.amberAccent,fontSize:20),),
),
);
Output:
// main.dart
import 'package:flutter/material.dart';
width: 150,
color: Colors.blue,
child: Center(
child: Text(
'Middle Widget',
style: TextStyle(color: Colors.white, fontSize: 20),
),
),
),
),
Positioned(
top: 30,
left: 20,
child: Container(
height: 100,
width: 150,
color: Colors.orange,
child: Center(
child: Text(
'Bottom Widget',
style: TextStyle(color: Colors.white, fontSize: 20),
),
),
)
),
],
),
)
),
);
}
}
Output: