flutter lec 3
flutter lec 3
Here are the most frequently used layout widgets and their purposes:
a. Single-child Layout Widgets
These widgets contain only one child.
Container: Adds padding, margins, borders, or background colors to a child widget.
Center: Centers a single child within the available space.
Padding: Adds padding around a child.
Align: Aligns a child within its parent (e.g., top-left, center, etc.).
b. Multi-child Layout Widgets
These widgets can contain multiple children.
Row: Arranges children horizontally.
Column: Arranges children vertically.
Stack: Overlays children on top of each other (like layers).
ListView: Creates a scrollable list of children.
GridView: Creates a grid layout.
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.
Container
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.
Why a container widget in
Flutter?
If we have a widget that needs some background styling may be a color, shape, or size
compose, decorate, and position its child widgets. If we wrap our widgets in a
container, then without using any parameters, we would not notice any difference in its
appearance. But if we add any properties such as color, margin, padding, etc. in a
container, we can style our widgets on the screen according to our needs.
Constructors of the container class
Container({Key key,
AlignmentGeometry alignment,
EdgeInsetsGeometry padding,
Color color,
double width,
double height,
Decoration decoration,
Decoration foregroundDecoration,
BoxConstraints constraints,
Widget child,
Clip clipBehavior: Clip.none
});
Properties of Container widget
1. child: This property is used to store the child widget of the
container.
Container(
child: Text("Hello! I am in the container widget",
style: TextStyle(fontSize: 25)),
)
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.
Container(
color: Colors.green,
constraints: BoxConstraints.expand(height: 60.0),
child: Text("Hello! I am in the container widget", style: TextStyle(fon
tSize: 25)),
)
class MyContainerWidget extends StatelessWidge
Example t{
@override
Widget build(BuildContext context) {
import 'package:flutter/material.dart'; return MaterialApp(
home: Scaffold(
void main() => runApp(MyApp()); appBar: AppBar(
title: Text("Flutter Container Example"),
/// This Widget is the main application wi ),
dget. body: Container(
class MyApp extends StatelessWidget { width: double.infinity,
height: 150.0,
color: Colors.green,
@override margin: EdgeInsets.all(25),
Widget build(BuildContext context) { padding: EdgeInsets.all(35),
return MaterialApp( alignment: Alignment.center,
home: MyContainerWidget(), transform: Matrix4.rotationZ(0.1),
); child: Text("Hello! I am in the container widg
} et!!",
} style: TextStyle(fontSize: 25)),
),
),
);
Flutter Row and Column
Rows and columns
Rows and columns are not a single widget; they are two different widgets, namely
Row and Column. Row and column are the two essential widgets in Flutter that allows
developers to align children horizontally and vertically according to our needs.
These widgets are very necessary when we design the application user interface in
Flutter.
1. Row and Column widgets are the most commonly used layout patterns in the Flutter
application.
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.
Row properties
We can align the row's children widget with the help of the following properties:
• start: It will place the children from the starting of the main axis.
• end: It will place the children at the end of the main axis.
• center: It will place the children in the middle of the main axis.
• spaceBetween: It will place the free space between the children evenly.
• 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.
• spaceEvenly: It will place the free space between the children evenly and before and after the
first and last children widget.
import 'package:flutter/material.dart';
TextField in Flutter is the most commonly used text input widget that allows
users to collect inputs from the keyboard into an app.
We can add several attributes with TextField, such as label, icon, inline hint text,
and error text using an InputDecoration as the decoration. If we want to remove
the decoration properties entirely, it is required to set the decoration to null.
TextField (
decoration: InputDecoration(
border: InputBorder.none,
labelText: 'Enter Name',
hintText: 'Enter Your Name'
),
);
• decoration: It is used to show the decoration around TextField.
void main() {
runApp(MaterialApp( home: MyApp(),));
}
Flutter allows the user to retrieve the text in mainly two ways: First is the
onChanged method, and another is the controller method. Both are discussed
below:
1. onChanged method: It is the easiest way to retrieve the text field value. This
method store the current value in a simple variable and then use it in the TextField
widget.
String value = "";
TextField(
onChanged: (text) {
value = text;
},
)
2. Controller method: It is a popular method to retrieve text field
value using TextEditingController. It will be attached to the
TextField widget and then listen to change and control the widget's
text value.
1. Create a TextEditingController.
2. Attach the TextEditingController to a TextField using controller property.
3. Retrieve the value of the TextField by using the text() method provided by
the TextEditingController.
TextEditingController mycontroller = TextEditingCo
ntroller();
TextField(
controller: mycontroller,
)
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp( home: MyApp(),));
}