Flutter: bloc, how to show an alert dialog

前端 未结 5 1725
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-19 19:01

I´m new in the bloc pattern and stream stuff. I want to show up an alert dialog when I press a button, but I can´t find a way to do it. Actually my code is:

Widg         


        
5条回答
  •  -上瘾入骨i
    2021-02-19 19:55

    Here is what I did, it might be wrong as I'm also new to flutter. But works for my scenario.

    Widget build(BuildContext context) {
    final authBloc = BlocProvider.of(context);
    
    authBloc.outServerResponse.listen((serverResponse) {
      if (serverResponse.status == 'success') {
        _navigateToLogin();
      } else {
        _showSnakBar(serverResponse.message);
      }
    });
    .... Rest of the code which returns the widget, 
    which in my case is form widget with button for submitting as follows,
    onPressed: () {
      if (_formKey.currentState.validate()) {
          _formKey.currentState.save();
          authBloc.processRegister.add(_registrationData.toMap());
      }
    }
    

    outServerResponse is the stream that outputs after finishing API POST call.

    authBloc.processRegister is the input sink to pass form data to my Auth API Service Provider.

    _nagivateToLogin & _showSnakBar are simple functions

    _navigateToLogin() {
          Navigator.of(context).pop();
    }
    
    _showSnakBar(String msg) {
         Scaffold.of(context).showSnackBar(
          SnackBar(
            content: Text(msg),
          ),
         );
     }
    

提交回复
热议问题