Skip to content

Instantly share code, notes, and snippets.

@GIfatahTH
Created January 10, 2020 17:36
Show Gist options
  • Save GIfatahTH/db866f0175d67b864990d9782f7fcc07 to your computer and use it in GitHub Desktop.
Save GIfatahTH/db866f0175d67b864990d9782f7fcc07 to your computer and use it in GitHub Desktop.
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>();
return Row(
children: <Widget>[
StateBuilder(
models: [postsServiceRM],
builder: (context, snapshot) {
//NOTE2: Optimizing rebuild. Only Text is rebuild
return Text('Likes ${postsServiceRM.state.getPostLikes(postId)}');
},
),
MaterialButton(
color: Colors.white,
child: Icon(Icons.thumb_up),
onPressed: () {
//NOTE3: incrementLikes is a synchronous method so we do not expect errors
postsServiceRM.setState((state) => state.incrementLikes(postId));
},
)
],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment