Skip to content

Instantly share code, notes, and snippets.

@GIfatahTH
Last active June 1, 2020 20:23
Show Gist options
  • Save GIfatahTH/9151dcf78f3d1b4f7c2d0a92a7ddf3fc to your computer and use it in GitHub Desktop.
Save GIfatahTH/9151dcf78f3d1b4f7c2d0a92a7ddf3fc to your computer and use it in GitHub Desktop.
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)
? null
: () async {
await cartRM.setState(
(currentState) => CartState.add(
currentState,
item,
),
//cartRM has no observer yet
// silent no-observer exception
silent: true,
);
//Notify the global CatalogState ReactiveModel to rebuild.
RM.get<CatalogState>().notify();
},
splashColor: Theme.of(context).primaryColor,
child: cartRM.state.items.contains(item)
? Icon(Icons.check, semanticLabel: 'ADDED')
: Text('ADD'),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment