Skip to content

Instantly share code, notes, and snippets.

@renatoathaydes
Created May 31, 2019 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renatoathaydes/8f477a2dfd48d70eeeace03023716638 to your computer and use it in GitHub Desktop.
Save renatoathaydes/8f477a2dfd48d70eeeace03023716638 to your computer and use it in GitHub Desktop.
abstract class _State {
T use<T>(
T Function(_ChooseProviderState) useChooseProviderState,
T Function(_LoadingState) useLoadingState,
T Function(_ChooseAccountsState) useChooseAccountState,
T Function(_ImportingState) useImportingState) {
if (this is _ChooseProviderState) {
return useChooseProviderState(this);
}
if (this is _LoadingState) {
return useLoadingState(this);
}
if (this is _ChooseAccountsState) {
return useChooseAccountState(this);
}
if (this is _ImportingState) {
return useImportingState(this);
}
throw Exception('Invalid state');
}
}
class _ChooseProviderState extends _State {}
class _LoadingState extends _State {}
class _ChooseAccountsState extends _State {
final List<String> accounts;
final Set<String> selected;
_ChooseAccountsState(this.accounts) : selected = Set();
}
class _ImportingState extends _State {
final List<String> imported;
final List<String> failed;
final List<String> processing;
final List<String> queue;
_ImportingState(this.imported, this.failed, this.processing, this.queue);
}
// just to compile
class Widget {
final String something;
Widget(this.something);
}
class BuildContext {}
class MyWidget {
_State _state = _LoadingState();
@override
Widget build(BuildContext context) {
return _state.use(
(chooseProvider) => _buildChooseProvider(context, chooseProvider),
(loading) => _buildLoading(context, loading),
(chooseAccounts) => _buildChooseAccounts(context, chooseAccounts),
(importing) => _buildImporting(context, importing));
}
_buildChooseProvider(BuildContext context, _ChooseProviderState state) =>
Widget("choose provider");
_buildLoading(BuildContext context, _LoadingState state) => Widget("loading");
_buildChooseAccounts(BuildContext context, _ChooseAccountsState state) =>
Widget("choose accounts");
_buildImporting(BuildContext context, _ImportingState state) =>
Widget("importing");
}
main() {
print(MyWidget().build(BuildContext()).something);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment