diff --git a/ios/Podfile.lock b/ios/Podfile.lock index bc929c8..72d6904 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -73,6 +73,8 @@ PODS: - flutter_secure_storage_darwin (10.0.0): - Flutter - FlutterMacOS + - flutter_volume_controller (0.0.1): + - Flutter - gal (1.0.0): - Flutter - FlutterMacOS @@ -225,6 +227,7 @@ DEPENDENCIES: - flutter_keyboard_visibility (from `.symlinks/plugins/flutter_keyboard_visibility/ios`) - flutter_local_notifications (from `.symlinks/plugins/flutter_local_notifications/ios`) - flutter_secure_storage_darwin (from `.symlinks/plugins/flutter_secure_storage_darwin/darwin`) + - flutter_volume_controller (from `.symlinks/plugins/flutter_volume_controller/ios`) - gal (from `.symlinks/plugins/gal/darwin`) - GoogleUtilities - local_auth_darwin (from `.symlinks/plugins/local_auth_darwin/darwin`) @@ -279,6 +282,8 @@ EXTERNAL SOURCES: :path: ".symlinks/plugins/flutter_local_notifications/ios" flutter_secure_storage_darwin: :path: ".symlinks/plugins/flutter_secure_storage_darwin/darwin" + flutter_volume_controller: + :path: ".symlinks/plugins/flutter_volume_controller/ios" gal: :path: ".symlinks/plugins/gal/darwin" local_auth_darwin: @@ -317,6 +322,7 @@ SPEC CHECKSUMS: flutter_keyboard_visibility: 4625131e43015dbbe759d9b20daaf77e0e3f6619 flutter_local_notifications: 395056b3175ba4f08480a7c5de30cd36d69827e4 flutter_secure_storage_darwin: ce237a8775b39723566dc72571190a3769d70468 + flutter_volume_controller: c2be490cb0487e8b88d0d9fc2b7e1c139a4ebccb gal: baecd024ebfd13c441269ca7404792a7152fde89 GoogleAppMeasurement: fc0817122bd4d4189164f85374e06773b9561896 GoogleDataTransport: aae35b7ea0c09004c3797d53c8c41f66f219d6a7 diff --git a/lib/src/views/camera_to_share/camera_preview_view.dart b/lib/src/views/camera_to_share/camera_preview_view.dart index dbd0a83..15558ca 100644 --- a/lib/src/views/camera_to_share/camera_preview_view.dart +++ b/lib/src/views/camera_to_share/camera_preview_view.dart @@ -1,6 +1,7 @@ import 'dart:typed_data'; import 'package:camera/camera.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_volume_controller/flutter_volume_controller.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:screenshot/screenshot.dart'; import 'package:twonly/globals.dart'; @@ -56,6 +57,7 @@ class _CameraPreviewViewState extends State { bool isZoomAble = false; double basePanY = 0; double baseScaleFactor = 0; + bool cameraLoaded = false; final GlobalKey navigatorKey = GlobalKey(); late CameraController controller; @@ -65,6 +67,26 @@ class _CameraPreviewViewState extends State { void initState() { super.initState(); selectCamera(0, init: true); + + FlutterVolumeController.addListener( + (volume) { + if (!cameraLoaded) { + // there is a bug, this is called at the start + return; + } + if (sharePreviewIsShown) return; + if (controller.value.isInitialized) takePicture(); + }, + ); + } + + @override + void dispose() { + FlutterVolumeController.removeListener(); + if (cameraId < gCameras.length) { + controller.dispose(); + } + super.dispose(); } void selectCamera(int sCameraId, {bool init = false}) { @@ -88,7 +110,9 @@ class _CameraPreviewViewState extends State { isZoomAble = await controller.getMinZoomLevel() != await controller.getMaxZoomLevel(); - setState(() {}); + setState(() { + cameraLoaded = true; + }); }).catchError((Object e) { if (e is CameraException) { switch (e.code) { @@ -123,12 +147,53 @@ class _CameraPreviewViewState extends State { }); } - @override - void dispose() { - if (cameraId < gCameras.length) { - controller.dispose(); + Future takePicture() async { + if (isFlashOn) { + if (isFront) { + setState(() { + showSelfieFlash = true; + }); + } else { + controller.setFlashMode(FlashMode.torch); + } + await Future.delayed(Duration(milliseconds: 1000)); } - super.dispose(); + + await controller.pausePreview(); + if (!context.mounted) return; + + controller.setFlashMode(isFlashOn ? FlashMode.always : FlashMode.off); + + Future imageBytes = screenshotController.capture(pixelRatio: 1); + + setState(() { + sharePreviewIsShown = true; + }); + await Navigator.push( + context, + PageRouteBuilder( + opaque: false, + pageBuilder: (context, a1, a2) => + ShareImageEditorView(imageBytes: imageBytes), + transitionsBuilder: (context, animation, secondaryAnimation, child) { + return child; + }, + transitionDuration: Duration.zero, + reverseTransitionDuration: Duration.zero, + ), + ); + // does not work?? + //await controller.resumePreview(); + selectCamera(0); + if (context.mounted) { + setState(() { + sharePreviewIsShown = false; + }); + } + + setState(() { + showSelfieFlash = false; + }); } bool get isFront => @@ -261,57 +326,7 @@ class _CameraPreviewViewState extends State { const SizedBox(height: 30), GestureDetector( onTap: () async { - if (isFlashOn) { - if (isFront) { - setState(() { - showSelfieFlash = true; - }); - } else { - controller.setFlashMode(FlashMode.torch); - } - await Future.delayed( - Duration(milliseconds: 1000)); - } - - await controller.pausePreview(); - if (!context.mounted) return; - - controller.setFlashMode( - isFlashOn ? FlashMode.always : FlashMode.off); - - Future imageBytes = - screenshotController.capture(pixelRatio: 1); - - setState(() { - sharePreviewIsShown = true; - }); - await Navigator.push( - context, - PageRouteBuilder( - opaque: false, - pageBuilder: (context, a1, a2) => - ShareImageEditorView( - imageBytes: imageBytes), - transitionsBuilder: (context, animation, - secondaryAnimation, child) { - return child; - }, - transitionDuration: Duration.zero, - reverseTransitionDuration: Duration.zero, - ), - ); - // does not work?? - //await controller.resumePreview(); - selectCamera(0); - if (context.mounted) { - setState(() { - sharePreviewIsShown = false; - }); - } - - setState(() { - showSelfieFlash = false; - }); + takePicture(); }, onLongPress: () async {}, child: Align( diff --git a/pubspec.lock b/pubspec.lock index 150abef..c178ad0 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -648,6 +648,14 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_volume_controller: + dependency: "direct main" + description: + name: flutter_volume_controller + sha256: "15f2c25bc4632ac5e8d42a208fe07c3224a4ee66b155d1ac86945b3db2bb58d9" + url: "https://pub.dev" + source: hosted + version: "1.3.3" flutter_web_plugins: dependency: transitive description: flutter diff --git a/pubspec.yaml b/pubspec.yaml index c0bba2a..d47f6c5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -54,6 +54,7 @@ dependencies: camera: ^0.11.1 avatar_maker: ^0.2.0 flutter_svg: ^2.0.17 + flutter_volume_controller: ^1.3.3 # avatar_maker # avatar_maker: # path: ./dependencies/avatar_maker/