Blame view

lib/login/loginpage/bloc/login_bloc.dart 3.66 KB
062f0df2   liangchengyou   feat:登录模块代码提交
1
  import 'package:flutter/cupertino.dart';
2a29701f   liangchengyou   feat:提交代码
2
  import 'package:flutter_bloc/flutter_bloc.dart';
23384a42   liangchengyou   feat:登陆请求
3
4
  import 'package:wow_english/network/api.dart';
  import 'package:wow_english/network/network_manager.dart';
2a29701f   liangchengyou   feat:提交代码
5
6
7
8
  
  part 'login_event.dart';
  part 'login_state.dart';
  
062f0df2   liangchengyou   feat:登录模块代码提交
9
10
11
12
13
14
15
  enum LoginType {
    ///密码登陆
    pwd,
    ///验证码登陆
    sms,
  }
  
2a29701f   liangchengyou   feat:提交代码
16
17
  class LoginBloc extends Bloc<LoginEvent, LoginState> {
  
062f0df2   liangchengyou   feat:登录模块代码提交
18
19
20
    bool _canLogin = false;
    ///是否可以发送验证码
    bool _canSendSms = false;
062f0df2   liangchengyou   feat:登录模块代码提交
21
22
23
24
25
    ///是否阅读协议
    bool _agreement = false;
    ///登陆方式
    LoginType _loginType = LoginType.sms;
  
062f0df2   liangchengyou   feat:登录模块代码提交
26
27
    final TextEditingController phoneNumController = TextEditingController();
    final TextEditingController checkNumController = TextEditingController();
2a29701f   liangchengyou   feat:提交代码
28
  
062f0df2   liangchengyou   feat:登录模块代码提交
29
30
31
32
    bool get canLogin => _canLogin;
    bool get agreement => _agreement;
    LoginType get loginType => _loginType;
    bool get canSendSms => _canSendSms;
2a29701f   liangchengyou   feat:提交代码
33
34
35
  
    LoginBloc() : super(LoginInitial()) {
      on<RequestLoginEvent>(_requestLoginApi);
062f0df2   liangchengyou   feat:登录模块代码提交
36
37
38
      on<ChangeLoginTypeEvent>(_changeLoginType);
      on<PhoneNumChangeEvent>(_changePhoneNumber);
      on<AgreementChangeEvent>(_agreementTypeChange);
062f0df2   liangchengyou   feat:登录模块代码提交
39
      on<CheckFieldChangeEvent>(_checkFiledChange);
7912b3f7   liangchengyou   feat:更新代码
40
      on<RequestSmsCodeEvent>(_requestSmsCodeApi);
2a29701f   liangchengyou   feat:提交代码
41
42
43
    }
  
  
062f0df2   liangchengyou   feat:登录模块代码提交
44
    ///请求登陆
2a29701f   liangchengyou   feat:提交代码
45
    void _requestLoginApi(RequestLoginEvent event, Emitter<LoginState> emitter) async {
ea2c8205   liangchengyou   feat:更新代码
46
      await DioUtil().requestData(
23384a42   liangchengyou   feat:登陆请求
47
          Api.login,
ea2c8205   liangchengyou   feat:更新代码
48
          method: HttpMethod.post,
23384a42   liangchengyou   feat:登陆请求
49
50
51
52
53
          data: {
            'phoneNum':'17730280759',
            'type':_loginType.toString(),
            'password':'asd123456'},
          successCallBack: (data){
ea2c8205   liangchengyou   feat:更新代码
54
            emitter(LoginResultChangeState(true));
23384a42   liangchengyou   feat:登陆请求
55
56
          },
          errorCallBack: (error){
ea2c8205   liangchengyou   feat:更新代码
57
            emitter(LoginResultChangeState(false));
23384a42   liangchengyou   feat:登陆请求
58
          });
062f0df2   liangchengyou   feat:登录模块代码提交
59
60
    }
  
7912b3f7   liangchengyou   feat:更新代码
61
62
63
64
65
    ///请求验证码
    void _requestSmsCodeApi(RequestSmsCodeEvent event, Emitter<LoginState> emitter) async {
  
    }
  
062f0df2   liangchengyou   feat:登录模块代码提交
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    ///切换登陆方式
    void _changeLoginType(ChangeLoginTypeEvent event, Emitter<LoginState> emitter) async {
      if (_loginType == LoginType.sms) {
        _loginType = LoginType.pwd;
      } else {
        _loginType = LoginType.sms;
      }
      checkNumController.clear();
      if (_loginStateChange()) {
        emitter(LoginEventChangeState());
      }
      emitter(LoginTypeChangeState());
    }
  
    ///手机号输入
    void _changePhoneNumber(PhoneNumChangeEvent event, Emitter<LoginState> emitter) async {
      if(phoneNumController.text.isNotEmpty) {
        if (!_canSendSms) {
          _canSendSms = true;
          emitter(SmsSendTypeChangeState());
        }
        if (_loginStateChange()) {
          emitter(LoginEventChangeState());
        }
      } else {
        if (_canSendSms) {
          _canSendSms = false;
          emitter(SmsSendTypeChangeState());
        }
        if (_loginStateChange()) {
          emitter(LoginEventChangeState());
        }
      }
    }
  
    ///验证码/密码输入变化
    void _checkFiledChange(CheckFieldChangeEvent event,Emitter<LoginState> emitter) async {
      if (_loginStateChange()) {
        emitter(LoginEventChangeState());
      }
    }
  
    ///是否阅读协议
    void _agreementTypeChange(AgreementChangeEvent event, Emitter<LoginState> emitter) async {
      _agreement = !_agreement;
      emitter(AgreementTypeChangeState());
      if (_loginStateChange()) {
        emitter(LoginEventChangeState());
      }
    }
  
bfb40cd0   liangchengyou   feat:忘记密码获取验证码
117
    ///登陆状态判断
062f0df2   liangchengyou   feat:登录模块代码提交
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    bool _loginStateChange() {
      if (_agreement) {
        if (phoneNumController.text.isNotEmpty && checkNumController.text.isNotEmpty) {
          if (!_canLogin) {
            _canLogin = true;
            return true;
          }
        } else {
          if (_canLogin) {
            _canLogin = false;
            return true;
          }
        }
      } else {
        if (_canLogin) {
          _canLogin = false;
          return true;
        }
      }
      return false;
2a29701f   liangchengyou   feat:提交代码
138
139
    }
  }