DOC-20241208-WA0017
DOC-20241208-WA0017
DOC-20241208-WA0017
ا.سعاد عثمان
Widget
In Flutter, everything is a widget, which is the core concept of this framework. Widget
in the Flutter is basically a user interface component that affects and controls the view
and interface of the app. It represents an immutable description of part of the user
interface and includes graphics, text, shapes, and animations that are created using
widgets.
Widgets are nested with each other to build the app. It means the root of your app is
itself a widget, and all the way down is a widget also.
Widgets
Widgets are the building blocks of a Flutter app, and each widget is
an immutable declaration of the user interface. In other words,
widgets are configurations (instructions) for different parts of the UI.
Placing the widgets together creates the widget tree.
➤➤ Widgets with structuring elements such as a list, grid, text, and button
➤➤ Widgets with input elements such as a form, form fields, and keyboard listeners
➤➤ Widgets with styling elements such as font type, size, weight, color, border, and shadow
➤➤ Widgets to lay out the UI such as row, column, stack, centering, and padding
➤➤ Widgets with interactive elements that respond to touch, gestures, dragging, and dismissible
➤➤ Widgets with animation and motion elements such as hero animation, animated container,
animated crossfade, fade transition, rotation, scale, size, slide, and opacity
The visible widgets are related to the user input and output data.
Some of the important types of this widget are: Text, Button,
Image, Icon, column, Row
State Management Widget
(types of widget)
In Flutter, there are mainly two types of widget:
• StatelessWidget
• StatefulWidget
A StatefulWidget has state information. It contains mainly two classes: the state object and
the widget. It is dynamic because it can change the inner data during the widget lifetime. It
has createState() method, which returns a class that extends the Flutters State Class.
The StatelessWidget does not have any state information. It remains static throughout its
lifecycle.
WIDGET LIFECYCLE EVENTS
To build the UI, you use two main types of widgets, StatelessWidget
and StatefulWidget. A
stateless widget is used when the values (state) do not change, and
the stateful widget is used when values (state) change.
You call the setState() method to notify the framework that this object
has changes, and the widget’s build method is called (scheduled). You
would set the new state values inside the setState() method.
The Scaffold is a widget in Flutter used to implements the basic material design
visual layout structure.
const Scaffold({
Key key,
this.appBar,
this.body,
this.floatingActionButton,
this.floatingActionButtonLocation,
this.persistentFooterButtons,
this.drawer,
this.endDrawer,
this.bottomNavigationBar,
this.bottomSheet,
this.floatingActionButtonAnimator,
this.backgroundColor,
this.resizeToAvoidBottomPadding = true,
this.primary = true,
})
Scaffold widget.
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. Widget build(BuildContext context)
{
return Scaffold(
appBar: AppBar(
title: Text('First Flutter Application'),
), )
}
Scaffold widget.
2.body: It is the other primary and required property of this widget, which
will display the main content in the Scaffold. The widgets inside the body
are positioned at the top-left of the available space by default.
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(this.title),
),
body: Center(
child: Text('Hello World'),
),
);
}
}
Scaffold properties(con..)
floatingActionButton: It is a button displayed at the bottom right corner and
floating above the body. It is a circular icon button that floats over the content
of a screen at a fixed place to promote a primary action in the application.
While scrolling the page, its position cannot be changed. It uses the
FloatingActionButton widget properties
using Scaffold.floatingActionButton.
floatingActionButton example:
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('First Flutter Application')),
body: Center(
child: Text("Welcome to Javatpoint!!"),
),
floatingActionButton: FloatingActionButton(
elevation: 8.0,
child: Icon(Icons.add),
onPressed: (){
print('I am Floating Action Button');
}
);
}
Elevation property that gives a shadow effect to the button. Icon widget to give an icon to the button
using preloaded Flutter SDK icons. The onPressed() property will be called when the user taps the
button.
Scaffold properties(con..)
floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
Scaffold properties(con..)
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.
primary: true/false,