Fakultas Ilmu Komputer UI

register.dart 9.85 KB
Newer Older
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
1
import 'package:flutter/material.dart';
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
2
import 'package:flutter/painting.dart';
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
3
4
5
6
7
8
9
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:home_industry/Component/custom_input_form.dart';
import 'package:home_industry/Pages/Register/bloc/bloc.dart';
import 'package:home_industry/Pages/Register/model/register_model.dart';
import 'package:home_industry/Pages/Register/repositories/register_repository.dart';
import 'package:home_industry/State/Auth/repositories/depedencies_repositories.dart';
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
10
import 'package:outline_material_icons/outline_material_icons.dart';
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
11
12
13
14
15
16
17
18
19
20

class Register extends StatelessWidget {
  const Register({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          centerTitle: true,
21
          title: const Text('Industri Pilar'),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
22
23
          elevation: 0,
        ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
24
25
26
27
28
29
30
31
32
33
34
        body: ListView(
            physics: const BouncingScrollPhysics(),
            padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
            children: const <Widget>[
              Center(
                child: Padding(
                  padding: EdgeInsets.only(bottom: 20),
                  child: Text(
                    'Registrasi',
                    style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
                  ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
35
                ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
36
37
38
              ),
              _RegisterView(),
            ]));
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  }
}

class _RegisterView extends StatelessWidget {
  const _RegisterView({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) {
        return RegisterBloc(
            registerRepository: RegisterRepository(
                dio: RepositoryProvider.of<DependenciesRepositories>(context)
                    .dio));
      },
      child: const RegisterForm(),
    );
  }
}

class RegisterForm extends StatefulWidget {
  const RegisterForm({Key key}) : super(key: key);

  @override
  _RegisterFormState createState() => _RegisterFormState();
}

class _RegisterFormState extends State<RegisterForm> {
  final _formKey = GlobalKey<FormState>();
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
68
  FocusScopeNode _focusScopeNode;
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

  String _nama;
  String _nomorhp;
  String _alamat;
  String _rt;
  String _rw;
  String _kelurahan;
  String _kecamatan;
  String _username;
  String _password;

  void _updateNama(String nama) {
    setState(() {
      _nama = nama;
    });
  }

  void _updateNomorhp(String nomorhp) {
    setState(() {
      _nomorhp = nomorhp;
    });
  }

  void _updateAlamat(String alamat) {
    setState(() {
      _alamat = alamat;
    });
  }

  void _updateRt(String rt) {
    setState(() {
      _rt = rt;
    });
  }

  void _updateRw(String rw) {
    setState(() {
      _rw = rw;
    });
  }

  void _updateKelurahan(String kelurahan) {
    setState(() {
      _kelurahan = kelurahan;
    });
  }

  void _updateKecamatan(String kecamatan) {
    setState(() {
      _kecamatan = kecamatan;
    });
  }

  void _updateUsername(String username) {
    setState(() {
      _username = username;
    });
  }

  void _updatePassword(String password) {
    setState(() {
      _password = password;
    });
  }

  String filteredRtRw(String rtrw) {
    if (rtrw.length == 1) {
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
136
      return '00$rtrw';
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
137
    } else if (rtrw.length == 2) {
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
138
      return '0$rtrw';
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
139
140
141
142
143
    }
    return rtrw;
  }

  void _sentEvent() {
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
144
145
146
    final noHp = '+62$_nomorhp';
    final model = RegisterModel(_username, _password, _nama, noHp, _alamat,
        filteredRtRw(_rt), filteredRtRw(_rw), _kelurahan, _kecamatan);
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
147
148
149
    BlocProvider.of<RegisterBloc>(context).add(ButtonRegisterPressed(model));
  }

Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  @override
  void initState() {
    _focusScopeNode = FocusScopeNode();
    super.initState();
  }

  @override
  void dispose() {
    _focusScopeNode.dispose();
    super.dispose();
  }

