FLUTTER
FLUTTER
/AamirPinger
/AamirPingerOfficial
/AamirPinger
Click for Flutter
Course Playlist
Flutter
● Flutter is a an open-source software development toolkit from Google for
building cross-platform apps.
● Flutter is NOT a language.
● Flutter uses Dart as its programming language.
● Dart is an object-oriented language.
Flutter gives you the ability to write once and run anywhere.
Flutter
● Flutter apps consist of a series of packages, plugins and widgets.
● Flutter app are build widgets.
● Widgets are building blocks for any flutter app.
● Widgets are collection of reusable UI elements (buttons, text inputs, sliders,
and so on) that you can personalize for your own needs.
● Development ease with hot reload.
○ This lets you update your code and see the changes live without re-deploying it.
Flutter App
Individual Widgets
Flutter - Widgets Tree
scaffold(
appBar: AppBar(),
body: Container(
child: Column(
children: [
Row( ... ),
Text(),
]
)
)
)
Source: https://iq.opengenus.org/getting-started-with-flutter-development/
Installation
https://flutter.dev/docs/get-started/install
Prerequisite
Source: https://www.javatpoint.com/flutter-scaffold
Simple centered Image App
Repo: https://github.com/aamirpinger/flutter-slide-code/
Repo: https://github.com/aamirpinger/flutter-slide-code/
Git Branch: example_3_bundling_image_as_asset
Git Diff: #1
App Icon
App Icon
● We will be using https://www.iconify.pro/ to generate icon files
App Icon
● After extracting the zip file, folders from android folder will go to
\android\app\src\main\res
Repo: https://github.com/aamirpinger/flutter-slide-code/
Git Branch: app_icons
Git Diff: #2
Free to use icons and images
Hot reload
Hot reload
Repo: https://github.com/aamirpinger/flutter-slide-code/
Git Branch: feature/hot_reload
Git Diff: #3
SafeArea( )
SafeArea( )
● Whenever we create a base widget it is by default align to top left corner
● Sometimes it not even visible because of Appbar or Mobile device screen
design.
● SafeArea can solve this issue.
MaterialApp(
home: SafeArea(child: Text('My Flutter App')),
);
Layout widgets
Layout Widgets
● Everything in flutter is a widget.
● Flutter allows us to create a layout by composing multiple widgets to build
more complex widgets.
● Layout widget can be categories into two types:
○ Single Child Widget
■ Widget that has only one child.
○ Multiple Child Widget
■ Widget may have multiple childs.
https://flutter.dev/docs/development/ui/widgets/layout
Single-Child
multi-Child
multi-Child
Single-Child
Source: https://www.javatpoint.com/flutter-layouts
Container( )
Container( )
● Container is a widget class that allows us to customize the child widget.
● It is mainly used to add borders, padding, margins, background color, and
many more.
●
body: SafeArea(
child: Container(
child: Text('Hello World'),
padding: EdgeInsets.symmetric(vertical: 20, horizontal: 12),
margin: EdgeInsets.only(left: 10, top: 20),
color: Colors.deepOrange,
width: 150,
height: 70,
), Git Branch: container_widget
),
Git Diff: #4
Row( ) & Column( )
Column( )
● Column is a Multi-child layout widgets.
Child 1
Child 2
Columns( )
body: SafeArea(
child: Column(
children: [
Container(
child: Text(
'Idea!!!',
),
),
Image(
image: AssetImage('images/bulb.jpg'),
),
],
),
), Git Branch: column_widget
Git Diff: #5
Row( )
● Row is also a Multi-child layout widgets.
Child 1 Child 2
Row( )
body: SafeArea(
child: Column(
children: [
Container(
child: Text(
'Idea!!!',
),
),
Image(
image: AssetImage('images/bulb.jpg'),
),
],
),
), Git Branch: row_widget
Git Diff: #6
A must practice task, practice every thing from the article.
Direct Link
Round Shaped Image
Round Shaped Image
● Image can be rounded in various ways
● Container( ) can be decorated with round shaped box decorated image
● If the image is avatar, the CircleAvatar class can be used.
Width: 195
Height: 200
- borderRadius: 150
- border width: 8
Solution - (Rows & Columns)
Icon(
Icons.star_border,
color: Colors.red,
size: 50,
)
Good Read:
https://material.io/components/buttons/flutter#contained-button
Source1: https://flutter.dev/docs/development/ui/interactive
Source2: https://medium.com/flutter-community/flutter-stateful-vs-stateless-db325309deae
Stateful Widget
● For example, in a counter app, you have two widgets a text widget and a
button widget.
● Text widget should show updated counter value when button is pressed.
● In other words, on button press, app should increase the number and redraw
Text widget with new number.
● A stateful widget is dynamic: for example, it can change its appearance in
response to events triggered by user interactions or when it receives data.
● A widget’s state is stored in a State object, separating the widget’s state from
its appearance.
● setState() method help updating the state and redraw the UI.
Stateful Widget
● A widget’s state is stored in a State object,
separating the widget’s state from its appearance.
● setState() method help updating the state and
redraw the UI.
● PRO Tip: type stful in editor and press enter to
create a stateful widget.
Tips:
By using gestureDetector, we
will toggle question and
answer
We will remove this
button
Gesture Detector
Good read:
https://flutter.dev/docs/cookbook/design/themes
● APIs
○ Word meaning: https://www.dictionaryapi.com
○ Random Word: https://random-word-api.herokuapp.com/word
○ Audio: https://media.merriam-webster.com
○ How to find audio docs: https://dictionaryapi.com/products/json
Text Input
Text Input
● On type the word will be displayed on UI.
Source: https://betterprogramming.pub/stateful-widget-lifecycle-a01c44dc89b0
Widget Lifecycle
events ● Required method for
stateful widget.
Example
Source: https://betterprogramming.pub/stateful-widget-lifecycle-a01c44dc89b0
Widget Lifecycle
events
● When the object is
inserted into the tree this
method is automatically
executed after the class
Example constructor.
● Is called only once, when
the state object is created
for the first time.
● Tip: Use this method to
manage HTTP requests
and subscribe to streams
or any other object that
could change the data on
this widget.
Source: https://betterprogramming.pub/stateful-widget-lifecycle-a01c44dc89b0
Widget Lifecycle
events
● This method gets called
immediately after the
initState().
● It will also be called when
Example an object that a widget
depends on changes.
● Build method is always
called after this method,
that is why this method is
rarely used.
Source: https://betterprogramming.pub/stateful-widget-lifecycle-a01c44dc89b0
Example Widget Lifecycle
events
● This method is invoked
when value of constructor
variables are change
Source: https://betterprogramming.pub/stateful-widget-lifecycle-a01c44dc89b0
Widget Lifecycle
events
● This method will be called
some state is removed
from the tree, temporarily
Example or permanently.
● In some cases, the
framework will reinsert the
State object into another
part of the tree
Source: https://betterprogramming.pub/stateful-widget-lifecycle-a01c44dc89b0
Widget Lifecycle
events
● This method will be called
some state is removed
from the tree,
Example permanently.
● We use this method when
we remove permanently
like should release
resource created by an
object like stop animation
etc.
Source: https://betterprogramming.pub/stateful-widget-lifecycle-a01c44dc89b0
Source2: https://medium.flutterdevs.com/explore-widget-lifecycle-in-flutter-e36031c697d0
Displaying random word on App Load
● Coming back to our app feature!
● Functionality is to load a random word from one API and call another API to
get the meaning.
● We will be using https://random-word-api.herokuapp.com/word to get the
random word and usual API to get the meaning.
● This will happen on app load event.
● Most of the services can be used for free (ofcourse, quota applies!)
Setting up Firebase project
Setting up Firebase project
● First of all create a project at https://console.firebase.google.com/
● Using firebase console Create an App of Android, iOS, Web as required
● Setup FlutterFire in the app code, https://firebase.flutter.dev/docs/overview
○ Add dependencies in pubspec.yaml
■ firebase_core (Always required)
■ firebase_auth
■ Cloud_firestore
● Find and replace all the app name with the app name added at firebase e.g.
com.flutter.notification.app
● In your app/build.gradle, increase the minSdkVersion to 23
● Stop everything and rebuild the apk from android studio
Auth flow in Notice Board App
Auth flow
What screen to show first?
APP
● We need to structure a code in a
way that when app starts it lands
to a place where we it can
Landing Page
decide what screen to show.
● If user is logged in then Notice
Board Screen. If Not logged in If logged in
○ currentUser Property
● Landing Widget with stream If Not logged in If logged in
builder.
Login Screen Notice board Screen
Notice Board App - Basic Skeleton
● Created AuthService class (singleton)
● Integrated Signup function
● Firebase login integration
● Firebase signout integration
Git Branch:
notificaiton_app_firebase_integration
Git Diff: #37
Cloud Firestore + Flutter
Cloud Firestore + Flutter
● We will be storing notices data into firestore’s collection
● Firestore is a NoSQL document database.
● It is scalable and high performant.
● We will be using cloud_firestore sdk for flutter with our notice board app
NoSql Vs Sql
SQL vs NOSQL
NoSQL Collection
{
{
SQL NoSQL
SQL Table "id": "1",
"fname": "Aamir",
Table Collection "lname": "Pinger",
id fname lname gender "gender": "Male"
},
1 Aamir Pinger Male
Row Document {
"id": "2",
2 Irfan Ali Male Column Field "fname": "Irfan",
"lname": "Ali",
"gender": "Male"
Joins Embedded }
documents, Linking }
Add notifications
Add notifications
Git Branch:
notice_app_drawer_menu
Git Diff: #40
Hero Animation
Hero Animation
● Hero Animation help as transite
and animatedly transform widgets.
● It can be done to any widget by
simply wrapping it with Hero
Widget.
● By providing a same tag property
to Hero Widget of starting widget
and ending widget, flutter handles
all itself.
Git Branch:
● Let’s try Hero animation on logo at notice_board_app_hero_animation
drawer menu and about screen.
Git Diff: #41
Text Animation Library
Text Animation Library
● Let’s add some typewriter text effect on About Screen.
● We will be using animated_text_kit package.
Git Branch:notice_board_app_text_animation
Git Diff: #42
Other Animation Libraries
Good read:
Flutter cookbook:
https://docs.flutter.dev/cookbook#animation
Blog:
https://www.topcoder.com/thrive/articles/top-five-animation-packages-to-u
se-in-your-next-flutter-app
Other Animation Libraries
● Carousel_slider ● Charts_flutter
● Photo_view ● Page_transition
● Flutter_slidable ● Expandable
● Percent_indicator ● Flutter_swiper
● Lottie ● Syncfusion_flutter_charts
● Pin_code_fields ● Persistent_bottom_nav_bar
● Flutter_typeahead
● Table_calendar
Assignment - Shopping Cart App
Shopping Cart App
● Create a shopping cart app with
all the knowledge we have learn.
● More info at next slide.
Shopping Cart App
● This app will have 2 screens
○ Main screen
○ Checkout Screen
Git Repo:/flutter-shopping-app-example
Git Branch: main
setState
Riverpod BLoC / Rx
GetIt
MobX
Provider
Basic Lifting state up Example
● Let’s create a small example having deep widget tree
● We will lift the state up and pass the state to the child
widgets.
● We will also pass updateState method to the last widget
to modify state managed by top level widget
Source: https://codewithandrea.com/articles/flutter-state-management-riverpod/
Accessing data using Riverpod
● Package to add: flutter_riverpod
Git Branch:
riverpod_accessing_global_provider_value
Git Diff: #44
Types of Provider
● StateProvider
○ It's not much more flexible also, but you can actually change its value from the outside.
);
To access/modify the state
Source: https://codewithandrea.com/articles/flutter-state-management-riverpod/
Modifying global data using Riverpod
○ Provider and StateProvider are sufficient for simple use cases like the counter example.
○ But in more complex apps we often need to store some state along with some business
logic outside our widget classes.
○ The general idea is that you first create a class that holds some kind of state.
○ Then you create a StateNotifier that holds that state and makes updates/manipulations
(update notifications are sent when you manipulate state)
Source: https://codewithandrea.com/articles/flutter-state-management-riverpod/
State model
// To create a StateNotifier you first need to create class/model to hold
the state
class MyState {
MyState({required this.value});
// assign state a new value, in the following case new MyState instance.
// we can also directly return MyState instance instead of state.copyWith
void update(String newValue) => state = state.copyWith(newValue);
onPressed: () {
ref
.read(valueProvider.notifier)
.update("Hello from State Notifier Provider.");
},
To listen
Git Branch:
riverpod_accessing_state_stateful_widget
Git Diff: #48
Assignment - Transform
Shopping Cart App
With Riverpod
Shopping Cart App
● While creating shopping Cart
App, we have witnessed the
complexities of passing values
from one screen to another
screen.
● Modify this app and use
Riverpod to manage the data.
● Keep categories, product list,
and selected products in
separate states
● Access everything from state
BONUS - Assignment
TODO App
With Riverpod
Assignment - Todo App
● Similar to Notice Board App, create a To Do app
● Take task details and due date/time as user input.
● User input field MUST BE on separate screen/modal
● Display similar to notice board but if task due date
is passed, text colors should turn into red. TODO APP
● Only tasks added by logged in user should be
displayed.
● Task should be sorted as ascending order on due
date.
Thank you!
Thank you
https://youtu.be/vLtwQMKPuH0
/AamirPinger
/AamirPingerOfficial
/AamirPinger