simple flutter state management with setState and dum class that hold logic
import 'package:flutter/material.dart'; | |
final Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatefulWidget { | |
@override | |
Mystate createState() => Mystate(); | |
} | |
class Mystate extends State<MyApp> { | |
var counter = Counter(); | |
var currentCount; | |
@override | |
initState() { | |
currentCount = counter.counter; | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue), | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
drawer: Drawer(), | |
appBar: | |
AppBar(centerTitle: true, title: Text("Simple State Management")), | |
body: Column(children: [ | |
Center( | |
child: MyWidget(currentCount), | |
), | |
Row(children: [ | |
FlatButton.icon( | |
icon: Icon(Icons.minimize), | |
onPressed: () { | |
counter.decrement(); | |
setState(() { | |
currentCount = counter.counter; | |
}); | |
}, | |
label: Text("decrement")), | |
FlatButton.icon( | |
icon: Icon(Icons.add), | |
onPressed: () { | |
counter.increment(); | |
setState(() { | |
currentCount = counter.counter; | |
}); | |
}, | |
label: Text("increment")) | |
]) | |
]), | |
), | |
); | |
} | |
} | |
class MyWidget extends StatelessWidget { | |
final int numl; | |
MyWidget(this.numl); | |
@override | |
Widget build(BuildContext context) { | |
return Text('the current count is: $numl', | |
style: TextStyle(color: Colors.red)); | |
} | |
} | |
class Counter { | |
var counter = 0; | |
void increment() { | |
counter++; | |
} | |
void decrement() { | |
counter--; | |
} | |
int getCurrentCount() { | |
return counter; | |
} | |
} | |
class Observer { | |
Map<dynamic, dynamic> repository = {}; | |
Observer() { | |
register(Counter, Counter()); | |
} | |
void register(name, object) { | |
repository[name] = object; | |
} | |
fetch(name) => repository[name]; | |
} |
This comment has been minimized.
This comment has been minimized.
contact me at google : wdalhajana50@gmail.com. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
this is simple state management with trying to implement multi Blocs in one app by using third class called "Ovserver"
i will regist all blocs over there by using map
then access the specific bloc by using Provider package