Part 2 - App architecture

In my last article, I wrote about different ways to send and share data across multiple widgets and pages. In this case, we are going to use a little more advanced approach: an instance of a singleton class named appState will be provided to the widgets tree by using an InheritedWidget provider ( AppStateProvider ), this will hold the state of the app, some business logic, and the instance of the only BLoC which handles the “quiz part” of the app. So, in the end, it will be a sort of a mix between the singleton and the BLoC pattern.

Inside each widget it is possible to get the instance of the AppState class by calling:

final appState = AppStateProvider.of<AppState>(context);

1 - main.dart

This is the entry point of the app. The class App is a stateless widget where it is declared the instance of the AppState class, and where, by using the AppStateProvider, this is then provided to the widgets tree. The appState instance will be disposed, closing all the streams, in the dispose method of the AppStateProvider class.

The MaterialApp widget is wrapped inside a ValueBuilder widget so that, every time a new theme is selected, the entire widgets tree rebuilds, updating the theme.

2 - State management

As said before, the appState instance holds the state of the app. This class will be used for: