CameraProvider.dart 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 with ChangeNotifier {
  8. CameraController cameraController;
  9. List<CameraDescription> cameras;
  10. int curCamera = 0;
  11. String appFolder = "";
  12. String fileName;
  13. CameraProvider() {
  14. getCameras();
  15. }
  16. changeFileName(){
  17. String id=Uuid().v4().toString();
  18. fileName=p.join(appFolder,'$id.png');
  19. notifyListeners();
  20. }
  21. getCameras() async {
  22. Directory appDocDir = await getApplicationDocumentsDirectory();
  23. appFolder = appDocDir.path;
  24. cameras = await availableCameras();
  25. cameraController =
  26. CameraController(cameras[curCamera], ResolutionPreset.max);
  27. cameraController.initialize().then((_) {
  28. notifyListeners();
  29. });
  30. }
  31. changeCamera() {
  32. if (curCamera == 0) {
  33. curCamera = 1;
  34. } else {
  35. curCamera = 0;
  36. }
  37. cameraController =
  38. CameraController(cameras[curCamera], ResolutionPreset.max);
  39. cameraController.initialize().then((_) {
  40. notifyListeners();
  41. });
  42. }
  43. }