Skip to content

Commit e8e8da3

Browse files
authored
[camera] 🐛 Fix toggles overflow in the camera example (#9274)
| Before | After | | ------ | ------ | | ![Screenshot_2025-05-17-11-31-18-73_2088dbc85c3063e](https://github.com/user-attachments/assets/25fb9323-81dd-4fa0-8bd5-80cc5cb36459) | ![Screenshot_2025-05-17-11-37-47-38_2088dbc85c3063e](https://github.com/user-attachments/assets/9dffa9fa-de9a-4038-9984-ab7191659f72) | | ![Screenshot_2025-05-17-11-31-25-33_2088dbc85c3063e](https://github.com/user-attachments/assets/d3178853-afff-4e94-af3c-d2ab14accfa4) | ![Screenshot_2025-05-17-11-37-56-58_2088dbc85c3063e](https://github.com/user-attachments/assets/19390f1b-e374-4e69-b825-275c1a17683a) |
1 parent 83256f0 commit e8e8da3

File tree

3 files changed

+59
-37
lines changed

3 files changed

+59
-37
lines changed

packages/camera/camera/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## NEXT
2+
3+
* Fixes overflowed toggles in the camera example.
4+
15
## 0.11.1
26

37
* Adds API support query for image streaming.

packages/camera/camera/example/lib/main.dart

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -225,43 +225,33 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
225225

226226
/// Display the thumbnail of the captured image or video.
227227
Widget _thumbnailWidget() {
228-
final VideoPlayerController? localVideoController = videoController;
229-
230-
return Expanded(
231-
child: Align(
232-
alignment: Alignment.centerRight,
233-
child: Row(
234-
mainAxisSize: MainAxisSize.min,
235-
children: <Widget>[
236-
if (localVideoController == null && imageFile == null)
237-
Container()
238-
else
239-
SizedBox(
240-
width: 64.0,
241-
height: 64.0,
242-
child: (localVideoController == null)
243-
? (
244-
// The captured image on the web contains a network-accessible URL
245-
// pointing to a location within the browser. It may be displayed
246-
// either with Image.network or Image.memory after loading the image
247-
// bytes to memory.
248-
kIsWeb
249-
? Image.network(imageFile!.path)
250-
: Image.file(File(imageFile!.path)))
251-
: Container(
252-
decoration: BoxDecoration(
253-
border: Border.all(color: Colors.pink)),
254-
child: Center(
255-
child: AspectRatio(
256-
aspectRatio:
257-
localVideoController.value.aspectRatio,
258-
child: VideoPlayer(localVideoController)),
259-
),
260-
),
228+
return Row(
229+
mainAxisSize: MainAxisSize.min,
230+
children: <Widget>[
231+
if (videoController case final VideoPlayerController controller?)
232+
Container(
233+
width: 64.0,
234+
height: 64.0,
235+
decoration: BoxDecoration(border: Border.all(color: Colors.pink)),
236+
child: Center(
237+
child: AspectRatio(
238+
aspectRatio: controller.value.aspectRatio,
239+
child: VideoPlayer(controller),
261240
),
262-
],
263-
),
264-
),
241+
),
242+
)
243+
else if (imageFile?.path case final String path)
244+
Container(
245+
width: 64.0,
246+
height: 64.0,
247+
decoration: BoxDecoration(border: Border.all(color: Colors.pink)),
248+
// The captured image on the web contains a network-accessible URL
249+
// pointing to a location within the browser. It may be displayed
250+
// either with Image.network or Image.memory after loading the image
251+
// bytes to memory.
252+
child: kIsWeb ? Image.network(path) : Image.file(File(path)),
253+
),
254+
],
265255
);
266256
}
267257

@@ -598,7 +588,12 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
598588
}
599589
}
600590

601-
return Row(children: toggles);
591+
return Expanded(
592+
child: SizedBox(
593+
height: 56.0,
594+
child: ListView(scrollDirection: Axis.horizontal, children: toggles),
595+
),
596+
);
602597
}
603598

604599
String timestamp() => DateTime.now().millisecondsSinceEpoch.toString();
@@ -1048,6 +1043,9 @@ class CameraApp extends StatelessWidget {
10481043
}
10491044
}
10501045

1046+
/// Getting available cameras for testing.
1047+
@visibleForTesting
1048+
List<CameraDescription> get cameras => _cameras;
10511049
List<CameraDescription> _cameras = <CameraDescription>[];
10521050

10531051
Future<void> main() async {

packages/camera/camera/example/test/main_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:camera/camera.dart';
56
import 'package:camera_example/main.dart';
67
import 'package:flutter/material.dart';
78
import 'package:flutter_test/flutter_test.dart';
@@ -13,4 +14,23 @@ void main() {
1314
await tester.pumpAndSettle();
1415
expect(find.byType(SnackBar), findsOneWidget);
1516
});
17+
18+
testWidgets(
19+
'CameraDescription toggles will not overflow',
20+
(WidgetTester tester) async {
21+
WidgetsFlutterBinding.ensureInitialized();
22+
// Adds 10 fake camera descriptions.
23+
for (int i = 0; i < 10; i++) {
24+
cameras.add(
25+
CameraDescription(
26+
name: 'camera_$i',
27+
lensDirection: CameraLensDirection.back,
28+
sensorOrientation: 90,
29+
),
30+
);
31+
}
32+
await tester.pumpWidget(const CameraApp());
33+
await tester.pumpAndSettle();
34+
},
35+
);
1636
}

0 commit comments

Comments
 (0)