Fakultas Ilmu Komputer UI

money_donation_page.dart 12.1 KB
Newer Older
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
1
2
3
import 'dart:io';

import 'package:flutter/material.dart';
4
import 'package:flutter/services.dart';
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
5
import 'package:flutter_bloc/flutter_bloc.dart';
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
6
7
8
9
10
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/custom_input_form.dart';
import 'package:home_industry/Component/product_image.dart';
11
import 'package:home_industry/Pages/Bank/rekening.dart';
12
import 'package:home_industry/Pages/Donation/bloc_money_donation/bloc.dart';
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
13
import 'package:home_industry/Pages/Donation/repositories/donation_repository.dart';
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
14
import 'package:home_industry/Pages/DonationHistory/model/donation.dart';
15
import 'package:home_industry/Pages/ProductPayment/product_payment.dart';
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
16
import 'package:home_industry/Pages/Program/model/program.dart';
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
17
import 'package:home_industry/State/Auth/repositories/depedencies_repositories.dart';
18
import 'package:home_industry/State/ChangeBottomNavigatonBar/bloc.dart';
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
19
import 'package:home_industry/typedef/donasi.dart';
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
20
21
22
import 'package:image_picker/image_picker.dart';
import 'package:provider/provider.dart';

Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
23
class Donasi extends StatefulWidget {
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
24
25
26
27
  final Program program;

  const Donasi({Key key, @required this.program}) : super(key: key);

Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
28
29
30
31
32
  @override
  _DonationState createState() => _DonationState();
}

class _DonationState extends State<Donasi> {
33
  MoneyDonationBloc donationBloc;
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
34
35
36

  @override
  void initState() {
37
    donationBloc = MoneyDonationBloc(
38
39
40
        DonationRepository(
            RepositoryProvider.of<DependenciesRepositories>(context).dio),
        BlocProvider.of<ChangeBottomNavigationBarBloc>(context));
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
41
42
43
    super.initState();
  }

44
45
46
47
48
49
  void onTap(
      {@required String amount,
      @required File image,
      @required String bankName,
      @required String accountName,
      @required String bankIdDestination}) {
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
50
    donationBloc.add(ButtonDonationClicked(
51
52
53
54
55
56
        program: widget.program.id,
        amount: amount,
        image: image,
        bankName: bankName,
        accountName: accountName,
        bankIdDestination: bankIdDestination));
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
57
58
59
60
61
62
63
64
  }

  @override
  void dispose() {
    donationBloc.close();
    super.dispose();
  }

Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
65
66
67
68
69
70
71
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('Donasi'),
          centerTitle: true,
        ),
72
        body: BlocListener<MoneyDonationBloc, MoneyDonationState>(
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
73
74
75
76
77
            bloc: donationBloc,
            listener: (_, state) {
              if (state is UploadProofLoading) {
                EasyLoading.show(status: 'Bukti sedang diunggah');
              } else if (state is UploadProofError) {
78
                EasyLoading.showError('Gagal unggah bukti donasi');
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
79
              } else if (state is UploadProofSuccess) {
80
81
                EasyLoading.showSuccess(
                    'Terima kasih!\nDonasi anda sedang dalam pengecekan');
82
                Navigator.of(context).popUntil(ModalRoute.withName('/'));
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
83
84
85
86
87
88
89
              }
            },
            child: ListView(
              padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 8),
              children: <Widget>[
                SizedBox(
                    height: MediaQuery.of(context).size.height / 4,
90
                    child: CachedImage(url: widget.program.posterImage)),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
91
92
93
94
95
96
                DonasiBody(
                  tap: onTap,
                  program: widget.program,
                ),
              ],
            )));
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  }
}

