Unit: 2 UI Design, State Management, Navigation: Prof. Mehul Bhundiya
Unit: 2 UI Design, State Management, Navigation: Prof. Mehul Bhundiya
(MADF) (2105CS303)
Unit : 2
UI Design, State
Management,
Navigation
Prof. Mehul Bhundiya
Computer Science & Engineering Department
Darshan Institute of Engineering & Technology, Rajkot
[email protected]
+91 - 9428231065
Introduction to flutter widgets
Flutter widgets are built using a modern framework.
Widgets describe their view on screen how it look, current configuration and state.
Each element on a screen of the Flutter app is a widget.
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.
My App
Material App
My Home
Page
Scaffold
Structure
1 MaterialApp
2 Scaffold
3 AppBar
4 Body
5 Center
6 Text("Hello, world!")
Prof. Mehul Bhundiya #2105CS303 (MDAF) Unit 2 – UI Design, State Management, Navigation 9
Scaffold
The Scaffold widget is a layout widget.
It provides a basic structure for the UI of a Flutter application.
It includes an AppBar at the top of the screen, a Body in the center of the screen, and a
FloatingActionButton at the bottom of the screen.
It is a flexible widget that can be customized to meet the specific needs of the application.
For example, the AppBar can be configured to include a title, a search bar, or a set of actions.
The Body used to include any widget, such as text, images, lists, or forms.
Prof. Mehul Bhundiya #2105CS303 (MDAF) Unit 2 – UI Design, State Management, Navigation 12
Safearea
SafeArea is an important and useful widget in Flutter which makes UI dynamic and adaptive
to a wide variety of devices.
In Different designs and in certain scenarios, App might overlay any pre-occupied constraints
Like Status bar, notches, navigation bar, etc.
So, in order to make our UI adaptive and error-free, we use SafeArea widget.
SafeArea is basically a padding widget, which adds any necessary padding to your app, based
on the device it is running on.
If App’s widgets are overlaying any of the system’s features like notches, status bar, camera
or any other such features, then SafeArea would add padding around the app, as required.
Internally SafeArea uses MediaQuery to check the dimensions of the display screen and
includes extra padding if needed.
bottom This property is of type bool. It is true by default and setting it to false would
disable SafeArea from adding padding to the bottom of the screen.
top This property is also of type bool and setting it to false would avoid padding at top of the screen.
left This property is of type bool and setting it to false would avoid padding at left side of the
screen.
right This property is of type bool and setting it to false would avoid padding at right side of the
screen.
minimum This property is of type EdgeInsets. You can specify the minimum padding to be added using
this property.
maintainBottomViewPaddi This property is of type bool and it specifies whether SafeArea should maintain
ng the viewPadding instead of padding. For instance, if you are using an on-screen keyboard
with SafeArea , the the padding can be maintained below the obstruction rather than being
consumed.
textAlign This property is used to align the text according. Ex: textAlign: TextAlign.center.
textScaleFactor This property is adjust the font size one is using the textScaleFactor in Text widget or using
TextStyle class. Ex: textScaleFactor: 4.0,
color This property used to set color to the text.
Ex: style: TextStyle(
color: Colors.blue
),
fontWeight This property is used to set weight to the text. Ex.: fontWeight: FontWeight.w300,
fontFamily This property is used to set different font family (.ttf). Ex: fontFamily: "Caveat",
we can expand a TextField very easily by adding the attributes maxLines (ex. maxLines: 4,).
We can restrict the maximum number of characters inside the text field by adding
the maxLength attributes (Ex. maxLength: 10,)
We can convert visible text to password using obscure attribute (Ex.: obscureText: true,).
Flutter allows the user to retrieve the text in mainly two ways:
onChanged method.
the controller method.
Example
Example
1 TextEditingController mycontroller = TextEditingController
1 String value = ""; ();
2
2 TextField( TextField(controller: mycontroller,)
3
3 onChanged: (text) {
4
4 value = text; controller.addListener(() {
5
5 }, print(controller.text);
6
6 ) });
children This property takes in List<Widget>, that is a list of widgets to display inside the Row or
the Column widget.
crossAxisAlignment The crossAxisAlignment takes in CrossAxisAlignment enum as the object to how the children’s
widgets should be places in crossAxisAlignment. For Row it is vertical and for Column it is
horizontal.
mainAxisAlignment This property takes in MainAxisAlignment enum as the object to decide how the children
widgets should be place in mainAxisAlignment. For Row it is horizontal and for Column it is
vertical.
mainAxisSize This property decides the size of main-axis by taking in MainAxisSize enum as the object.
textDirection This property controls the text direction of the Row or Column widget, which can either be from
left-to-right (by default) or right-to-left.
Example
1 @override
2 Widget build(BuildContext context) {
3 return ListView(
4 padding: EdgeInsets.all(20),
5 children: <Widget>[
6 CircleAvatar(maxRadius: 50,
7 backgroundColor: Colors.black,
8 child: Icon(Icons.person, color: Colors.white, size:
9 50),),
10 Center(child: Text('Sooraj S Nair',
11 style: TextStyle(
12 fontSize: 50,),),),
13 Text(
14 "Lorem Ipsum is simply dummy text of the
15 printing and typesetting industry. Lorem Ipsum has been the ",
16 style: TextStyle(fontSize: 20,),),
17 ],
18 ),
}
Example
1 @override
2 Widget build(BuildContext context) {
3 return ListView.builder(
4 itemCount: 20,
5 itemBuilder: (context, position) {
6 return Card(
7 child: Padding(
8 padding: EdgeInsets.all(20.0),
9 child: Text(
10 position.toString(),
11 style: TextStyle(fontSize: 22.0),
12 ),),);
13 },),
14 }
#2105CS303 (MDAF) Unit 2 – Introduction to dart programming
Prof. Mehul Bhundiya 41
Button Widgets
Buttons are the graphical control element that provides a user to trigger an event.
Buttons are the Flutter widgets, which is a part of the material design library.
There are different types of buttons available in flutter:
Flat Button
Raised Button
Floating Button
Icon Button
Inkwell Button
Outline Button
Example
1 @override
2 Widget build(BuildContext context) {
3 return Scaffold(
4 floatingActionButton: FloatingActionButton.extended(
5 onPressed: () {},
6 icon: Icon(Icons.save),
7 label: Text("Save"),
8 ),
9 ),
10 );
11 }
#2105CS303 (MDAF) Unit 2 – Introduction to dart programming
Prof. Mehul Bhundiya 47
Icon Button
InkWell button is a material design concept, which is used for touch response.
This widget comes under the Material widget where the ink reactions are actually painted.
It creates the app UI interactive by adding gesture feedback.
It is mainly used for adding splash ripple effect.
Example
1 @override
2 Widget build(BuildContext context) {
3 return InkWell(
4 splashColor: Colors.green,
5 highlightColor: Colors.blue,
6 child: Icon(Icons.ring_volume, size: 50),
7 onTap: () {
8 setState(() {
9 _volume += 2;
10 });
11 }, ),
12 }
Example
1 @override
2 Widget build(BuildContext context) {
3 return OutlineButton(
4 child: Text("Outline Button", style: TextStyle(fontSize: 20.0),
5 ),
6 highlightedBorderColor: Colors.red,
7 shape: RoundedRectangleBorder(
8 borderRadius: BorderRadius.circular(15)),
9 onPressed: () {},
10 ),
11 }, ),
12 }
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.
Example
1 flutter:
2 assets:
3 - assets/
Example
1 // Within the `FirstRoute` widget
2 onPressed: () {
3 Navigator.push(
4 context,
5 MaterialPageRoute(builder: (context) => SecondRoute()),
6
7 );
}
Example
1 // Within the SecondRoute widget
2 onPressed: () {
3 Navigator.pop(context);
4 }
Example
1 MaterialApp(
2 title: 'Named Route Navigation',
3 theme: ThemeData(
4 primarySwatch: Colors.green,
5 ),
6 initialRoute: '/',
7 routes: {
8 '/': (context) => HomeScreen(),
9 '/second': (context) => SecondScreen(),
10 },
11 ));
Route - 1 Route - 2
1 @override 1 @override
2 Widget build(BuildContext context) { 2 Widget build(BuildContext context) {
3 return Scaffold( 3 return Scaffold(
4 appBar: AppBar( 4 appBar: AppBar(
5 title: Text('First Route'), 5 title: Text("Second Route"),
6 ), 6 ),
7 body: Center( 7 body: Center(
8 child: RaisedButton( 8 child: RaisedButton(
9 child: Text('Open route'), 9 onPressed: () {
10 onPressed: () { 10 Navigator.pop(context);
11 Navigator.pushNamed(context, '/ 11 },
12 second'); 12 child: Text('Go back!'),
13 ); 13 ),
14 }, 14 ),
15 ), 15 );
16 ), 16 }
17 );
} #2105CS303 (MDAF) Unit 2 – Introduction to dart programming
Prof. Mehul Bhundiya 61
Thank You