-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathscannerPage.dart
More file actions
116 lines (110 loc) · 4.27 KB
/
scannerPage.dart
File metadata and controls
116 lines (110 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
60
61
62
63
64
65
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
import 'package:barcode_scan/barcode_scan.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:forest_tagger/DataModels/Tree.dart';
import 'package:forest_tagger/components/treeListItem.dart';
import 'package:forest_tagger/main.dart';
class ScannerPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return ScannerPageState();
}
}
class ScannerPageState extends State<ScannerPage> {
List<Tree> listOfScanneedTrees = [];
@override
Widget build(BuildContext context) {
return Material(
child: Container(
child: Center(
child: Padding(
padding: const EdgeInsets.only(top: 40),
child: ListView(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: FlatButton(
padding: EdgeInsets.all(15.0),
onPressed: () async {
try {
String res = await BarcodeScanner.scan();
var arr = res.split(", ");
Tree t = Tree(arr[0], (arr[1]), (arr[2]));
TreeProvider().insert(t);
List<Tree> newList = [];
listOfScanneedTrees.forEach((element) {
newList.add(element);
});
newList.add(t);
setState(() {
listOfScanneedTrees = newList;
});
} on PlatformException catch (e) {
if (e.code == BarcodeScanner.CameraAccessDenied) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
duration: Duration(seconds: 5),
content: Text('Camera Access Denied.')));
} else {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
duration: Duration(seconds: 5),
content: Text('Unknown error: $e')));
}
} on FormatException {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
duration: Duration(seconds: 5),
content: Text('Nothing Captured')));
Navigator.pop(context);
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
duration: Duration(seconds: 5),
content: Text('Unknown error: $e')));
}
},
child: Text(
'Open Scanner',
style: TextStyle(
color: Colors.lightGreen, fontWeight: FontWeight.bold),
),
shape: RoundedRectangleBorder(
side: BorderSide(color: Colors.green, width: 3.0),
borderRadius: BorderRadius.circular(20.0)),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: const Divider(
color: Colors.black,
height: 20,
thickness: 1,
endIndent: 0,
),
),
StreamBuilder(
stream: TreeProvider().getTrees().asStream(),
builder:
(BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.hasError) {
return new Text("Error!");
} else if (snapshot.data == null) {
return LinearProgressIndicator();
} else {
return Container(
height: 400,
child: ListView(
children: [
for (int i = 0; i < snapshot.data.length; i++)
TreeListItem(i + 1, Tree.fromMap(snapshot.data[i]))
],
),
);
}
},
)
],
),
),
),
));
}
}