【问题标题】:Show dialog using Scoped model使用 Scoped 模型显示对话框
【发布时间】:2019-07-25 18:06:08
【问题描述】:

我有一个基本的登录表单,我的LoginModel。 但我不明白如何调用函数notifyListeners 在我的视图中显示一个对话框。

登录小部件:

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        body: new ScopedModel<LoginModel>(
            model: _loginModel,
            child: Center(child: ScopedModelDescendant<LoginModel>(
                builder: (context, child, model) {
              if (model.status == Status.LOADING) {
                return Loading();
              }
              else return showForm(context);
            }))));
  }

以及登录模型:

class LoginModel extends Model {

  Status _status = Status.READY;
  Status get status => _status;

  void onLogin(String username, String password) async {
    _status = Status.LOADING;
    notifyListeners();

    try {
      await api.login();
      _status = Status.SUCCESS;
      notifyListeners();

    } catch (response) {
      _status = Status.ERROR;
      notifyListeners();
    }
  }

statusError 时,我需要显示一个对话框

【问题讨论】:

    标签: flutter scoped-model


    【解决方案1】:

    终于明白了,只是在方法onLogin中返回了一个Future

    Future<bool> onLogin(String username, String password) async {
      _status = Status.LOADING;
      notifyListeners();
    
      try {
        await api.login();
        _status = Status.SUCCESS;
        notifyListeners();
        return true;
    
      } catch (response) {
        _status = Status.ERROR;
        notifyListeners();
        return false;
      }
    }
    

    在小部件中:

    onPressed: () async {
      bool success = await _loginModel.onLogin(_usernameController.text, _passwordController.text);
      if(success) {
        Navigator.pop(context, true);
      }
      else{
        _showDialogError();
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多