Fixed CameraPreview aspect ratio

Probably will be changed in future, is it currently relies on the `ResolutionPreset` being tied with aspect ratio
This commit is contained in:
Vadim 2023-01-07 13:23:54 +03:00
parent 56480cca0a
commit 5eb0869aa0
4 changed files with 66 additions and 20 deletions

34
.vscode/launch.json vendored
View file

@ -5,12 +5,26 @@
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{ {
"name": "dev", "name": "dev (android)",
"request": "launch", "request": "launch",
"type": "dart", "type": "dart",
"args": [ "args": [
"--flavor", "--flavor",
"dev", "dev",
"--dart-define",
"cameraPreviewAspectRatio=2/3",
],
"program": "${workspaceFolder}/lib/main.dart",
},
{
"name": "dev (ios)",
"request": "launch",
"type": "dart",
"args": [
"--flavor",
"dev",
"--dart-define",
"cameraPreviewAspectRatio=3/4",
], ],
"program": "${workspaceFolder}/lib/main.dart", "program": "${workspaceFolder}/lib/main.dart",
}, },
@ -21,16 +35,32 @@
"args": [ "args": [
"--flavor", "--flavor",
"dev", "dev",
"--dart-define",
"cameraPreviewAspectRatio=3/4",
], ],
"program": "${workspaceFolder}/lib/main_mock.dart", "program": "${workspaceFolder}/lib/main_mock.dart",
}, },
{ {
"name": "prod", "name": "prod (android)",
"request": "launch", "request": "launch",
"type": "dart", "type": "dart",
"args": [ "args": [
"--flavor", "--flavor",
"prod", "prod",
"--dart-define",
"cameraPreviewAspectRatio=2/3",
],
"program": "${workspaceFolder}/lib/main.dart",
},
{
"name": "prod (ios)",
"request": "launch",
"type": "dart",
"args": [
"--flavor",
"prod",
"--dart-define",
"cameraPreviewAspectRatio=3/4",
], ],
"program": "${workspaceFolder}/lib/main.dart", "program": "${workspaceFolder}/lib/main.dart",
}, },

View file

@ -12,3 +12,10 @@ Some time ago I've started developing the [Material Lightmeter](https://play.goo
But as the existing repo contained some sensitive data, that I've pushed due to lack of experience, I had to make a new one. And if creating a new repo, why not rewrite the app from scratch?) But as the existing repo contained some sensitive data, that I've pushed due to lack of experience, I had to make a new one. And if creating a new repo, why not rewrite the app from scratch?)
Without further delay behold my new Lightmeter app inspired by Material You (a.k.a. M3) Without further delay behold my new Lightmeter app inspired by Material You (a.k.a. M3)
## Build
```
flutter build apk --flavor dev --dart-define cameraPreviewAspectRatio=2/3
flutter build apk --flavor dev --dart-define cameraPreviewAspectRatio=2/3
```

6
lib/platform_config.dart Normal file
View file

@ -0,0 +1,6 @@
class PlatformConfig {
static double get cameraPreviewAspectRatio {
final rational = const String.fromEnvironment('cameraPreviewAspectRatio', defaultValue: "3/4").split('/');
return int.parse(rational[0]) / int.parse(rational[1]);
}
}

View file

@ -2,6 +2,7 @@ import 'package:camera/camera.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lightmeter/platform_config.dart';
import 'package:lightmeter/screens/metering/ev_source/camera/bloc_camera.dart'; import 'package:lightmeter/screens/metering/ev_source/camera/bloc_camera.dart';
import 'package:lightmeter/screens/metering/ev_source/camera/state_camera.dart'; import 'package:lightmeter/screens/metering/ev_source/camera/state_camera.dart';
@ -11,25 +12,27 @@ class CameraView extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return AspectRatio( return AspectRatio(
aspectRatio: 3 / 4, aspectRatio: PlatformConfig.cameraPreviewAspectRatio,
child: BlocBuilder<CameraBloc, CameraState>( child: Center(
buildWhen: (previous, current) => current is CameraInitializedState, child: BlocBuilder<CameraBloc, CameraState>(
builder: (context, state) { buildWhen: (previous, current) => current is CameraInitializedState,
if (state is CameraInitializedState) { builder: (context, state) {
final value = state.controller.value; if (state is CameraInitializedState) {
return ValueListenableBuilder<CameraValue>( final value = state.controller.value;
valueListenable: state.controller, return ValueListenableBuilder<CameraValue>(
builder: (_, __, ___) => AspectRatio( valueListenable: state.controller,
aspectRatio: _isLandscape(value) ? value.aspectRatio : (1 / value.aspectRatio), builder: (_, __, ___) => AspectRatio(
child: RotatedBox( aspectRatio: _isLandscape(value) ? value.aspectRatio : (1 / value.aspectRatio),
quarterTurns: _getQuarterTurns(value), child: RotatedBox(
child: state.controller.buildPreview(), quarterTurns: _getQuarterTurns(value),
child: state.controller.buildPreview(),
),
), ),
), );
); }
} return const ColoredBox(color: Colors.black);
return const ColoredBox(color: Colors.black); },
}, ),
), ),
); );
} }