SlideShare a Scribd company logo
MODULE 4 (1).pptx flutter dart programming
TRAINING
Transforming Lives Through
Skilling
State
Management
State management is a critical concept in Flutter development. It involves maintaining and
updating the state of an application in response to user interactions or external changes
while ensuring that the UI reflects the current state.
What is a State?
â—Ź State refers to data or information that can change during the lifetime of the
application.
â—Ź Example of state:
â—‹ The current user input in a text field.
â—‹ Whether a checkbox is checked or not.
â—‹ The current screen/page in a navigation flow.
Lorem Ipsum is simply dummy text of the
printing and typesetting industry. Lorem ipsum
has been industry’s standard dummy text ever
since the 1500s, when an unknown printer took
a gallery of ty. Lorem Ipsum is simply dummy
text of the printing and typesetting industry.
Lorem ipsum has been industry’s standard
dummy text ever since the 1500s, when an
unknown printer took a gallery of type.
Image
â—Ź Stateless Widget: Has immutable state. Its state does not change once created.
class MyStatelessWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text('I am stateless');
}
}
Stateless vs Stateful Widgets
Stateful Widget: Has mutable state that can change during its lifecycle.
class MyStatefulWidget extends StatefulWidget {
@override
_MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
int counter = 0;
@override
Widget build(BuildContext context) {
return Column(
children: [
Image
Image Image Image
Text('Counter: $counter'),
ElevatedButton(
onPressed: () {
setState(() {
counter++;
});
},
child: Text('Increment'),
),
],
);
}
}
Why is State Management Important?
â—Ź Ensures a clear separation of UI and business logic.
● Provides a structured way to manage the application’s dynamic data.
â—Ź Makes the code easier to read, maintain, and debug.
Types of State in Flutter
a) Ephemeral (Local) State
â—Ź State that is specific to a single widget and is temporary.
â—Ź Example: Whether a checkbox is checked or not.
â—Ź Managed using StatefulWidget.
b) App-Wide (Global) State
â—Ź State shared across multiple widgets or screens.
â—Ź Example: User authentication status or theme settings.
â—Ź Requires external state management solutions.
Common State Management Approaches
a) setState (Built-in State Management)
â—Ź Simplest and most basic way to manage state.
â—Ź Used for ephemeral state in StatefulWidget.
class CounterApp extends StatefulWidget {
@override
_CounterAppState createState() => _CounterAppState();
}
class _CounterAppState extends State<CounterApp> {
int counter = 0;
void incrementCounter() {
setState(() {
counter++;
});
}
@override
Widget build(BuildContext context) {
return Column(
children: [
Text('Counter: $counter'),
ElevatedButton(
onPressed: incrementCounter,
child: Text('Increment'),
),
],
);
}
}
b) InheritedWidget and InheritedModel
â—Ź Used for propagating state to descendant widgets.
â—Ź Typically used for small-scale state sharing.
class CounterProvider extends InheritedWidget {
final int counter;
final Function increment;
CounterProvider({required this.counter, required this.increment, required Widget child})
: super(child: child);
@override
bool updateShouldNotify(InheritedWidget oldWidget) => true;
static CounterProvider? of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<CounterProvider>();
}
}
c) Provider (Recommended by Flutter)
â—Ź A wrapper around InheritedWidget that makes state management simpler and more scalable.
â—Ź Widely used for both ephemeral and global state management.
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class Counter with ChangeNotifier {
int value = 0;
void increment() {
value++;
notifyListeners();
}
}
void main() {
runApp(
ChangeNotifierProvider(
create: (context) => Counter(),
child: MyApp(),
),
);
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: CounterScreen(),
);
}
}
class CounterScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final counter = Provider.of<Counter>(context);
return Scaffold(
appBar: AppBar(title: Text('Provider Example')),
body: Center(
child: Text('Counter: ${counter.value}'),
),
floatingActionButton: FloatingActionButton(
onPressed: counter.increment,
child: Icon(Icons.add),
child: Icon(Icons.add),
),
);
}
}
Hands-On Activities
â—Ź Activity 1: Build a counter app using `setState`.
â—Ź Activity 2: Create a to-do list app with Provider to manage the list of tasks.
â—Ź Activity 3: Extend the to-do list to include filters (e.g., completed, pending tasks).
THANK YOU
+91 78150 95095 codemithra@ethnus.com www.codemithra.com

