CameraProvider.dart 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:camera/camera.dart';
  4. import 'package:path/path.dart' as p;
  5. import 'package:path_provider/path_provider.dart';
  6. import 'package:uuid/uuid.dart';
  7. class CameraProvider extends State<StatefulWidget>
  8. with ChangeNotifier, TickerProviderStateMixin {
  9. CameraController cameraController;
  10. TabController tabController;
  11. List<CameraDescription> cameras;
  12. int curCamera = 0;
  13. String appFolder = "";
  14. String fileName;
  15. Widget photoButton;
  16. bool ifMakeVideo=false;
  17. CameraProvider() {
  18. tabController=TabController(length: 6,vsync: this);
  19. getCameras();
  20. }
  21. changeFileName(afterFix) {
  22. String id = Uuid().v4().toString();
  23. fileName = p.join(appFolder, '$id.$afterFix');
  24. notifyListeners();
  25. }
  26. getCameras() async {
  27. Directory appDocDir = await getApplicationDocumentsDirectory();
  28. if (!Directory(appDocDir.path).existsSync()) {
  29. appDocDir.createSync();
  30. }
  31. appFolder = appDocDir.path;
  32. cameras = await availableCameras();
  33. cameraController =
  34. CameraController(cameras[curCamera], ResolutionPreset.high);
  35. cameraController.initialize().then((_) {
  36. cameraController.prepareForVideoRecording();
  37. notifyListeners();
  38. });
  39. }
  40. changeCamera() {
  41. if (curCamera == 0) {
  42. curCamera = 1;
  43. } else {
  44. curCamera = 0;
  45. }
  46. cameraController =
  47. CameraController(cameras[curCamera], ResolutionPreset.max);
  48. cameraController.initialize().then((_) {
  49. notifyListeners();
  50. });
  51. }
  52. changePhotoWidget(){
  53. ifMakeVideo=!ifMakeVideo;
  54. notifyListeners();
  55. }
  56. @override
  57. Widget build(BuildContext context) {
  58. // TODO: implement build
  59. return null;
  60. }
  61. }