Skip to content

Instantly share code, notes, and snippets.

@kururu-abdo
Last active March 16, 2020 02:07
Show Gist options
  • Save kururu-abdo/ab1f8bb741b5db21c81c657f36f60152 to your computer and use it in GitHub Desktop.
Save kururu-abdo/ab1f8bb741b5db21c81c657f36f60152 to your computer and use it in GitHub Desktop.
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];
}
@kururu-abdo
Copy link
Author

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

@kururu-abdo
Copy link
Author

contact me at google : [email protected].
whatsapp: 249966302389

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment