Skip to content

Commit

Permalink
🚸 Adapt device orientation layouts (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 authored Aug 17, 2023
1 parent a4dae89 commit 61d54ac
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ that can be found in the LICENSE file. -->

See the [Migration Guide](guides/migration_guide.md) for the details of breaking changes between versions.

## 4.0.0-dev.3

### Improvements

- Adapt layouts according to the device orientation.

## 4.0.0-dev.2

### New features
Expand Down
11 changes: 11 additions & 0 deletions guides/migration_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ few naming or signatures of methods are changed. including:
- `buildCameraPreview`
- `buildCaptureButton`
- `buildFocusingPoint`
- `buildForegroundBody`

### Details

Expand Down Expand Up @@ -53,3 +54,13 @@ few naming or signatures of methods are changed. including:
int quarterTurns = 0,
})
```
- `buildForegroundBody` now adds `DeviceOrientation? deviceOrientation`
to make responsive layouts according to the device orientation.
So the signature becomes:
```dart
Widget buildForegroundBody(
BuildContext context,
BoxConstraints constraints,
DeviceOrientation? deviceOrientation,
)
```
68 changes: 57 additions & 11 deletions lib/src/states/camera_picker_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -956,11 +956,13 @@ class CameraPickerState extends State<CameraPicker>
child: flashModeSwitch,
);
}
final isPortrait = v.deviceOrientation.toString().contains('portrait');
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 12),
child: Row(
child: Flex(
direction: isPortrait ? Axis.horizontal : Axis.vertical,
children: <Widget>[
if (innerController?.value.isRecordingVideo != true) backButton,
if (!v.isRecordingVideo) backButton,
const Spacer(),
flashModeSwitch,
],
Expand Down Expand Up @@ -1055,9 +1057,17 @@ class CameraPickerState extends State<CameraPicker>
required BoxConstraints constraints,
CameraController? controller,
}) {
final orientation = controller?.value.deviceOrientation ??
MediaQuery.of(context).orientation;
final isPortrait = orientation.toString().contains('portrait');
return SizedBox(
height: 118,
child: Row(
width: isPortrait ? null : 118,
height: isPortrait ? 118 : null,
child: Flex(
direction: isPortrait ? Axis.horizontal : Axis.vertical,
verticalDirection: orientation == DeviceOrientation.landscapeLeft
? VerticalDirection.up
: VerticalDirection.down,
children: <Widget>[
const Spacer(),
Expanded(
Expand Down Expand Up @@ -1268,7 +1278,8 @@ class CameraPickerState extends State<CameraPicker>

Widget buildFromPoint(Offset point) {
const double controllerWidth = 20;
final double pointWidth = constraints.maxWidth / 5;
final double pointWidth =
math.min(constraints.maxWidth, constraints.maxHeight) / 5;
final double lineHeight = pointWidth * 2.5;
final double exposureControlWidth =
pickerConfig.enableExposureControlOnPoint ? controllerWidth : 0;
Expand Down Expand Up @@ -1437,7 +1448,12 @@ class CameraPickerState extends State<CameraPicker>
child: RotatedBox(
quarterTurns: cameraQuarterTurns,
child: Align(
alignment: Alignment.bottomCenter,
alignment: {
DeviceOrientation.portraitUp: Alignment.bottomCenter,
DeviceOrientation.portraitDown: Alignment.topCenter,
DeviceOrientation.landscapeLeft: Alignment.centerRight,
DeviceOrientation.landscapeRight: Alignment.centerLeft,
}[cameraValue.deviceOrientation]!,
child: buildCaptureTips(innerController),
),
),
Expand Down Expand Up @@ -1497,11 +1513,24 @@ class CameraPickerState extends State<CameraPicker>
);
}

Widget buildForegroundBody(BuildContext context, BoxConstraints constraints) {
Widget buildForegroundBody(
BuildContext context,
BoxConstraints constraints,
DeviceOrientation? deviceOrientation,
) {
final orientation = deviceOrientation ?? MediaQuery.of(context).orientation;
final isPortrait = orientation.toString().contains('portrait');
return SafeArea(
child: Padding(
padding: const EdgeInsets.only(bottom: 20),
child: Column(
child: Flex(
direction: isPortrait ? Axis.vertical : Axis.horizontal,
textDirection: orientation == DeviceOrientation.landscapeRight
? TextDirection.rtl
: TextDirection.ltr,
verticalDirection: orientation == DeviceOrientation.portraitDown
? VerticalDirection.up
: VerticalDirection.down,
children: <Widget>[
Semantics(
sortKey: const OrdinalSortKey(0),
Expand Down Expand Up @@ -1540,9 +1569,17 @@ class CameraPickerState extends State<CameraPicker>
);
}
return Align(
alignment: AlignmentDirectional.topCenter,
alignment: {
DeviceOrientation.portraitUp: Alignment.topCenter,
DeviceOrientation.portraitDown: Alignment.bottomCenter,
DeviceOrientation.landscapeLeft: Alignment.centerLeft,
DeviceOrientation.landscapeRight: Alignment.centerRight,
}[v.deviceOrientation]!,
child: AspectRatio(
aspectRatio: 1 / v.aspectRatio,
aspectRatio:
v.deviceOrientation.toString().contains('portrait')
? 1 / v.aspectRatio
: v.aspectRatio,
child: LayoutBuilder(
builder: (BuildContext c, BoxConstraints constraints) {
return buildCameraPreview(
Expand Down Expand Up @@ -1602,7 +1639,16 @@ class CameraPickerState extends State<CameraPicker>
pickerConfig.foregroundBuilder!(context, innerController),
),
],
buildForegroundBody(context, constraints),
if (innerController == null)
buildForegroundBody(context, constraints, null)
else
buildInitializeWrapper(
builder: (CameraValue v, _) => buildForegroundBody(
context,
constraints,
v.deviceOrientation,
),
),
],
);
},
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: wechat_camera_picker
description: A camera picker based on WeChat's UI which is a separate runnable extension to wechat_assets_picker.
repository: https://github.com/fluttercandies/flutter_wechat_camera_picker
version: 4.0.0-dev.2
version: 4.0.0-dev.3

environment:
sdk: ">=2.18.0 <3.0.0"
Expand Down

0 comments on commit 61d54ac

Please sign in to comment.