class DonasiBody extends StatelessWidget {
  final Program program;
  final Donation donation;
  final OnTap tap;
  const DonasiBody({
    Key key,
    this.program,
    this.donation,
    @required this.tap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
113
114
115
116
117
118
119
    return MultiProvider(
      providers: [
        Provider<Donation>.value(value: donation),
        ChangeNotifierProvider<SelectedBank>(
          create: (_) => SelectedBank(),
        )
      ],
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
120
121
122
123
124
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text(
            program?.name ?? donation?.programName,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
125
            style: Theme.of(context).textTheme.headline6,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
126
          ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
127
          const Divider(
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
128
            thickness: 2,
Aditya Pratama's avatar
Aditya Pratama committed
129
            height: 20,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
130
          ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
131
          const Text(
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
132
133
134
            'Terima kasih! Silahkan transfer ke:',
            style: TextStyle(fontSize: 15, fontWeight: FontWeight.w500),
          ),
135
136
137
          Rekening(
            previousBankDestinationId: donation?.bankAccountTransferDestination,
          ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
138
          const Divider(
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
139
            thickness: 2.5,
Aditya Pratama's avatar
Aditya Pratama committed
140
            height: 20,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
          ),
          _FormView(
            tap: tap,
          ),
        ],
      ),
    );
  }
}

class _FormView extends StatefulWidget {
  final OnTap tap;
  const _FormView({Key key, @required this.tap}) : super(key: key);
  @override
  _FormViewState createState() => _FormViewState();
}

class _FormViewState extends State<_FormView> {
  final _formKey = GlobalKey<FormState>();
  String nominal;
  String senderName;
162
  String senderBank;
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
163
  File _image;
Aditya Pratama's avatar
Aditya Pratama committed
164
  FocusScopeNode _focusScopeNode;
165
  final _picker = ImagePicker();
Aditya Pratama's avatar
Aditya Pratama committed
166
167
168
169
170
171
172
173
174
175
176
177

  @override
  void initState() {
    _focusScopeNode = FocusScopeNode();
    super.initState();
  }

  @override
  void dispose() {
    _focusScopeNode.dispose();
    super.dispose();
  }
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
178
179
180
181
182
183
184
185
186

  void setAccountNominal(String nominal) {
    setState(() {
      this.nominal = nominal;
    });
  }

  void setSenderName(String name) {
    setState(() {
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
187
      senderName = name;
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
188
189
190
    });
  }

191
192
193
194
195
196
  void setSenderBank(String bank) {
    setState(() {
      senderBank = bank;
    });
  }

Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
197
  Future<void> _getImageFromCamera() async {
198
199
200
201
202
203
204
205
206
    final img = await _picker.getImage(source: ImageSource.camera);
    _setImage(img);
  }

  void _setImage(PickedFile img) {
    if (img == null) {
      return;
    }
    final image = File(img.path);
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
207
208
209
210
211
212
    setState(() {
      _image = image ?? _image;
    });
  }

  Future<void> _getImageFromGallery() async {
213
214
    final img = await _picker.getImage(source: ImageSource.gallery);
    _setImage(img);
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
215
216
217
  }

  void _showChoosePicture() {
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
218
    FocusScope.of(context).unfocus();
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
219
220
221
222
223
224
    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
225
          title: const Text('Choose Image From'),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
226
227
228
229
230
231
          children: <Widget>[
            BorderBlueButton(
                onPressed: () async {
                  await _getImageFromCamera();
                  Navigator.pop(context);
                },
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
232
                label: 'Camera'),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
233
234
235
236
237
            BorderBlueButton(
              onPressed: () async {
                await _getImageFromGallery();
                Navigator.pop(context);
              },
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
238
              label: 'Gallery',
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
239
240
241
242
243
244
            )
          ],
        ));
  }

  void _buttonSendClicked() {
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
245
    FocusScope.of(context).unfocus();
246
247
248
249
    final selectedBank = context.read<SelectedBank>().selectedBank;
    if (_formKey.currentState.validate() &&
        _image != null &&
        selectedBank != null) {
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
250
      _formKey.currentState.save();
251
252
253
254
255
256
      widget.tap(
          bankName: senderBank,
          accountName: senderName,
          amount: nominal,
          image: _image,
          bankIdDestination: selectedBank.id);
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
257
258
    }
    if (_image == null) {
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
259
      EasyLoading.showError('Bukti foto tidak ditemukan');
260
261
    } else if (selectedBank == null) {
      EasyLoading.showError('Anda belum memilih bank tujuan');
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
262
263
264
265
    }
  }

  Future<void> _retrieveLostData() async {
266
    final response = await _picker.getLostData();
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
267
268
269
270
271
    if (response.isEmpty) {
      return;
    }
    if (response.file != null && response.type == RetrieveType.image) {
      setState(() {
272
        _image = File(response.file.path);
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
273
274
275
276
277
278
279
280
281
282
283
284
      });
    }
  }

