add volume listener #3

This commit is contained in:
otsmr 2025-03-22 20:35:32 +01:00
parent 06feb2d18c
commit c1322525f7
4 changed files with 87 additions and 57 deletions

View file

@ -73,6 +73,8 @@ PODS:
- flutter_secure_storage_darwin (10.0.0): - flutter_secure_storage_darwin (10.0.0):
- Flutter - Flutter
- FlutterMacOS - FlutterMacOS
- flutter_volume_controller (0.0.1):
- Flutter
- gal (1.0.0): - gal (1.0.0):
- Flutter - Flutter
- FlutterMacOS - FlutterMacOS
@ -225,6 +227,7 @@ DEPENDENCIES:
- flutter_keyboard_visibility (from `.symlinks/plugins/flutter_keyboard_visibility/ios`) - flutter_keyboard_visibility (from `.symlinks/plugins/flutter_keyboard_visibility/ios`)
- flutter_local_notifications (from `.symlinks/plugins/flutter_local_notifications/ios`) - flutter_local_notifications (from `.symlinks/plugins/flutter_local_notifications/ios`)
- flutter_secure_storage_darwin (from `.symlinks/plugins/flutter_secure_storage_darwin/darwin`) - 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`) - gal (from `.symlinks/plugins/gal/darwin`)
- GoogleUtilities - GoogleUtilities
- local_auth_darwin (from `.symlinks/plugins/local_auth_darwin/darwin`) - local_auth_darwin (from `.symlinks/plugins/local_auth_darwin/darwin`)
@ -279,6 +282,8 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/flutter_local_notifications/ios" :path: ".symlinks/plugins/flutter_local_notifications/ios"
flutter_secure_storage_darwin: flutter_secure_storage_darwin:
:path: ".symlinks/plugins/flutter_secure_storage_darwin/darwin" :path: ".symlinks/plugins/flutter_secure_storage_darwin/darwin"
flutter_volume_controller:
:path: ".symlinks/plugins/flutter_volume_controller/ios"
gal: gal:
:path: ".symlinks/plugins/gal/darwin" :path: ".symlinks/plugins/gal/darwin"
local_auth_darwin: local_auth_darwin:
@ -317,6 +322,7 @@ SPEC CHECKSUMS:
flutter_keyboard_visibility: 4625131e43015dbbe759d9b20daaf77e0e3f6619 flutter_keyboard_visibility: 4625131e43015dbbe759d9b20daaf77e0e3f6619
flutter_local_notifications: 395056b3175ba4f08480a7c5de30cd36d69827e4 flutter_local_notifications: 395056b3175ba4f08480a7c5de30cd36d69827e4
flutter_secure_storage_darwin: ce237a8775b39723566dc72571190a3769d70468 flutter_secure_storage_darwin: ce237a8775b39723566dc72571190a3769d70468
flutter_volume_controller: c2be490cb0487e8b88d0d9fc2b7e1c139a4ebccb
gal: baecd024ebfd13c441269ca7404792a7152fde89 gal: baecd024ebfd13c441269ca7404792a7152fde89
GoogleAppMeasurement: fc0817122bd4d4189164f85374e06773b9561896 GoogleAppMeasurement: fc0817122bd4d4189164f85374e06773b9561896
GoogleDataTransport: aae35b7ea0c09004c3797d53c8c41f66f219d6a7 GoogleDataTransport: aae35b7ea0c09004c3797d53c8c41f66f219d6a7

View file

@ -1,6 +1,7 @@
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:camera/camera.dart'; import 'package:camera/camera.dart';
import 'package:flutter/material.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:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:screenshot/screenshot.dart'; import 'package:screenshot/screenshot.dart';
import 'package:twonly/globals.dart'; import 'package:twonly/globals.dart';
@ -56,6 +57,7 @@ class _CameraPreviewViewState extends State<CameraPreviewView> {
bool isZoomAble = false; bool isZoomAble = false;
double basePanY = 0; double basePanY = 0;
double baseScaleFactor = 0; double baseScaleFactor = 0;
bool cameraLoaded = false;
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>(); final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
late CameraController controller; late CameraController controller;
@ -65,6 +67,26 @@ class _CameraPreviewViewState extends State<CameraPreviewView> {
void initState() { void initState() {
super.initState(); super.initState();
selectCamera(0, init: true); 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}) { void selectCamera(int sCameraId, {bool init = false}) {
@ -88,7 +110,9 @@ class _CameraPreviewViewState extends State<CameraPreviewView> {
isZoomAble = await controller.getMinZoomLevel() != isZoomAble = await controller.getMinZoomLevel() !=
await controller.getMaxZoomLevel(); await controller.getMaxZoomLevel();
setState(() {}); setState(() {
cameraLoaded = true;
});
}).catchError((Object e) { }).catchError((Object e) {
if (e is CameraException) { if (e is CameraException) {
switch (e.code) { switch (e.code) {
@ -123,12 +147,53 @@ class _CameraPreviewViewState extends State<CameraPreviewView> {
}); });
} }
@override Future takePicture() async {
void dispose() { if (isFlashOn) {
if (cameraId < gCameras.length) { if (isFront) {
controller.dispose(); setState(() {
showSelfieFlash = true;
});
} else {
controller.setFlashMode(FlashMode.torch);
} }
super.dispose(); await Future.delayed(Duration(milliseconds: 1000));
}
await controller.pausePreview();
if (!context.mounted) return;
controller.setFlashMode(isFlashOn ? FlashMode.always : FlashMode.off);
Future<Uint8List?> 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 => bool get isFront =>
@ -261,57 +326,7 @@ class _CameraPreviewViewState extends State<CameraPreviewView> {
const SizedBox(height: 30), const SizedBox(height: 30),
GestureDetector( GestureDetector(
onTap: () async { onTap: () async {
if (isFlashOn) { takePicture();
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<Uint8List?> 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;
});
}, },
onLongPress: () async {}, onLongPress: () async {},
child: Align( child: Align(

View file

@ -648,6 +648,14 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" 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: flutter_web_plugins:
dependency: transitive dependency: transitive
description: flutter description: flutter

View file

@ -54,6 +54,7 @@ dependencies:
camera: ^0.11.1 camera: ^0.11.1
avatar_maker: ^0.2.0 avatar_maker: ^0.2.0
flutter_svg: ^2.0.17 flutter_svg: ^2.0.17
flutter_volume_controller: ^1.3.3
# avatar_maker # avatar_maker
# avatar_maker: # avatar_maker:
# path: ./dependencies/avatar_maker/ # path: ./dependencies/avatar_maker/