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",
"configurations": [
{
"name": "dev",
"name": "dev (android)",
"request": "launch",
"type": "dart",
"args": [
"--flavor",
"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",
},
@ -21,16 +35,32 @@
"args": [
"--flavor",
"dev",
"--dart-define",
"cameraPreviewAspectRatio=3/4",
],
"program": "${workspaceFolder}/lib/main_mock.dart",
},
{
"name": "prod",
"name": "prod (android)",
"request": "launch",
"type": "dart",
"args": [
"--flavor",
"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",
},

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