  void _handleSubmitted(_) {
    _focusScopeNode.nextFocus();
  }

Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
166
167
168
169
170
  @override
  Widget build(BuildContext context) {
    return BlocListener<RegisterBloc, RegisterState>(
      listener: (context, state) {
        if (state is RegisterSuccess) {
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
171
          EasyLoading.showSuccess('Registrasi berhasil');
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
172
173
174
175
          Navigator.pop(context);
        } else if (state is RegisterFailure) {
          EasyLoading.showError(state.error);
        } else if (state is RegisterLoading) {
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
176
          EasyLoading.show(status: 'Tunggu sebentar...');
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
177
178
        }
      },
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
179
180
181
182
183
184
185
      child: SizedBox(
        height: 580,
        child: Form(
            key: _formKey,
            child: FocusScope(
              node: _focusScopeNode,
              child: Column(
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
186
187
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
188
189
190
191
192
193
194
195
                  CustomTextInputForm(
                    prefixIcon: Icons.person,
                    key: const Key('nama'),
                    label: 'Nama Lengkap',
                    onSave: _updateNama,
                    maxLength: 200,
                    onSubmit: _handleSubmitted,
                    textInputAction: TextInputAction.next,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
196
                  ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
197
198
199
200
201
202
203
204
205
206
                  CustomTextInputForm(
                    prefixIcon: Icons.phone,
                    key: const Key('nomorhp'),
                    label: 'Nomor HP',
                    prefixText: '+62',
                    isNumberOnly: true,
                    onSave: _updateNomorhp,
                    maxLength: 20,
                    onSubmit: _handleSubmitted,
                    textInputAction: TextInputAction.next,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
207
                  ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
                  CustomTextInputForm(
                    key: const Key('alamat'),
                    prefixIcon: Icons.location_on,
                    label: 'Alamat',
                    onSave: _updateAlamat,
                    maxLength: 200,
                    onSubmit: _handleSubmitted,
                    textInputAction: TextInputAction.next,
                  ),
                  Row(
                    children: <Widget>[
                      Expanded(
                        flex: 20,
                        child: CustomTextInputForm(
                          isNumberOnly: true,
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
223
                          prefixIcon: OMIcons.nearMe,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
224
225
226
227
228
229
230
231
232
233
234
235
236
                          key: const Key('rt'),
                          label: 'RT',
                          onSave: _updateRt,
                          maxLength: 3,
                          onSubmit: _handleSubmitted,
                          textInputAction: TextInputAction.next,
                        ),
                      ),
                      const Spacer(),
                      Expanded(
                        flex: 20,
                        child: CustomTextInputForm(
                          isNumberOnly: true,
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
237
                          prefixIcon: OMIcons.nearMe,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
                          key: const Key('rw'),
                          label: 'RW',
                          onSave: _updateRw,
                          maxLength: 3,
                          onSubmit: _handleSubmitted,
                          textInputAction: TextInputAction.next,
                        ),
                      ),
                    ],
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Expanded(
                        flex: 20,
                        child: CustomTextInputForm(
                          key: const Key('kelurahan'),
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
255
                          prefixIcon: Icons.near_me,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
256
257
258
259
260
261
262
263
264
265
266
267
268
                          label: 'Kelurahan',
                          onSave: _updateKelurahan,
                          maxLength: 100,
                          onSubmit: _handleSubmitted,
                          textInputAction: TextInputAction.next,
                        ),
                      ),
                      const Spacer(),
                      Expanded(
                        flex: 20,
                        child: CustomTextInputForm(
                          key: const Key('kecamatan'),
                          label: 'Kecamatan',
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
269
                          prefixIcon: Icons.near_me,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
                          onSave: _updateKecamatan,
                          maxLength: 100,
                          onSubmit: _handleSubmitted,
                          textInputAction: TextInputAction.next,
                        ),
                      ),
                    ],
                  ),
                  CustomTextInputForm(
                    key: const Key('username'),
                    label: 'Username',
                    prefixIcon: Icons.person_pin,
                    onSave: _updateUsername,
                    maxLength: 150,
                    onSubmit: _handleSubmitted,
                    textInputAction: TextInputAction.next,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
286
                  ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
                  CustomTextInputForm(
                    key: const Key('password'),
                    label: 'Password',
                    prefixIcon: Icons.vpn_key,
                    isPassword: true,
                    onSave: _updatePassword,
                    maxLength: 200,
                  ),
                  Container(
                    height: 40,
                    child: SizedBox.expand(
                      child: RaisedButton(
                        onPressed: () {
                          if (_formKey.currentState.validate()) {
                            _formKey.currentState.save();
                            _sentEvent();
                          }
                        },
                        child: const Text('DAFTAR'),
                        color: const Color(0xff4E7890),
                        textColor: Colors.white,
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(10)),
                      ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
311
312
313
314
                    ),
                  ),
                ],
              ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
315
316
            )),
      ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
317
318
319
    );
  }
}