Want to enable torch flash for camera in Flutter? Use torch dependency instead.
Here is an example using a torch dependency:
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 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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | class CameraHomeScreen extends StatefulWidget { final List<CameraDescription> cameras; CameraHomeScreen(this.cameras); @override State<StatefulWidget> createState() { return _CameraHomeScreenState(); } } class _CameraHomeScreenState extends State<CameraHomeScreen> { String imagePath; bool _toggleCamera = false; bool _toggleFlash = false; String _icFlash = "assets/icons/ic_flash_off.png"; String _lensDirection = "CameraLensDirection.back"; CameraController controller; @override void initState() { try { onCameraSelected(widget.cameras[0]); } catch (e) { print(e.toString()); } super.initState(); } @override void dispose() { controller?.dispose(); super.dispose(); } @override Widget build(BuildContext context) { if (widget.cameras.isEmpty) { return Container( alignment: Alignment.center, padding: EdgeInsets.all(16.0), child: Text( 'No Camera Found', style: TextStyle( fontSize: 16.0, color: Colors.white, ), ), ); } if (!controller.value.isInitialized) { return Container(); } return Transform.scale( scale: 1 / controller.value.aspectRatio, child: Center( child: AspectRatio( aspectRatio: controller.value.aspectRatio, child: Container( child: Stack( children: <Widget>[ CameraPreview(controller), Align( alignment: Alignment.bottomCenter, child: Container( width: MediaQuery.of(context).size.width, height: 100.0, color: Colors.transparent, child: Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.center, children: <Widget>[ _lensDirection == "CameraLensDirection.back" ? Material( color: Colors.transparent, child: InkWell( borderRadius: BorderRadius.all(Radius.circular(50.0)), onTap: () async { bool hasTorch = await Torch.hasTorch; if (hasTorch) { if (!_toggleFlash) { Torch.turnOn(); setState(() { _icFlash = "assets/icons/ic_flash_on.png"; _toggleFlash = true; }); } else { Torch.turnOff(); setState(() { _icFlash = "assets/icons/ic_flash_off.png"; _toggleFlash = false; }); } } }, child: Container( child: Image.asset( _icFlash, width: 15.0, height: 15.0, ), ), ), ) : Container( width: 15.0, height: 15.0, ), Material( color: Colors.transparent, child: InkWell( borderRadius: BorderRadius.all(Radius.circular(50.0)), onTap: () { _captureImage(); }, child: Container( child: Image.asset( 'assets/icons/ic_shutter.png', width: 50.0, height: 50.0, ), ), ), ), Material( color: Colors.transparent, child: InkWell( borderRadius: BorderRadius.all(Radius.circular(50.0)), onTap: () { if (!_toggleCamera) { onCameraSelected(widget.cameras[1]); setState(() { _toggleCamera = true; }); } else { onCameraSelected(widget.cameras[0]); setState(() { _toggleCamera = false; }); } }, child: Container( child: Image.asset( 'assets/icons/ic_switch_camera.png', width: 18.0, height: 18.0, ), ), ), ), ], ), ), ), ], ), ), ), ), ); } void onCameraSelected(CameraDescription cameraDescription) async { if (controller != null) await controller.dispose(); controller = CameraController(cameraDescription, ResolutionPreset.medium); controller.addListener(() { if (mounted) setState(() { _icFlash = "assets/icons/ic_flash_off.png"; _toggleFlash = false; _lensDirection = cameraDescription.lensDirection.toString(); }); if (controller.value.hasError) { showMessage('Camera Error: ${controller.value.errorDescription}'); } }); try { await controller.initialize(); } on CameraException catch (e) { showException(e); } if (mounted) setState(() {}); } String timestamp() => new DateTime.now().millisecondsSinceEpoch.toString(); void _captureImage() { takePicture().then((String filePath) { if (mounted) { setState(() { imagePath = filePath; }); if (filePath != null) { showMessage('Picture saved to $filePath'); setCameraResult(); } } }); } void setCameraResult() { // Navigator.pop(context, imagePath); Navigator.push( context, MaterialPageRoute(builder: (context) => Preview(path: imagePath)), ); } Future<String> takePicture() async { if (!controller.value.isInitialized) { showMessage('Error: select a camera first.'); return null; } final Directory extDir = await getApplicationDocumentsDirectory(); final String dirPath = '${extDir.path}/FlutterDevs/Camera/Images'; await new Directory(dirPath).create(recursive: true); final String filePath = '$dirPath/${timestamp()}.jpg'; if (controller.value.isTakingPicture) { // A capture is already pending, do nothing. return null; } try { await controller.takePicture(filePath); } on CameraException catch (e) { showException(e); return null; } return filePath; } void showException(CameraException e) { logError(e.code, e.description); showMessage('Error: ${e.code}\n${e.description}'); } void showMessage(String message) { print(message); } void logError(String code, String message) => print('Error: $code\nMessage: $message'); } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.