Fakultas Ilmu Komputer UI

edit_profile.dart 13.8 KB
Newer Older
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:home_industry/Component/blue_button.dart';
import 'package:home_industry/Component/border_blue_button.dart';
import 'package:home_industry/Component/circular_image.dart';
import 'package:home_industry/Component/custom_input_form.dart';
import 'package:home_industry/Pages/Profile/bloc/profile_bloc.dart';
import 'package:home_industry/Pages/Profile/bloc/profile_event.dart';
import 'package:home_industry/Pages/Profile/bloc/profile_state.dart';
import 'package:home_industry/Pages/Profile/model/profile.dart';
import 'package:image_picker/image_picker.dart';
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
15
import 'package:outline_material_icons/outline_material_icons.dart';
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
16
17
18
19
20
21
22
23
24

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text(
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
25
            'Edit Akun',
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
26
27
28
          ),
          centerTitle: true,
        ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
        body: ListView(
          padding: const EdgeInsets.symmetric(horizontal: 20),
          physics: const BouncingScrollPhysics(),
          children: <Widget>[
            BlocConsumer<ProfileBloc, ProfileState>(
              buildWhen: (previous, current) {
                return current is! ProfileLoading;
              },
              builder: (context, state) {
                if (state is ProfileLoaded) {
                  return EditForm(
                    profile: state.profile,
                  );
                } else {
                  return const Center(
                    child: Text('Mohon periksa koneksi anda'),
                  );
                }
              },
              listener: (context, state) {
                if (state is ProfileLoading) {
                  EasyLoading.show(status: 'Mohon tunggu...');
                } else if (state is ProfileError) {
                  EasyLoading.showError(state.error);
                } else if (state is ProfileLoaded) {
                  Navigator.of(context).pop();
                  EasyLoading.showSuccess('Data akun terupdate');
                }
              },
            )
          ],
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
        ));
  }
}

class EditForm extends StatefulWidget {
  final Profile profile;
  const EditForm({Key key, @required this.profile}) : super(key: key);

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

class _EditFormState extends State<EditForm> {
  final _formKey = GlobalKey<FormState>();
  final _formPassword = GlobalKey<FormState>();
75
  final _picker = ImagePicker();
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
76

Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
77
  FocusScopeNode _focusScopeNode;
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
78
79
80
81
82
83
84
85
86
87
88
  File _image;
  String _nama;
  String _nomorhp;
  String _alamat;
  String _rt;
  String _rw;
  String _kelurahan;
  String _kecamatan;
  String _username;
  String _password;

Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
89
90
91
92
93
94
95
96
97
98
99
100
  @override
  void initState() {
    _focusScopeNode = FocusScopeNode();
    super.initState();
  }

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

Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
101
  Future<void> _getImageFromCamera() async {
102
103
    final image = await _picker.getImage(source: ImageSource.camera);
    await _sentPicture(image);
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
104
105
  }

106
  Future<void> _sentPicture(PickedFile image) async {
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    File imgFile;
    if (Platform.isAndroid && image == null) {
      final response = await _picker.getLostData();
      if (response.isEmpty) {
        return;
      }
      imgFile = File(response.file.path);
    } else if (image == null) {
      return;
    }
    imgFile = File(image.path);
    setState(() {
      _image = imgFile;
    });
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
121
122
123
124
125
126
127
    if (_image != null) {
      BlocProvider.of<ProfileBloc>(context).add(UpdateProfilePicture(_image));
      Navigator.of(context).pop();
    }
  }

