Skip to content

Commit 61d54ac

Browse files
authored
🚸 Adapt device orientation layouts (#185)
1 parent a4dae89 commit 61d54ac

File tree

4 files changed

+75
-12
lines changed

4 files changed

+75
-12
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ that can be found in the LICENSE file. -->
66

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

9+
## 4.0.0-dev.3
10+
11+
### Improvements
12+
13+
- Adapt layouts according to the device orientation.
14+
915
## 4.0.0-dev.2
1016

1117
### New features

guides/migration_guide.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ few naming or signatures of methods are changed. including:
2424
- `buildCameraPreview`
2525
- `buildCaptureButton`
2626
- `buildFocusingPoint`
27+
- `buildForegroundBody`
2728

2829
### Details
2930

@@ -53,3 +54,13 @@ few naming or signatures of methods are changed. including:
5354
int quarterTurns = 0,
5455
})
5556
```
57+
- `buildForegroundBody` now adds `DeviceOrientation? deviceOrientation`
58+
to make responsive layouts according to the device orientation.
59+
So the signature becomes:
60+
```dart
61+
Widget buildForegroundBody(
62+
BuildContext context,
63+
BoxConstraints constraints,
64+
DeviceOrientation? deviceOrientation,
65+
)
66+
```

lib/src/states/camera_picker_state.dart

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -956,11 +956,13 @@ class CameraPickerState extends State<CameraPicker>
956956
child: flashModeSwitch,
957957
);
958958
}
959+
final isPortrait = v.deviceOrientation.toString().contains('portrait');
959960
return Padding(
960961
padding: const EdgeInsets.symmetric(horizontal: 12),
961-
child: Row(
962+
child: Flex(
963+
direction: isPortrait ? Axis.horizontal : Axis.vertical,
962964
children: <Widget>[
963-
if (innerController?.value.isRecordingVideo != true) backButton,
965+
if (!v.isRecordingVideo) backButton,
964966
const Spacer(),
965967
flashModeSwitch,
966968
],
@@ -1055,9 +1057,17 @@ class CameraPickerState extends State<CameraPicker>
10551057
required BoxConstraints constraints,
10561058
CameraController? controller,
10571059
}) {
1060+
final orientation = controller?.value.deviceOrientation ??
1061+
MediaQuery.of(context).orientation;
1062+
final isPortrait = orientation.toString().contains('portrait');
10581063
return SizedBox(
1059-
height: 118,
1060-
child: Row(
1064+
width: isPortrait ? null : 118,
1065+
height: isPortrait ? 118 : null,
1066+
child: Flex(
1067+
direction: isPortrait ? Axis.horizontal : Axis.vertical,
1068+
verticalDirection: orientation == DeviceOrientation.landscapeLeft
1069+
? VerticalDirection.up
1070+
: VerticalDirection.down,
10611071
children: <Widget>[
10621072
const Spacer(),
10631073
Expanded(
@@ -1268,7 +1278,8 @@ class CameraPickerState extends State<CameraPicker>
12681278

12691279
Widget buildFromPoint(Offset point) {
12701280
const double controllerWidth = 20;
1271-
final double pointWidth = constraints.maxWidth / 5;
1281+
final double pointWidth =
1282+
math.min(constraints.maxWidth, constraints.maxHeight) / 5;
12721283
final double lineHeight = pointWidth * 2.5;
12731284
final double exposureControlWidth =
12741285
pickerConfig.enableExposureControlOnPoint ? controllerWidth : 0;
@@ -1437,7 +1448,12 @@ class CameraPickerState extends State<CameraPicker>
14371448
child: RotatedBox(
14381449
quarterTurns: cameraQuarterTurns,
14391450
child: Align(
1440-
alignment: Alignment.bottomCenter,
1451+
alignment: {
1452+
DeviceOrientation.portraitUp: Alignment.bottomCenter,
1453+
DeviceOrientation.portraitDown: Alignment.topCenter,
1454+
DeviceOrientation.landscapeLeft: Alignment.centerRight,
1455+
DeviceOrientation.landscapeRight: Alignment.centerLeft,
1456+
}[cameraValue.deviceOrientation]!,
14411457
child: buildCaptureTips(innerController),
14421458
),
14431459
),
@@ -1497,11 +1513,24 @@ class CameraPickerState extends State<CameraPicker>
14971513
);
14981514
}
14991515

1500-
Widget buildForegroundBody(BuildContext context, BoxConstraints constraints) {
1516+
Widget buildForegroundBody(
1517+
BuildContext context,
1518+
BoxConstraints constraints,
1519+
DeviceOrientation? deviceOrientation,
1520+
) {
1521+
final orientation = deviceOrientation ?? MediaQuery.of(context).orientation;
1522+
final isPortrait = orientation.toString().contains('portrait');
15011523
return SafeArea(
15021524
child: Padding(
15031525
padding: const EdgeInsets.only(bottom: 20),
1504-
child: Column(
1526+
child: Flex(
1527+
direction: isPortrait ? Axis.vertical : Axis.horizontal,
1528+
textDirection: orientation == DeviceOrientation.landscapeRight
1529+
? TextDirection.rtl
1530+
: TextDirection.ltr,
1531+
verticalDirection: orientation == DeviceOrientation.portraitDown
1532+
? VerticalDirection.up
1533+
: VerticalDirection.down,
15051534
children: <Widget>[
15061535
Semantics(
15071536
sortKey: const OrdinalSortKey(0),
@@ -1540,9 +1569,17 @@ class CameraPickerState extends State<CameraPicker>
15401569
);
15411570
}
15421571
return Align(
1543-
alignment: AlignmentDirectional.topCenter,
1572+
alignment: {
1573+
DeviceOrientation.portraitUp: Alignment.topCenter,
1574+
DeviceOrientation.portraitDown: Alignment.bottomCenter,
1575+
DeviceOrientation.landscapeLeft: Alignment.centerLeft,
1576+
DeviceOrientation.landscapeRight: Alignment.centerRight,
1577+
}[v.deviceOrientation]!,
15441578
child: AspectRatio(
1545-
aspectRatio: 1 / v.aspectRatio,
1579+
aspectRatio:
1580+
v.deviceOrientation.toString().contains('portrait')
1581+
? 1 / v.aspectRatio
1582+
: v.aspectRatio,
15461583
child: LayoutBuilder(
15471584
builder: (BuildContext c, BoxConstraints constraints) {
15481585
return buildCameraPreview(
@@ -1602,7 +1639,16 @@ class CameraPickerState extends State<CameraPicker>
16021639
pickerConfig.foregroundBuilder!(context, innerController),
16031640
),
16041641
],
1605-
buildForegroundBody(context, constraints),
1642+
if (innerController == null)
1643+
buildForegroundBody(context, constraints, null)
1644+
else
1645+
buildInitializeWrapper(
1646+
builder: (CameraValue v, _) => buildForegroundBody(
1647+
context,
1648+
constraints,
1649+
v.deviceOrientation,
1650+
),
1651+
),
16061652
],
16071653
);
16081654
},

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: wechat_camera_picker
22
description: A camera picker based on WeChat's UI which is a separate runnable extension to wechat_assets_picker.
33
repository: https://github.com/fluttercandies/flutter_wechat_camera_picker
4-
version: 4.0.0-dev.2
4+
version: 4.0.0-dev.3
55

66
environment:
77
sdk: ">=2.18.0 <3.0.0"

0 commit comments

Comments
 (0)