Este conteúdo não está disponível em sua língua ainda.
In the following tutorial, we’re going to build a Counter in AngularDart using the Bloc library.
Setup
We’ll start off by creating a brand new AngularDart project with stagehand.
If you don’t have stagehand installed, activate it via:
Then generate a new project via:
We can then go ahead and replace the contents of pubspec.yaml with:
and then install all of our dependencies
Our counter app is just going to have two buttons to increment/decrement the counter value and an element to display the current value. Let’s get started designing the CounterEvents.
Counter Bloc
Since our counter’s state can be represented by an integer we don’t need to create a custom class and we can co-locate the events and bloc.
Counter App
Now that we have our CounterBloc fully implemented, we can get started creating our AngularDart App Component.
Our app.component.dart should look like:
and our app.component.html should look like:
Counter Page
Finally, all that’s left is to build our Counter Page Component.
Our counter_page_component.dart should look like:
Lastly, our counter_page_component.html should look like:
That’s it! We’ve separated our presentation layer from our business logic layer. Our CounterPageComponent has no idea what happens when a user presses a button; it just adds an event to notify the CounterBloc. Furthermore, our CounterBloc has no idea what is happening with the state (counter value); it’s simply converting the CounterEvents into integers.