View difference between Paste ID: qcNMjA5H and dQDgFwSJ
SHOW: | | - or go back to the newest paste.
1
import 'dart:developer';
2
import 'dart:io';
3
import 'package:flutter/foundation.dart';
4
import 'package:flutter/material.dart';
5
import 'package:provider/provider.dart';
6
7
import 'package:qr_code_scanner/qr_code_scanner.dart';
8
import 'dasboard.dart';
9
import 'model/undangan.dart';
10
import 'package:qrcode2/apiTamu.dart';
11
12
class QRViewExample extends StatefulWidget {
13
  const QRViewExample({Key? key}) : super(key: key);
14
15
  @override
16
  State<StatefulWidget> createState() => _QRViewExampleState();
17
}
18
19
class _QRViewExampleState extends State<QRViewExample> {
20
  Barcode? result;
21
  QRViewController? controller;
22
  final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
23
24
  @override
25
  void reassemble() {
26
    super.reassemble();
27
    if (Platform.isAndroid) {
28
      controller?.pauseCamera();
29
    }
30
    controller?.resumeCamera();
31
  }
32
33
  @override
34
  Widget build(BuildContext context) {
35
    return Scaffold(
36
      body: Column(
37
        children: <Widget>[
38
          if (result == null)
39-
            FutureBuilder<Undangan?>(
39+
40-
              future: ApiUndangan.cekUndangan(result!.code!),
40+
41-
              builder:
41+
            const CircularProgressIndicator(),
42-
                  (BuildContext context, AsyncSnapshot<Undangan?> snapshot) {
42+
43-
                if (snapshot.hasData) {
43+
44-
                  return _profil(snapshot.data!);
44+
45-
                } else if (snapshot.hasError) {
45+
46-
                  print('Error snapshot');
46+
47-
                  return Container(
47+
48-
                    alignment: Alignment.center,
48+
49-
                    margin: const EdgeInsets.only(top: 100),
49+
50-
                    child: const Text(
50+
51-
                      'data tidak di temukan',
51+
52-
                      style: TextStyle(fontSize: 28, color: Colors.red),
52+
53-
                    ),
53+
54-
                  );
54+
55-
                }
55+
56-
                return const CircularProgressIndicator();
56+
57-
              },
57+
58
                      });
59
                    },
60
                    child: const Text('coba lagi'),
61
                  ),
62
                  Row(
63
                    mainAxisAlignment: MainAxisAlignment.center,
64
                    crossAxisAlignment: CrossAxisAlignment.center,
65
                    children: <Widget>[
66
                      Container(
67
                        margin: const EdgeInsets.all(8),
68
                        child: ElevatedButton(
69
                          onPressed: () async {
70
                            await controller?.toggleFlash();
71
                            setState(() {});
72
                          },
73
                          child: FutureBuilder(
74
                            future: controller?.getFlashStatus(),
75
                            builder: (context, snapshot) {
76
                              return Text('Flash: ${snapshot.data}');
77
                            },
78
                          ),
79
                        ),
80
                      ),
81
                      Container(
82
                        margin: const EdgeInsets.all(8),
83
                        child: ElevatedButton(
84
                          onPressed: () async {
85
                            await controller?.flipCamera();
86
                            setState(() {});
87
                          },
88
                          child: FutureBuilder(
89
                            future: controller?.getCameraInfo(),
90
                            builder: (context, snapshot) {
91
                              if (snapshot.data != null) {
92
                                return Text(
93
                                    'Camera facing ${describeEnum(snapshot.data!)}');
94
                              } else {
95
                                return const Text('loading');
96
                              }
97
                            },
98
                          ),
99
                        ),
100
                      ),
101
                    ],
102
                  ),
103
                  Row(
104
                    mainAxisAlignment: MainAxisAlignment.center,
105
                    crossAxisAlignment: CrossAxisAlignment.center,
106
                    children: <Widget>[
107
                      Container(
108
                        margin: const EdgeInsets.all(8),
109
                        child: ElevatedButton(
110
                          onPressed: () async {
111
                            await controller?.pauseCamera();
112
                          },
113
                          child: const Text(
114
                            'pause',
115
                            style: TextStyle(fontSize: 20),
116
                          ),
117
                        ),
118
                      ),
119
                      Container(
120
                        margin: const EdgeInsets.all(8),
121
                        child: ElevatedButton(
122
                          onPressed: () async {
123
                            await controller?.resumeCamera();
124
                          },
125
                          child: const Text('resume',
126
                              style: TextStyle(fontSize: 20)),
127
                        ),
128
                      ),
129
                    ],
130
                  ),
131
                ],
132
              ),
133
            ),
134
          )
135
        ],
136
      ),
137
    );
138
  }
139
140
  Widget _buildQrView(BuildContext context) {
141
    // For this example we check how width or tall the device is and change the scanArea and overlay accordingly.
142
    var scanArea = (MediaQuery.of(context).size.width < 400 ||
143
            MediaQuery.of(context).size.height < 400)
144
        ? 400.0
145
        : 400.0;
146
    // To ensure the Scanner view is properly sizes after rotation
147
    // we need to listen for Flutter SizeChanged notification and update controller
148
    return QRView(
149
      key: qrKey,
150
      onQRViewCreated: _onQRViewCreated,
151
      overlay: QrScannerOverlayShape(
152
        borderColor: Colors.red,
153
        borderRadius: 10,
154
        borderLength: 30,
155
        borderWidth: 10,
156
        cutOutSize: scanArea,
157
      ),
158
      onPermissionSet: (ctrl, p) => _onPermissionSet(context, ctrl, p),
159
    );
160
  }
161
162
  void _onQRViewCreated(QRViewController controller) {
163
    setState(() {
164
      this.controller = controller;
165
    });
166
    controller.scannedDataStream.listen((scanData) {
167
      setState(() {
168
        result = scanData;
169
      });
170
      _cekUndangan(scanData.code!);
171
    });
172
  }
173
174
  _cekUndangan(String code) async {
175
    final undangan = await ApiUndangan.cekUndangan(code);
176
177
    if (undangan != null) {
178
      final undanganProvider = Provider.of<UndanganProvider>(context);
179-
  Widget _profil(Undangan undangan) {
179+
      undanganProvider.undangan = undangan;
180-
    return Container(
180+
181-
      padding: const EdgeInsets.fromLTRB(5, 5, 5, 5),
181+
      Navigator.push(context, MaterialPageRoute(builder: (_) => Dasboard()));
182-
      margin: const EdgeInsets.fromLTRB(10, 50, 10, 50),
182+
    } else {
183-
      color: Colors.blue.withOpacity(9),
183+
184-
      child: Column(
184+
        result = null;
185-
        children: [
185+
186-
          Text(
186+
187-
            undangan.id,
187+
188-
            style: const TextStyle(
188+
189-
                fontWeight: FontWeight.bold, fontSize: 22, color: Colors.white),
189+
190-
          ),
190+
191-
          const SizedBox(height: 20),
191+
192-
          Text('Terimakasih sudah hadir di ${undangan.title}'),
192+
193
        const SnackBar(content: Text('no Permission')),
194
      );
195
    }
196
  }
197
198
  @override
199
  void dispose() {
200
    controller?.dispose();
201
    super.dispose();
202
  }
203
}