More Related Content

Similar to MODULE 4 (1).pptx flutter dart programming (20)

PDF
The battle between the states (all about flutter stateless & stateful widgets...
Katy Slemon
 
PPTX
Basic iOS Training with SWIFT - Part 3
Manoj Ellappan
 
PPTX
Flutter-Festivals Day-2.pptx
DSCVSSUT
 
PPTX
STATE-MANAGEMENT-IN-FLUTTER-TECHNOLOGY.pptx
subairahamed52
 
PDF
Flutter State Management Using GetX.pdf
Katy Slemon
 
PPT
Jar chapter 5_part_ii
Reham Maher El-Safarini
 
PPTX
Windows Store app using XAML and C#: Enterprise Product Development
Mahmoud Hamed Mahmoud
 
PPTX
Mobile Application Development class 003
Dr. Mazin Mohamed alkathiri
 
PPTX
Creating GUI.pptx Gui graphical user interface
pikachu02434
 
PDF
Unity3D Scripting: State Machine
Sperasoft
 
PPTX
Modern app development with Jetpack Compose.pptx
Md Shamsul Arafin Mahtab
 
PDF
Plain react, hooks and/or Redux ?
Jörn Dinkla
 
PPTX
Force Flutter to Rebuild or Redraw All Widgets.pptx
RubenGray1
 
PPTX
FlutterArchitecture FlutterArchitecture.ppt
KevinNemo
 
PPT
Hello Android
Trong Dinh
 
PPTX
FlutterArchitecture FlutterDevelopement (1).pptx
hw813301
 
PDF
BLoC - Be Reactive in flutter
Giacomo Ranieri
 
PDF
Flutter
Dave Chao
 
PPTX
MODULE 3.pptxMODULE 3.pptxMODULE 3.pptxMODULE 3.pptx
trwdcn
 
PPTX
Frontend training
Adrian Caetano
 
The battle between the states (all about flutter stateless & stateful widgets...
Katy Slemon
 
Basic iOS Training with SWIFT - Part 3
Manoj Ellappan
 
Flutter-Festivals Day-2.pptx
DSCVSSUT
 
STATE-MANAGEMENT-IN-FLUTTER-TECHNOLOGY.pptx
subairahamed52
 
Flutter State Management Using GetX.pdf
Katy Slemon
 
Jar chapter 5_part_ii
Reham Maher El-Safarini
 
Windows Store app using XAML and C#: Enterprise Product Development
Mahmoud Hamed Mahmoud
 
Mobile Application Development class 003
Dr. Mazin Mohamed alkathiri
 
Creating GUI.pptx Gui graphical user interface
pikachu02434
 
Unity3D Scripting: State Machine
Sperasoft
 
Modern app development with Jetpack Compose.pptx
Md Shamsul Arafin Mahtab
 
Plain react, hooks and/or Redux ?
Jörn Dinkla
 
Force Flutter to Rebuild or Redraw All Widgets.pptx
RubenGray1
 
FlutterArchitecture FlutterArchitecture.ppt
KevinNemo
 
Hello Android
Trong Dinh
 
FlutterArchitecture FlutterDevelopement (1).pptx
hw813301
 
BLoC - Be Reactive in flutter
Giacomo Ranieri
 
Flutter
Dave Chao
 
MODULE 3.pptxMODULE 3.pptxMODULE 3.pptxMODULE 3.pptx
trwdcn
 
Frontend training
Adrian Caetano
 

More from trwdcn (20)

PPTX
Unit 3 AI.pptxArtificial Intelligence unit 1 notes - intelligent agent.pptx
trwdcn
 
PPTX
Unit 2 AI.pptxUnit 2 AI.pptxUnit 2 AI.pptx
trwdcn
 
PPTX
jsonHMI ppt.pptxjsonHMI ppt.pptxjsonHMI ppt.pptx
trwdcn
 
PPTX
hmi ppt.pptxhmi ppt.pptxhmi ppt.pptxhmi ppt.pptx
trwdcn
 
PPTX
JSON Presentation.pptx JSON Presentation.pptx
trwdcn
 
PPTX
Wireless USB.pptx Wireless USB.pptx Wireless USB.pptx
trwdcn
 
PPTX
Module 2 - Dart Programming.pptx MODULE 3.pptx
trwdcn
 
PPTX
MODULE 5.pptx MODULE 4 (1).pptx flutter dart programming
trwdcn
 
PPTX
Practical Python.pptx Practical Python.pptx
trwdcn
 
PPTX
function ppt.pptx function ppt.pptx function ppt.pptx
trwdcn
 
PPTX
D 10 mark.pptx D 10 mark.pptx D 10 mark.pptx
trwdcn
 
PPTX
DAY1 PPT.pptx DAY1 PPT.pptx DAY1 PPT.pptx
trwdcn
 
PPTX
Wireless_Network_Experiments.pptx Wireless_Network_Experiments
trwdcn
 
PPTX
Introduction to Python.pptx Introduction to Python.pptx
trwdcn
 
PPTX
Storage Virtualization.pptx Storage Virtualization.pptx
trwdcn
 
PDF
Module 2 Azure Services for Web Developement.pdf
trwdcn
 
PPT
Artificial Intelligence AI Class notes unit 1
trwdcn
 
PPT
introdution-to-html html tags heml basics
trwdcn
 
PPTX
project Management system details and maintainence
trwdcn
 
PPTX
Writing research proposal dr. ramanathan.pptx
trwdcn
 
Unit 3 AI.pptxArtificial Intelligence unit 1 notes - intelligent agent.pptx
trwdcn
 
Unit 2 AI.pptxUnit 2 AI.pptxUnit 2 AI.pptx
trwdcn
 
jsonHMI ppt.pptxjsonHMI ppt.pptxjsonHMI ppt.pptx
trwdcn
 
hmi ppt.pptxhmi ppt.pptxhmi ppt.pptxhmi ppt.pptx
trwdcn
 
JSON Presentation.pptx JSON Presentation.pptx
trwdcn
 
Wireless USB.pptx Wireless USB.pptx Wireless USB.pptx
trwdcn
 
Module 2 - Dart Programming.pptx MODULE 3.pptx
trwdcn
 
MODULE 5.pptx MODULE 4 (1).pptx flutter dart programming
trwdcn
 
Practical Python.pptx Practical Python.pptx
trwdcn
 
function ppt.pptx function ppt.pptx function ppt.pptx
trwdcn
 
D 10 mark.pptx D 10 mark.pptx D 10 mark.pptx
trwdcn
 
DAY1 PPT.pptx DAY1 PPT.pptx DAY1 PPT.pptx
trwdcn
 
Wireless_Network_Experiments.pptx Wireless_Network_Experiments
trwdcn
 
Introduction to Python.pptx Introduction to Python.pptx
trwdcn
 
Storage Virtualization.pptx Storage Virtualization.pptx
trwdcn
 
Module 2 Azure Services for Web Developement.pdf
trwdcn
 
Artificial Intelligence AI Class notes unit 1
trwdcn
 
introdution-to-html html tags heml basics
trwdcn
 
project Management system details and maintainence
trwdcn
 
Writing research proposal dr. ramanathan.pptx
trwdcn
 
Ad

Recently uploaded (20)

DOCX
MUSIC AND ARTS 5 DLL MATATAG LESSON EXEMPLAR QUARTER 1_Q1_W1.docx
DianaValiente5
 
PDF
Rapid Mathematics Assessment Score sheet for all Grade levels
DessaCletSantos
 
PDF
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 
PPT
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
ErlizaRosete
 
PPTX
JSON, XML and Data Science introduction.pptx
Ramakrishna Reddy Bijjam
 
PDF
Nanotechnology and Functional Foods Effective Delivery of Bioactive Ingredien...
rmswlwcxai8321
 
PPTX
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
 
PDF
Supply Chain Security A Comprehensive Approach 1st Edition Arthur G. Arway
rxgnika452
 
PPTX
Elo the HeroTHIS IS A STORY ABOUT A BOY WHO SAVED A LITTLE GOAT .pptx
JoyIPanos
 
PDF
Andreas Schleicher_Teaching Compass_Education 2040.pdf
EduSkills OECD
 
PPTX
How to Create & Manage Stages in Odoo 18 Helpdesk
Celine George
 
PDF
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
PPTX
Urban Hierarchy and Service Provisions.pptx
Islamic University of Bangladesh
 
PDF
Wikinomics How Mass Collaboration Changes Everything Don Tapscott
wcsqyzf5909
 
PPTX
Photo chemistry Power Point Presentation
mprpgcwa2024
 
PPTX
Martyrs of Ireland - who kept the faith of St. Patrick.pptx
Martin M Flynn
 
PDF
VCE Literature Section A Exam Response Guide
jpinnuck
 
PPTX
Peer Teaching Observations During School Internship
AjayaMohanty7
 
PDF
The Power of Compound Interest (Stanford Initiative for Financial Decision-Ma...
Stanford IFDM
 
PDF
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
MUSIC AND ARTS 5 DLL MATATAG LESSON EXEMPLAR QUARTER 1_Q1_W1.docx
DianaValiente5
 
Rapid Mathematics Assessment Score sheet for all Grade levels
DessaCletSantos
 
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
ErlizaRosete
 
JSON, XML and Data Science introduction.pptx
Ramakrishna Reddy Bijjam
 
Nanotechnology and Functional Foods Effective Delivery of Bioactive Ingredien...
rmswlwcxai8321
 
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
 
Supply Chain Security A Comprehensive Approach 1st Edition Arthur G. Arway
rxgnika452
 
Elo the HeroTHIS IS A STORY ABOUT A BOY WHO SAVED A LITTLE GOAT .pptx
JoyIPanos
 
Andreas Schleicher_Teaching Compass_Education 2040.pdf
EduSkills OECD
 
How to Create & Manage Stages in Odoo 18 Helpdesk
Celine George
 
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
Urban Hierarchy and Service Provisions.pptx
Islamic University of Bangladesh
 
Wikinomics How Mass Collaboration Changes Everything Don Tapscott
wcsqyzf5909
 
Photo chemistry Power Point Presentation
mprpgcwa2024
 
Martyrs of Ireland - who kept the faith of St. Patrick.pptx
Martin M Flynn
 
VCE Literature Section A Exam Response Guide
jpinnuck
 
Peer Teaching Observations During School Internship
AjayaMohanty7
 
The Power of Compound Interest (Stanford Initiative for Financial Decision-Ma...
Stanford IFDM
 
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
Ad

MODULE 4 (1).pptx flutter dart programming

  • 4. State management is a critical concept in Flutter development. It involves maintaining and updating the state of an application in response to user interactions or external changes while ensuring that the UI reflects the current state. What is a State? â—Ź State refers to data or information that can change during the lifetime of the application. â—Ź Example of state: â—‹ The current user input in a text field. â—‹ Whether a checkbox is checked or not. â—‹ The current screen/page in a navigation flow.
  • 5. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem ipsum has been industry’s standard dummy text ever since the 1500s, when an unknown printer took a gallery of ty. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem ipsum has been industry’s standard dummy text ever since the 1500s, when an unknown printer took a gallery of type. Image â—Ź Stateless Widget: Has immutable state. Its state does not change once created. class MyStatelessWidget extends StatelessWidget { @override Widget build(BuildContext context) { return Text('I am stateless'); } } Stateless vs Stateful Widgets
  • 6. Stateful Widget: Has mutable state that can change during its lifecycle. class MyStatefulWidget extends StatefulWidget { @override _MyStatefulWidgetState createState() => _MyStatefulWidgetState(); } class _MyStatefulWidgetState extends State<MyStatefulWidget> { int counter = 0; @override Widget build(BuildContext context) { return Column( children: [ Image
  • 7. Image Image Image Text('Counter: $counter'), ElevatedButton( onPressed: () { setState(() { counter++; }); }, child: Text('Increment'), ), ], );
  • 8. } } Why is State Management Important? â—Ź Ensures a clear separation of UI and business logic. â—Ź Provides a structured way to manage the application’s dynamic data. â—Ź Makes the code easier to read, maintain, and debug. Types of State in Flutter a) Ephemeral (Local) State â—Ź State that is specific to a single widget and is temporary. â—Ź Example: Whether a checkbox is checked or not. â—Ź Managed using StatefulWidget.
  • 9. b) App-Wide (Global) State â—Ź State shared across multiple widgets or screens. â—Ź Example: User authentication status or theme settings. â—Ź Requires external state management solutions. Common State Management Approaches a) setState (Built-in State Management) â—Ź Simplest and most basic way to manage state. â—Ź Used for ephemeral state in StatefulWidget. class CounterApp extends StatefulWidget { @override _CounterAppState createState() => _CounterAppState();
  • 10. } class _CounterAppState extends State<CounterApp> { int counter = 0; void incrementCounter() { setState(() { counter++; }); } @override Widget build(BuildContext context) { return Column(
  • 11. children: [ Text('Counter: $counter'), ElevatedButton( onPressed: incrementCounter, child: Text('Increment'), ), ], ); } }
  • 12. b) InheritedWidget and InheritedModel â—Ź Used for propagating state to descendant widgets. â—Ź Typically used for small-scale state sharing. class CounterProvider extends InheritedWidget { final int counter; final Function increment; CounterProvider({required this.counter, required this.increment, required Widget child}) : super(child: child); @override bool updateShouldNotify(InheritedWidget oldWidget) => true; static CounterProvider? of(BuildContext context) {
  • 13. return context.dependOnInheritedWidgetOfExactType<CounterProvider>(); } } c) Provider (Recommended by Flutter) â—Ź A wrapper around InheritedWidget that makes state management simpler and more scalable. â—Ź Widely used for both ephemeral and global state management. import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; class Counter with ChangeNotifier { int value = 0;
  • 14. void increment() { value++; notifyListeners(); } } void main() { runApp( ChangeNotifierProvider( create: (context) => Counter(), child: MyApp(), ), );
  • 15. ); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: CounterScreen(), ); } }
  • 16. class CounterScreen extends StatelessWidget { @override Widget build(BuildContext context) { final counter = Provider.of<Counter>(context); return Scaffold( appBar: AppBar(title: Text('Provider Example')), body: Center( child: Text('Counter: ${counter.value}'), ), floatingActionButton: FloatingActionButton( onPressed: counter.increment, child: Icon(Icons.add),
  • 17. child: Icon(Icons.add), ), ); } } Hands-On Activities â—Ź Activity 1: Build a counter app using `setState`. â—Ź Activity 2: Create a to-do list app with Provider to manage the list of tasks. â—Ź Activity 3: Extend the to-do list to include filters (e.g., completed, pending tasks).
  • 18. THANK YOU +91 78150 95095 [email protected] www.codemithra.com