-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchange_camera_config_example.dart
156 lines (137 loc) · 3.83 KB
/
change_camera_config_example.dart
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
117
118
119
120
121
122
123
124
125
126
127
128
129
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
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:scan_barcode/scan_barcode.dart';
class ChangeCameraConfigExample extends StatefulWidget {
const ChangeCameraConfigExample({Key? key}) : super(key: key);
@override
State<ChangeCameraConfigExample> createState() =>
_ChangeCameraConfigExampleState();
}
class _ChangeCameraConfigExampleState extends State<ChangeCameraConfigExample> {
final scanValue = ScanValue();
CameraController? controller;
@override
Widget build(BuildContext context) {
return Scaffold(
body: SizedBox(
height: double.infinity,
child: Stack(
children: [
_buildBarcodeWidget(context),
SizedBox(
height: 56 + MediaQuery.of(context).padding.top,
child: AppBar(
title: const Text('Change Camera Config Example'),
),
),
Positioned(
bottom: 0,
left: 0,
right: 0,
height: 100,
child: options(),
),
],
),
),
);
}
Widget _buildBarcodeWidget(BuildContext context) {
return BarcodeWidget(
onHandleBarcodeList: onHandleBarcodeList,
scanValue: scanValue,
onCameraControllerCreate: (controller) {
this.controller = controller;
},
);
}
Widget options() {
return Row(
children: [
_buildFlash(),
_buildCamera(),
_buildChangePresent(),
].map((e) => Expanded(child: e)).toList(),
);
}
var flashOn = false;
Widget _buildFlash() {
final icon = flashOn ? Icons.flash_on : Icons.flash_off;
return IconButton(
icon: Icon(icon),
onPressed: () {
if (controller == null) {
log('controller is null, please wait');
return;
}
setState(() {
flashOn = !flashOn;
});
controller!.setFlashMode(flashOn ? FlashMode.torch : FlashMode.off);
},
);
}
var backCamera = true;
Widget _buildCamera() {
final icon = backCamera ? Icons.camera_front : Icons.camera_rear;
return IconButton(
icon: Icon(icon),
onPressed: () async {
if (controller == null) {
log('controller is null, please wait');
return;
}
setState(() {
backCamera = !backCamera;
});
final cameras = await availableCameras();
final camera = cameras.firstWhere((element) =>
element.lensDirection ==
(backCamera
? CameraLensDirection.back
: CameraLensDirection.front));
final oldConfig = scanValue.cameraConfig;
final newConfig = oldConfig.copyWith(
camera: camera,
);
scanValue.updateCameraConfig(newConfig);
},
);
}
ResolutionPreset present = ResolutionPreset.high;
Widget _buildChangePresent() {
return DropdownButton<ResolutionPreset>(
items: ResolutionPreset.values.map(
(e) {
final text = e.toString().split('.').last;
return DropdownMenuItem<ResolutionPreset>(
child: Text(text),
value: e,
);
},
).toList(),
value: present,
onChanged: (v) {
if (controller == null) {
log('controller is null, please wait');
return;
}
setState(() {
present = v!;
});
final oldConfig = scanValue.cameraConfig;
final newConfig = oldConfig.copyWith(
preset: present,
);
scanValue.updateCameraConfig(newConfig);
},
);
}
Future<void> onHandleBarcodeList(List<Barcode> barCode) async {
if (barCode.isEmpty) {
return;
}
log(barCode.map((e) => e.rawValue).join('\n'));
await Future.delayed(const Duration(seconds: 3));
}
}