Skip to content

The sample demonstrates how you can display a toast notification (SnackBar) in your Flutter + Redux solution.

License

Notifications You must be signed in to change notification settings

Pavel-Sulimau/flutter_redux_snackbar

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

flutter_redux_snackbar

Purpose:

The sample demonstrates how you can display a toast notification (SnackBar) in your Flutter + Redux solution.

Demo:

The piece of particular interest:

class ErrorNotifier extends StatelessWidget {
  ErrorNotifier({
    @required this.child,
  });

  final Widget child;

  @override
  Widget build(BuildContext context) {
    return StoreConnector<AppState, _ViewModel>(
      converter: (store) => _ViewModel.fromStore(store),
      builder: (context, vm) => child,
      onWillChange: (vm) {
        if (vm.error != null) {
          vm.markErrorAsHandled();
          Scaffold.of(context).showSnackBar(
            SnackBar(
              content: Text(vm.error.toString()),
            ),
          );
        }
      },
      distinct: true,
    );
  }
}

class _ViewModel {
  _ViewModel({
    this.markErrorAsHandled,
    this.error,
  });

  final Function markErrorAsHandled;
  final Exception error;

  static _ViewModel fromStore(Store<AppState> store) {
    return _ViewModel(
      markErrorAsHandled: () => store.dispatch(ErrorHandledAction()),
      error: store.state.error,
    );
  }

  @override
  int get hashCode => error.hashCode;

  @override
  bool operator ==(other) =>
      identical(this, other) || other is _ViewModel && other.error == this.error;
}

About

The sample demonstrates how you can display a toast notification (SnackBar) in your Flutter + Redux solution.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published