Skip to content

Instantly share code, notes, and snippets.

@xinthink
Created March 5, 2020 09:54
Show Gist options
  • Save xinthink/5a292f5d1048bfd8577b2c41b861f51f to your computer and use it in GitHub Desktop.
Save xinthink/5a292f5d1048bfd8577b2c41b861f51f to your computer and use it in GitHub Desktop.
// when pressed, dissmiss the bottom sheet and return a command
IconButton(
icon: const Icon(Icons.archive),
tooltip: 'Archive',
onPressed: () => Navigator.pop(context, NoteStateUpdateCommand(
id: _note.id,
uid: uid,
from: _note.state, // the current state
to: NoteState.archived, // the transition target
)),
),
// receiving a command
void _showNoteBottomSheet(BuildContext context) async {
final command = await showModalBottomSheet<NoteCommand>(
...
);
if (command != null) {
processNoteCommand(_scaffoldKey.currentState, command);
}
}
/// Processes the given [command], displays a SnackBar.
Future<void> processNoteCommand(ScaffoldState scaffoldState, NoteCommand command) async {
await command.execute();
final msg = command.message;
if (mounted && msg?.isNotEmpty == true) {
scaffoldState?.showSnackBar(SnackBar(
content: Text(msg),
action: SnackBarAction(
label: 'Undo',
onPressed: () => command.revert(),
),
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment