Skip to content

Commit 6c1419f

Browse files
committed
Only load camera once the button is pressed.
1 parent bc5a4de commit 6c1419f

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

demos/supabase-todolist/lib/attachments/photo_widget.dart

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,22 @@ class PhotoWidget extends StatefulWidget {
2424
class _ResolvedPhotoState {
2525
String? photoPath;
2626
bool fileExists;
27-
CameraDescription? camera;
2827

29-
_ResolvedPhotoState(
30-
{required this.photoPath,
31-
required this.fileExists,
32-
required this.camera});
28+
_ResolvedPhotoState({required this.photoPath, required this.fileExists});
3329
}
3430

3531
class _PhotoWidgetState extends State<PhotoWidget> {
3632
late String photoPath;
3733

3834
Future<_ResolvedPhotoState> _getPhotoState(photoId) async {
39-
final camera = await setupCamera();
4035
if (photoId == null) {
41-
return _ResolvedPhotoState(
42-
photoPath: null, fileExists: false, camera: camera);
36+
return _ResolvedPhotoState(photoPath: null, fileExists: false);
4337
}
4438
photoPath = await attachmentQueue.getLocalUri('$photoId.jpg');
4539

4640
bool fileExists = await File(photoPath).exists();
4741

48-
return _ResolvedPhotoState(
49-
photoPath: null, fileExists: fileExists, camera: camera);
42+
return _ResolvedPhotoState(photoPath: null, fileExists: fileExists);
5043
}
5144

5245
@override
@@ -60,8 +53,11 @@ class _PhotoWidgetState extends State<PhotoWidget> {
6053
}
6154
final data = snapshot.data!;
6255
Widget takePhotoButton = ElevatedButton(
63-
onPressed: () {
64-
if (data.camera == null) {
56+
onPressed: () async {
57+
final camera = await setupCamera();
58+
if (!mounted) return;
59+
60+
if (camera == null) {
6561
const snackBar = SnackBar(
6662
content: Text('No camera available'),
6763
backgroundColor:
@@ -71,11 +67,12 @@ class _PhotoWidgetState extends State<PhotoWidget> {
7167
ScaffoldMessenger.of(context).showSnackBar(snackBar);
7268
return;
7369
}
70+
7471
Navigator.push(
7572
context,
7673
MaterialPageRoute(
77-
builder: (context) => TakePhotoWidget(
78-
todoId: widget.todo.id, camera: data.camera!),
74+
builder: (context) =>
75+
TakePhotoWidget(todoId: widget.todo.id, camera: camera),
7976
),
8077
);
8178
},

0 commit comments

Comments
 (0)