Created
September 12, 2019 17:35
-
-
Save mikaelj/b33e6a713bdd66d0a2bcbf5b58c1ba4a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* pseudo-ish code | |
The code can be somewhat simplified by showing the dialog directly in onPressed(). | |
But since the dialog can't be dismissed w/o a BuildContext, I'd still have to listen for a state, | |
so it's cleaner to treat both show/dismiss the same way, i.e. by listening to DataBloc's state change. | |
The problem with all of this, as you can see, is that the many of the events and states serve no other | |
purpose than to display the dialog. Specifically, ItemTransmogrifyStartedEvent/State, ItemTransmogrifyDoneEvent/State. | |
My question here: how would you go about this? | |
*/ | |
// this handles all items + extra surrounding data | |
DataBloc { | |
mapEventToState(event) { | |
if (event is DataTransmogrifyRequestEvent) { | |
_transmogrify(event.data) | |
} | |
} | |
_transmogrify(data) { | |
yield DataTransmogrifyStartedState(); | |
// perform transmogrification with parameter 'data' | |
yield DataTransmogrifyDoneState(); | |
} | |
} | |
// this handles the ItemsWidget | |
ItemsBloc { | |
List<Item> items; | |
ctor() { | |
dataBloc.listen((state) { | |
if (state == DataTransmogrifyStartedState) { | |
dispatch(ItemTransmogrifyStartedEvent); | |
} | |
}) | |
mapEventToState(event) { | |
if (event is ItemButtonPressedEvent) { | |
dataBloc.dispatch(DataTransmogrifyRequestEvent); | |
} | |
if (event is ItemTransmogrifyStartedEvent) { | |
dispatch(ItemTransmogrifyStartedState); | |
} | |
if (event is ItemTransmogrifyDoneEvent) { | |
dispatch(ItemTransmogrifyDoneState); | |
} | |
} | |
} | |
ItemsWidget { | |
build() { | |
return BlocListener<ItemBloc, ItemState>( | |
listener: (context, state) { | |
if (state is ItemTransmogrifyStartedState) { | |
showDialog(...) | |
} | |
if (state is ItemTransmogrifyDoneState) { | |
Navigator....pop(); | |
} | |
}, | |
build: (context, state) { | |
return Button( | |
onPressed: () => itemsBloc.dispatch(ItemButtonPressedEvent) | |
); | |
} | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment