Professional Documents
Culture Documents
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
Layouts in Flutter
• The core of Flutter’s layout mechanism is widgets.
• In Flutter, almost everything is a widget—even layout models are
widgets.
• The images, icons, and text that you see in a Flutter app are all
widgets.
• But things you don’t see are also widgets, such as the rows, columns,
and grids that arrange, constrain, and align the visible widgets.
Widget
• Widgets describe what their view should look like given their current
configuration and state.
• When a widget’s state changes, the widget rebuilds its description,
which the framework diffs against the previous description in order to
determine the minimal changes needed in the underlying render tree to
transition from one state to the next.
Stateless widget
• A widget that does not require mutable state.
class MyWidget extends StatelessWidget {
const MyWidget({super.key});
@override
Widget build(BuildContext context) {
return Container();
}
}
Stateful Widget
• A stateful Widget means a widget that has a mutable state
class MyWidget extends StatefulWidget {
const MyWidget({super.key});
@override
State<MyWidget> createState() => _MyWidgetState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: ImmutableWidget(),
);
}
Material apps are one of the primary building blocks for apps that follow Google's
Material Design specification
This widget creates several helper properties, such as navigation, theming, and
localization.
You can also use a CupertinoApp if you want to follow Apple's design language, or a
WidgetsApp if you want to create your own.
There are some widgets that use different property names to describe
their child.
MaterialApp uses home, and Scaffold uses body.
Using the MaterialApp widget is entirely optional but a good practice.
Even though these are named differently, they are still the same as
the child property that you will see on most of the widgets in the
Flutter framework.
Container widget
• container is similar to a div in HTML. It's rendered as a box that has many styling options
• The Container widget lets you create a rectangular visual element. A container can be decorated with a
BoxDecoration, such as a background, a border, or a shadow.
@override
Widget build(BuildContext context) {
return Container(
color: Colors.green,
child: Padding(
padding: EdgeInsets.all(40),
child: Container(
color: Colors.purple,
),
),
);
}
The Padding widget will adjust the spacing of its child, which can be any widget of any shape or size.
Scaffold Widget
• Android and iOS user interfaces are based on two different design languages.
• Android uses Material Design, while iOS uses Cupertino
• Material and Cupertino, provide a set of widgets that mirror the user experience of their respective
platforms.
• These frameworks use a widget called Scaffold (CupertinoScaffold in the Cupertino framework) that
provides a basic structure of a screen.
• Scaffold widget are used to add an AppBar to the top of the screen and a slide-out drawer that you can
pull from the left, and body.
• AppBar is persistent header that lives at the top of the screen and helps you navigate from one screen
to the next.
• The scaffold will expand to fill the available space. That
usually means that it will occupy its entire window or device
screen
• Elements
AppBar
BottomAppBar
FloatingActionButton
BottomSheet
Drawer
body: is the primary content of the scaffold
• Which is a horizontal bar typically shown at the top
of an app using the appBar property.
• App bars typically expose one or more common
actions with IconButtons which are optionally
followed by a PopupMenuButton for less common
operations (sometimes called the "overflow menu").
appBar: AppBar(
leading:Text('') ,
title: Text('') ,
actions: [],
),.
appBar: AppBar(
backgroundColor: Colors.indigo,
title: Text('Welcome to Flutter’),
actions: <Widget>[
Padding(
padding: const EdgeInsets.all(10.0),
child: Icon(Icons.edit), ),
],
)
Drawer
• A panel displayed to the side of the body, often hidden on mobile
devices.
• You can easily add to Scaffold widget:
drawer: Drawer(
child: ListView()
),
Text
• The Text widget lets you create a run of styled text within your
application.
Attributes:
Text(“HELLO!”, style: TextStyle(fontSize: 14, fontweight:
FontWeight.bold))
• Scaffold widget as the root widget for your screen, as you have in this recipe, but it is not
required.
• You generally use a Scaffold widget when you want to create a screen.
• Widgets that do not begin with Scaffold are intended to be components used to compose
screens
• Scaffolds are also aware of your device's metrics.
Center Widget
A Center widget centers its child both horizontally and vertically.
• To make Widget element to center of the container widget(upper father widget)
Center(
child: Text(“Centered Text”);
)
AspectRatio widget
You can use the AspectRatio widget when you want to size a widget
following a specific aspect ratio.
The AspectRatio widget tries the largest width possible in its context,
and then sets the height applying the chosen aspect ratio to the
width.
For instance, an aspect ratio of 1 will set the height to be equal to the
width.
Icon
Icon(
child: Icons.home,
color:Colors.red,
size: 10,
)
• Icon widget have on tap functionality, but you can wrap this widget
with other widgets
Image
• NetworkImage(String url): this widget is user to
read image from the network/remote server
• Image.asset(String image_path): this widget helps
to read image from local asset folder.
• Image.file(File file): read image from file
• Image.memory(Uint8List byte): read an image from
byte array
TextButton
TextButton(
style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 20),
),
onPressed: () {},
child: const Text('Enabled'),
),
Column
• A widget that displays its children in a vertical array.
• To cause a child to expand to fill the available vertical space, wrap the child in an
Expanded widget.
Column(
children:<Widget> [
Text(“Hello”),
Container()
])
Row
• A widget that displays its children in a horizontal array.
• To cause a child to expand to fill the available horizontal space, wrap
the child in an Expanded widget.
Row(
children:<Widget> [
Text(“Hello”),
Container()
])
THANK YOU!