  Future<void> _getImageFromGallery() async {
128
129
    final image = await _picker.getImage(source: ImageSource.gallery);
    await _sentPicture(image);
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  }

  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;
    });
  }

  void _showDialogPassword() {
    showDialog(
        context: context,
        child: SimpleDialog(
          contentPadding:
              const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5)),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
193
          title: const Text('Ganti Password'),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
194
195
196
197
198
199
200
201
202
203
204
205
206
          children: <Widget>[
            Form(
              key: _formPassword,
              child: CustomTextInputForm(
                prefixIcon: Icons.vpn_key,
                key: const Key('password'),
                label: 'Password',
                onSave: _updatePassword,
                maxLength: 30,
                isPassword: true,
              ),
            ),
            FlatButton(
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
207
              key: const Key('passwordConfirmationButton'),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
208
209
210
211
212
213
214
              onPressed: () {
                if (_formPassword.currentState.validate()) {
                  _formPassword.currentState.save();
                  BlocProvider.of<ProfileBloc>(context)
                      .add(UpdatePassword(_password));
                }
              },
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
215
              child: const Text('Konfirmasi'),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(10)),
              color: Colors.redAccent,
              textColor: Colors.white,
            )
          ],
        ));
  }

  void _showChoosePicture() {
    showDialog(
        context: context,
        child: SimpleDialog(
          contentPadding:
              const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5)),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
232
          title: const Text('Choose Image From'),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
233
234
235
236
237
          children: <Widget>[
            BorderBlueButton(
                onPressed: () async {
                  await _getImageFromCamera();
                },
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
238
                label: 'Camera'),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
239
240
241
242
            BorderBlueButton(
                onPressed: () async {
                  await _getImageFromGallery();
                },
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
243
                label: 'Gallery')
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
          ],
        ));
  }

  Widget _dynamicImage() {
    if (_image != null) {
      return Container(
          height: 120,
          width: 120,
          decoration: BoxDecoration(
              shape: BoxShape.circle,
              color: Colors.grey[200],
              image: DecorationImage(image: FileImage(_image))));
    }
    return CircularImage(url: widget.profile.profilePicture);
  }

  String filteredRtRw(String rtrw) {
    if (rtrw.length == 1) {
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
263
      return '00$rtrw';
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
264
    } else if (rtrw.length == 2) {
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
265
      return '0$rtrw';
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
266
267
268
269
270
    }
    return rtrw;
  }

  void _sentEvent() {
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
271
272
273
    final noHp = '+62$_nomorhp';
    final model = Profile(widget.profile.id, _username, _nama, noHp, _alamat,
        filteredRtRw(_rt), filteredRtRw(_rw), _kelurahan, _kecamatan, null);
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
274
275
276
277
    BlocProvider.of<ProfileBloc>(context)
        .add(UpdateProfilePressed(profile: model));
  }

Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
278
279
280
281
  void _handleSubmitted(_) {
    _focusScopeNode.nextFocus();
  }

Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
282
283
284
285
  @override
  Widget build(BuildContext context) {
    return Form(
        key: _formKey,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
286
        child: Padding(
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
287
          padding: const EdgeInsets.symmetric(vertical: 10),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
288
289
290
291
          child: FocusScope(
            node: _focusScopeNode,
            child: Column(
              mainAxisSize: MainAxisSize.min,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
292
293
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
294
                GestureDetector(
295
296
297
298
                    onTap: _showChoosePicture,
                    child: Stack(
                      children: <Widget>[
                        _dynamicImage(),
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
299
                        const Positioned.fill(
300
301
302
303
304
305
                          child: Align(
                              alignment: Alignment.bottomRight,
                              child: Icon(Icons.edit)),
                        )
                      ],
                    )),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
306
                const Divider(
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
307
                  height: 10,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
308
                ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
309
310
311
312
313
314
315
316
317
                CustomTextInputForm(
                  prefixIcon: Icons.person,
                  key: const Key('nama'),
                  label: 'Nama Lengkap',
                  onSave: _updateNama,
                  initialValue: widget.profile.fullName,
                  maxLength: 200,
                  onSubmit: _handleSubmitted,
                  textInputAction: TextInputAction.next,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
318
                ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
                CustomTextInputForm(
                  prefixIcon: Icons.phone,
                  key: const Key('nomorhp'),
                  label: 'Nomor HP',
                  prefixText: '+62',
                  isNumberOnly: true,
                  onSave: _updateNomorhp,
                  maxLength: 20,
                  initialValue: widget.profile.phoneNumber.substring(3),
                  onSubmit: _handleSubmitted,
                  textInputAction: TextInputAction.next,
                ),
                CustomTextInputForm(
                  key: const Key('alamat'),
                  prefixIcon: Icons.location_on,
                  label: 'Alamat',
                  onSave: _updateAlamat,
                  maxLength: 200,
                  initialValue: widget.profile.address,
                  onSubmit: _handleSubmitted,
                  textInputAction: TextInputAction.next,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Expanded(
                      flex: 20,
                      child: CustomTextInputForm(
                        isNumberOnly: true,
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
348
                        prefixIcon: OMIcons.nearMe,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
349
350
351
352
353
354
355
356
357
358
359
360
361
362
                        key: const Key('rt'),
                        label: 'RT',
                        onSave: _updateRt,
                        maxLength: 3,
                        initialValue: widget.profile.neighborhood,
                        onSubmit: _handleSubmitted,
                        textInputAction: TextInputAction.next,
                      ),
                    ),
                    const Spacer(),
                    Expanded(
                      flex: 20,
                      child: CustomTextInputForm(
                        isNumberOnly: true,
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
363
                        prefixIcon: OMIcons.nearMe,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
364
365
366
367
368
369
370
371
372
373
                        key: const Key('rw'),
                        label: 'RW',
                        onSave: _updateRw,
                        maxLength: 3,
                        initialValue: widget.profile.hamlet,
                        onSubmit: _handleSubmitted,
                        textInputAction: TextInputAction.next,
                      ),
                    ),
                  ],
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
374
                ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
375
376
377
378
379
380
381
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Expanded(
                      flex: 20,
                      child: CustomTextInputForm(
                        key: const Key('kelurahan'),
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
382
                        prefixIcon: Icons.near_me,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
383
384
385
386
387
388
389
390
391
392
393
394
395
396
                        label: 'Kelurahan',
                        onSave: _updateKelurahan,
                        maxLength: 100,
                        initialValue: widget.profile.urbanVillage,
                        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
397
                        prefixIcon: Icons.near_me,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
                        onSave: _updateKecamatan,
                        maxLength: 100,
                        initialValue: widget.profile.subDistrict,
                        onSubmit: _handleSubmitted,
                        textInputAction: TextInputAction.next,
                      ),
                    ),
                  ],
                ),
                CustomTextInputForm(
                  key: const Key('username'),
                  label: 'Username',
                  prefixIcon: Icons.person_pin,
                  onSave: _updateUsername,
                  maxLength: 150,
                  initialValue: widget.profile.username,
                ),
                Container(
                  height: 40,
                  child: SizedBox.expand(
                    child: BorderBlueButton(
                      onPressed: _showDialogPassword,
                      label: 'Ganti Password',
                    ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
422
423
                  ),
                ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
424
425
                const Divider(
                  height: 15,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
426
                ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
427
428
429
430
431
432
433
434
435
436
437
438
439
                Container(
                  height: 40,
                  child: SizedBox.expand(
                    child: BlueButton(
                      onPressed: () {
                        if (_formKey.currentState.validate()) {
                          _formKey.currentState.save();
                          _sentEvent();
                        }
                      },
                      label: 'Konfirmasi',
                    ),
                  ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
440
                ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
441
              ],
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
442
            ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
443
          ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
444
445
446
        ));
  }
}