Last active
March 16, 2020 02:07
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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