03 Advanced Mobile Programming Using Flutter
03 Advanced Mobile Programming Using Flutter
Introduction to Flutter
Introduction to Widgets
Widgets is an immutable object that describes a specific part of a UI 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.
Minimal Flutter App
import 'package:flutter/material.dart';
The Text widget lets you create a run of styled text within your
application
Row, Column
These flex widgets let you create flexible layouts in both the horizontal
(Row) and vertical (Column) directions
The design of these objects is based on the web’s flexbox layout model.
Commonly Used Basic widgets
Stack
A Stack widget lets you place widgets on top of each other in paint order.
You can then use the Positioned widget on children of a Stack to position
them relative to the top, right, bottom, or left edge of the stack.
Text(
‘Example title',
style: Theme.of(context).primaryTextTheme.headline6,
)
Commonly Used Basic widgets
Row Widget
Row(
children: <Widget>[
IconButton(
icon: Icon(Icons.menu),
tooltip: 'Navigation menu',
onPressed: null, // null disables the button
),
IconButton(
icon: Icon(Icons.search),
tooltip: 'Search',
onPressed: null,
),
],
)
Commonly Used Basic widgets
Column Widget
Column(
children: <Widget>[
MyAppBar(
title: Text(
'Example title',
style: Theme.of(context).primaryTextTheme.headline6,
),
),
Expanded(
child: Center(
child: Text('Hello, world!'),
),
),
],
)
Using Material Components
Flutter provides a number of widgets that help you build apps that follow
Material Design
A Material app starts with the MaterialApp widget, which builds a number of
useful widgets at the root of your app
void main() {
runApp(MaterialApp(
home: TutorialHome(),
));
}
Handling User Interaction
class MyButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GestureDetector( The GestureDetector widget
onTap: () { doesn’t have a visual
print('MyButton was tapped!');
representation but instead
},
detects gestures made by the
child: Container(),
), user
);
} You can use GestureDetector When the user taps the Container,
} to detect a variety of input the GestureDetector calls its
gestures, including taps, drags, onTap() callback
and scales.
Stateless and Stateful Widgets
When writing an app, you’ll commonly create new widgets that are subclasses
of either StatelessWidget or StatefulWidget
The flutter framework builds those widgets in turn until the process bottoms
out in widgets that represent the underlying RenderObject, which computes
and describes the geometry of the widget
Stateless Widgets
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Material(
child: Container (),
); void main() {
} runApp(MaterialApp(
} title: 'My app',
home: SafeArea(
child: MyWidget(),
),
));
}
class _CounterState extends State<Counter>
{
void _increment() {
setState(() {
class Counter extends StatefulWidget {
_counter++;
@override
});
_CounterState createState() =>
}
_CounterState();
}
@override
Widget build(BuildContext context) {
return Row(
children: <Widget>[
ElevatedButton(
onPressed: _increment,
child: Text('Increment'),
),
Text('Count: $_counter'),
],
);
}
}
Composing Widgets
The following diagram is composed of 14 widgets
Rows, columns, and grids that arrange, constrain, and align the visible
widgets are also widgets
How to Lay out a single Widget
Let us see as an example how to lay out the text
widget shown in the right
How to Lay out a single Widget
Step 1: Select a layout widget
Text('Hello World')
How to Lay out a single Widget
Step 3: Add the visible widget to the layout widget
Center(
)
How to Lay out a single Widget
Step 4: Add the layout widget to the page
You can use a Row widget to arrange widgets horizontally, and a Column
widget to arrange widgets vertically
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Image.asset('images/pic1.jpg'),
Image.asset('images/pic2.jpg'),
Image.asset('images/pic3.jpg'),
],
);
Aligning widgets
The MainAxisAlignment and CrossAxisAlignment classes offer a variety of
constants for controlling alignment
Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Image.asset('images/pic1.jpg'),
Image.asset('images/pic2.jpg'),
Image.asset('images/pic3.jpg'),
],
);
Sizing widgets
When a layout is too large to fit a device, a
yellow and black striped pattern appears
along the affected edge
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: Image.asset('images/pic1.jpg'),
),
Expanded(
flex: 2,
child: Image.asset('images/pic2.jpg'),
),
Expanded(
child: Image.asset('images/pic3.jpg'),
),
],
);
Packing widgets
By default, a row or column occupies as much space along its main axis as
possible, but if you want to pack the children closely together, set its
mainAxisSize to MainAxisSize.min
Packing widgets
Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.star, color: Colors.green[500]),
Icon(Icons.star, color: Colors.green[500]),
Icon(Icons.star, color: Colors.green[500]),
Icon(Icons.star, color: Colors.black),
Icon(Icons.star, color: Colors.black),
],
)
Common layout widgets
Standard widgets
Card: Organizes related info into a box with rounded corners and a drop
shadow
Contains a single child widget, but that child can be a Row, Column, or even
the root of a widget tree
Common layout widgets: Container
Widget _buildImageColumn() => Container(
decoration: BoxDecoration(
color: Colors.black26,
),
child: Column(
children: [
_buildImageRow(1),
_buildImageRow(3),
],
),
);
Common layout widgets: GridView
Use GridView to lay widgets out as a two-dimensional list
GridView provides two pre-fabricated lists, or you can build your own custom
grid.
When a GridView detects that its contents are too long to fit the render box,
it automatically scrolls
Common layout widgets: GridView
GridView.count
GridView.extent
Less configurable than Column, but easier to use and supports scrolling
Common layout widgets: ListView
Common layout widgets: ListView
A specialized Column for organizing a list of boxes
Less configurable than Column, but easier to use and supports scrolling
Common layout widgets: ListView
Widget _buildList() => ListView(
children: [
_tile('CineArts at the Empire', '85 W Portal Ave',
Icons.theaters),
_tile('The Castro Theater', '429 Castro St', Icons.theaters),
...
Divider(),
_tile('K\'s Kitchen', '757 Monterey Blvd', Icons.restaurant),
_tile('Emmy\'s Restaurant', '1923 Ocean Ave', Icons.restaurant),
],
);
Common layout widgets: ListView
ListTile _tile(String title, String subtitle, IconData icon) => ListTile(
title: Text(title,
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 20,
)),
subtitle: Text(subtitle),
leading: Icon(
icon,
color: Colors.blue[500],
),
);
Common layout widgets: Stack
Use Stack to arrange widgets on top of a base widget—often an image
The first widget in the list of children is the base widget; subsequent children
are overlaid on top of that base widget
Accepts a single child, but that child can be a Row, Column, or other widget
that holds a list of children