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
- 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

View file

@ -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<CameraPreviewView> {
bool isZoomAble = false;
double basePanY = 0;
double baseScaleFactor = 0;
bool cameraLoaded = false;
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
late CameraController controller;
@ -65,6 +67,26 @@ class _CameraPreviewViewState extends State<CameraPreviewView> {
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<CameraPreviewView> {
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<CameraPreviewView> {
});
}
@override
void dispose() {
if (cameraId < gCameras.length) {
controller.dispose();
Future takePicture() async {
if (isFlashOn) {
if (isFront) {
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 =>
@ -261,57 +326,7 @@ class _CameraPreviewViewState extends State<CameraPreviewView> {
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<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;
});
takePicture();
},
onLongPress: () async {},
child: Align(

View file

@ -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

View file

@ -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/