  Widget _dynamicImage() {
    if (_image != null) {
      return Container(
          height: 120,
          width: 120,
          decoration:
              BoxDecoration(image: DecorationImage(image: FileImage(_image))));
    }
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
285
    return const Text('Belum memilih foto');
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
286
287
288
289
290
291
292
293
294
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: <Widget>[
        _FormDonasi(
Aditya Pratama's avatar
Aditya Pratama committed
295
          focusScopeNode: _focusScopeNode,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
296
297
298
          formKey: _formKey,
          setNominal: setAccountNominal,
          setName: setSenderName,
299
          setBankName: setSenderBank,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
300
        ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
301
        Platform.isAndroid && _image == null
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
            ? FutureBuilder<void>(
                future: _retrieveLostData(),
                builder: (BuildContext context, AsyncSnapshot<void> snapshot) {
                  if (snapshot.hasError) {
                    return Text(
                      'Pick image/video error: ${snapshot.error}}',
                      textAlign: TextAlign.center,
                    );
                  } else if (snapshot.connectionState == ConnectionState.done) {
                    return _dynamicImage();
                  }
                  return const Text(
                    'Belum memilih foto',
                    textAlign: TextAlign.center,
                  );
                },
              )
            : _dynamicImage(),
        OutlineButton.icon(
            onPressed: _showChoosePicture,
            borderSide:
                BorderSide(color: Theme.of(context).primaryColor, width: 2),
            shape:
                RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
326
            icon: const Icon(Icons.file_upload),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
327
328
            label: const Text('Upload bukti transfer')),
        const Divider(
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
329
330
331
332
333
334
335
336
          thickness: 0,
          height: 10,
        ),
        SizedBox(
            width: double.infinity,
            height: 42,
            child: BlueButton(
                onPressed: _buttonSendClicked,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
337
338
                label: context.watch<Donation>() != null
                    ? 'Unggah ulang'
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
339
                    : 'Donasi Sekarang')),
340
341
342
        const Divider(
          height: 10,
        ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
343
344
345
346
347
348
349
350
351
      ],
    );
  }
}

class _FormDonasi extends StatelessWidget {
  final Key formKey;
  final Function(String) setNominal;
  final Function(String) setName;
352
  final Function(String) setBankName;
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
353

Aditya Pratama's avatar
Aditya Pratama committed
354
355
356
357
358
  void _handleSubmitted(_) {
    focusScopeNode.nextFocus();
  }

  final FocusScopeNode focusScopeNode;
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
359
360
361
362
363
  const _FormDonasi(
      {Key key,
      @required this.formKey,
      @required this.setNominal,
      @required this.setName,
364
      @required this.setBankName,
Aditya Pratama's avatar
Aditya Pratama committed
365
      @required this.focusScopeNode})
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
366
367
368
369
370
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
371
        height: 230,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
372
373
        child: Form(
            key: formKey,
Aditya Pratama's avatar
Aditya Pratama committed
374
375
376
377
378
            child: FocusScope(
              node: focusScopeNode,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: <Widget>[
379
380
381
382
383
384
385
386
                  CustomTextInputForm(
                    initialValue: context.watch<Donation>()?.userBankName,
                    label: 'Nama Bank',
                    prefixIcon: Icons.payment,
                    onSave: setBankName,
                    onSubmit: _handleSubmitted,
                    textInputAction: TextInputAction.next,
                  ),
Aditya Pratama's avatar
Aditya Pratama committed
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
                  CustomTextInputForm(
                    initialValue:
                        context.watch<Donation>()?.userBankAccountName,
                    label: 'Nama Pengirim',
                    prefixIcon: Icons.person_outline,
                    onSave: setName,
                    onSubmit: _handleSubmitted,
                    textInputAction: TextInputAction.next,
                  ),
                  CustomTextInputForm(
                    initialValue: context
                        .watch<Donation>()
                        ?.amount
                        ?.split('.')
                        ?.elementAt(0),
                    label: 'Nominal',
                    prefixIcon: Icons.attach_money,
                    onSave: setNominal,
                    isNumberOnly: true,
                  ),
                ],
              ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
409
410
411
            )));
  }
}