Created
July 22, 2020 19:52
-
-
Save matejthetree/ebfe7bcfe475fd83c1dc980c09886faf to your computer and use it in GitHub Desktop.
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'; | |
import 'package:provider/provider.dart'; | |
import 'package:rxdart/rxdart.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MultiProvider( | |
providers: [ | |
Provider<Store>( | |
lazy: false, | |
create: (context) => Store(), | |
), | |
Provider<SeededStore>( | |
lazy: false, | |
create: (context) => SeededStore(), | |
), | |
StreamProvider<SeededCount>( | |
create: (context) => | |
Provider.of<SeededStore>(context, listen: false).counter, | |
), | |
StreamProvider<Count>( | |
create: (context) => | |
Provider.of<Store>(context, listen: false).counter, | |
), | |
], | |
child: MaterialApp( | |
home: BehaviourSubjectExample(), | |
), | |
); | |
} | |
} | |
//UI | |
class BehaviourSubjectExample extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
ListTile( | |
title: Text("Counter"), | |
onTap: () => Navigator.push( | |
context, MaterialPageRoute(builder: (context) => CountPage())), | |
), | |
ListTile( | |
title: Text("Seeded Counter"), | |
onTap: () => Navigator.push(context, | |
MaterialPageRoute(builder: (context) => SeededCountPage())), | |
), | |
], | |
), | |
); | |
} | |
} | |
class SeededCountPage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Consumer<SeededCount>( | |
builder: (_, count, __) { | |
if (count == null) { | |
print(" SEEDED COUNT IS NULL"); | |
return Container(); | |
} | |
return Scaffold( | |
body: Center( | |
child: Text(count.counter.toString()), | |
), | |
floatingActionButton: FloatingActionButton( | |
child: Icon(Icons.add), | |
onPressed: () => IncreaseCount.of(context).increaseSeededCount(), | |
), | |
); | |
}, | |
); | |
} | |
} | |
class CountPage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Consumer<Count>( | |
builder: (_, count, __) { | |
if (count == null) { | |
print(" COUNT IS NULL"); | |
return Container(); | |
} | |
return Scaffold( | |
body: Center( | |
child: Text(count.counter.toString()), | |
), | |
floatingActionButton: FloatingActionButton( | |
child: Icon(Icons.add), | |
onPressed: () => IncreaseCount.of(context).increaseCount(), | |
), | |
); | |
}, | |
); | |
} | |
} | |
//Stores | |
class SeededStore { | |
BehaviorSubject<SeededCount> counter = BehaviorSubject.seeded(SeededCount(0)); | |
void dispose() => counter.close(); | |
void increaseSeededCount() { | |
counter.add(SeededCount(counter.value.counter + 1)); | |
} | |
} | |
class Store { | |
BehaviorSubject<Count> counter = BehaviorSubject(); | |
void dispose() => counter.close(); | |
Store() { | |
Future.delayed(Duration(milliseconds: 300), () { | |
counter.add(Count(0)); | |
}); | |
} | |
void increaseCount() { | |
counter.add(Count(counter.value.counter + 1)); | |
} | |
} | |
//Observables | |
class SeededCount { | |
final int counter; | |
SeededCount(this.counter); | |
} | |
class Count { | |
final int counter; | |
Count(this.counter); | |
} | |
//Action | |
class IncreaseCount { | |
final Store _store; | |
final SeededStore _seededStore; | |
IncreaseCount(this._store, this._seededStore); | |
factory IncreaseCount.of(BuildContext context) => IncreaseCount( | |
Provider.of(context, listen: false), | |
Provider.of(context, listen: false), | |
); | |
void increaseCount() => _store.increaseCount(); | |
void increaseSeededCount() => _seededStore.increaseSeededCount(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment