1
1
import 'dart:io' ;
2
2
3
3
import 'package:flutter/material.dart' ;
4
+ import 'package:powersync_flutter_demo/attachments/camera_helpers.dart' ;
4
5
import 'package:powersync_flutter_demo/attachments/photo_capture_widget.dart' ;
5
6
import 'package:powersync_flutter_demo/attachments/queue.dart' ;
6
7
@@ -19,31 +20,58 @@ class PhotoWidget extends StatefulWidget {
19
20
}
20
21
}
21
22
23
+ class _ResolvedPhotoState {
24
+ String ? photoPath;
25
+ bool fileExists;
26
+
27
+ _ResolvedPhotoState ({required this .photoPath, required this .fileExists});
28
+ }
29
+
22
30
class _PhotoWidgetState extends State <PhotoWidget > {
23
31
late String photoPath;
24
32
25
- Future <Map < String , dynamic >> _getPhoto (photoId) async {
33
+ Future <_ResolvedPhotoState > _getPhotoState (photoId) async {
26
34
if (photoId == null ) {
27
- return { " photoPath" : null , " fileExists" : false } ;
35
+ return _ResolvedPhotoState ( photoPath: null , fileExists: false ) ;
28
36
}
29
37
photoPath = await attachmentQueue.getLocalUri ('$photoId .jpg' );
30
38
31
39
bool fileExists = await File (photoPath).exists ();
32
40
33
- return { " photoPath" : photoPath, " fileExists" : fileExists} ;
41
+ return _ResolvedPhotoState ( photoPath: photoPath, fileExists: fileExists) ;
34
42
}
35
43
36
44
@override
37
45
Widget build (BuildContext context) {
38
46
return FutureBuilder (
39
- future: _getPhoto (widget.todo.photoId),
40
- builder: (BuildContext context, AsyncSnapshot snapshot) {
47
+ future: _getPhotoState (widget.todo.photoId),
48
+ builder: (BuildContext context,
49
+ AsyncSnapshot <_ResolvedPhotoState > snapshot) {
50
+ if (snapshot.data == null ) {
51
+ return Container ();
52
+ }
53
+ final data = snapshot.data! ;
41
54
Widget takePhotoButton = ElevatedButton (
42
- onPressed: () {
55
+ onPressed: () async {
56
+ final camera = await setupCamera ();
57
+ if (! mounted) return ;
58
+
59
+ if (camera == null ) {
60
+ const snackBar = SnackBar (
61
+ content: Text ('No camera available' ),
62
+ backgroundColor:
63
+ Colors .red, // Optional: to highlight it's an error
64
+ );
65
+
66
+ ScaffoldMessenger .of (context).showSnackBar (snackBar);
67
+ return ;
68
+ }
69
+
43
70
Navigator .push (
44
71
context,
45
72
MaterialPageRoute (
46
- builder: (context) => TakePhotoWidget (todoId: widget.todo.id),
73
+ builder: (context) =>
74
+ TakePhotoWidget (todoId: widget.todo.id, camera: camera),
47
75
),
48
76
);
49
77
},
@@ -54,29 +82,25 @@ class _PhotoWidgetState extends State<PhotoWidget> {
54
82
return takePhotoButton;
55
83
}
56
84
57
- if (snapshot.hasData) {
58
- String filePath = snapshot.data['photoPath' ];
59
- bool fileIsDownloading = ! snapshot.data['fileExists' ];
60
-
61
- if (fileIsDownloading) {
62
- return const Text ("Downloading..." );
63
- }
64
-
65
- File imageFile = File (filePath);
66
- int lastModified = imageFile.existsSync ()
67
- ? imageFile.lastModifiedSync ().millisecondsSinceEpoch
68
- : 0 ;
69
- Key key = ObjectKey ('$filePath :$lastModified ' );
70
-
71
- return Image .file (
72
- key: key,
73
- imageFile,
74
- width: 50 ,
75
- height: 50 ,
76
- );
85
+ String ? filePath = data.photoPath;
86
+ bool fileIsDownloading = ! data.fileExists;
87
+
88
+ if (fileIsDownloading) {
89
+ return const Text ("Downloading..." );
77
90
}
78
91
79
- return takePhotoButton;
92
+ File imageFile = File (filePath! );
93
+ int lastModified = imageFile.existsSync ()
94
+ ? imageFile.lastModifiedSync ().millisecondsSinceEpoch
95
+ : 0 ;
96
+ Key key = ObjectKey ('$filePath :$lastModified ' );
97
+
98
+ return Image .file (
99
+ key: key,
100
+ imageFile,
101
+ width: 50 ,
102
+ height: 50 ,
103
+ );
80
104
});
81
105
}
82
106
}
0 commit comments