Skip to content

Instantly share code, notes, and snippets.

class CatalogState {
CatalogState({
@required this.repository,
});
//
final CatalogRepository repository;
List<Item> items = [];
Future<CatalogState> getItems() async {
items = await repository.fetchItems();
class _AddButton extends StatelessWidget {
final Item item;
const _AddButton({Key key, @required this.item}) : super(key: key);
@override
Widget build(BuildContext context) {
// Get the registered cartState ReactiveModel
final cartRM = RM.get<CartState>();
return FlatButton(
onPressed: cartRM.state.items.contains(item)
class _MyListItem extends StatelessWidget {
final Item item;
_MyListItem(this.item, {Key key}) : super(key: key);
//with states_rebuilder you can get the current them without BuildContext
final textTheme = RM.theme.textTheme.headline6;
void _displaySnackBar() {
//with states_rebuilder you can get the active ScaffoldState without
class MyCatalog extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: WhenRebuilder<CatalogState>(
// Create a new ReactiveModel and subscribe to this WhenRebuilder
// and invoke getItems method.
//
observe: () => RM.get<CatalogState>().future(
(CatalogState s, Future<CatalogState> _) => s.getItems(),
@GIfatahTH
GIfatahTH / main.dart
Created May 28, 2020 09:42
states_rebuilder_shopper
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Injector(
inject: [
// Inject the CatalogState.
@immutable
class CartState {
CartState({this.items});
// List of items in the cart.
final List<Item> items;
/// The current total price of all items.
int get totalPrice =>
items.fold(0, (total, current) => total + current.price);
@immutable
class CatalogState {
CatalogState({
@required this.repository,
@required this.items,
});
//
final CatalogRepository repository;
final List<Item> items;
class Comments extends StatelessWidget {
final int postId;
Comments(this.postId);
@override
Widget build(BuildContext context) {
return Injector(
//NOTE1: Inject CommentsService
inject: [Inject(() => CommentsService(api: Injector.get()))],
builder: (context) {
class LikeButton extends StatelessWidget {
LikeButton({
@required this.postId,
});
final int postId;
@override
Widget build(BuildContext context) {
//NOTE1: get reactiveModel of PostsService
final postsServiceRM = Injector.getAsReactive<PostsService>();
class PostPage extends StatelessWidget {
PostPage({this.post});
final Post post;
//NOTE1: Get the logged user
final user = Injector.get<AuthenticationService>().user;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: backgroundColor,