11import 'dart:io' ;
22
33import 'package:flutter/material.dart' ;
4+ import 'package:powersync_flutter_demo/attachments/camera_helpers.dart' ;
45import 'package:powersync_flutter_demo/attachments/photo_capture_widget.dart' ;
56import 'package:powersync_flutter_demo/attachments/queue.dart' ;
67
@@ -19,31 +20,58 @@ class PhotoWidget extends StatefulWidget {
1920 }
2021}
2122
23+ class _ResolvedPhotoState {
24+ String ? photoPath;
25+ bool fileExists;
26+
27+ _ResolvedPhotoState ({required this .photoPath, required this .fileExists});
28+ }
29+
2230class _PhotoWidgetState extends State <PhotoWidget > {
2331 late String photoPath;
2432
25- Future <Map < String , dynamic >> _getPhoto (photoId) async {
33+ Future <_ResolvedPhotoState > _getPhotoState (photoId) async {
2634 if (photoId == null ) {
27- return { " photoPath" : null , " fileExists" : false } ;
35+ return _ResolvedPhotoState ( photoPath: null , fileExists: false ) ;
2836 }
2937 photoPath = await attachmentQueue.getLocalUri ('$photoId .jpg' );
3038
3139 bool fileExists = await File (photoPath).exists ();
3240
33- return { " photoPath" : photoPath, " fileExists" : fileExists} ;
41+ return _ResolvedPhotoState ( photoPath: photoPath, fileExists: fileExists) ;
3442 }
3543
3644 @override
3745 Widget build (BuildContext context) {
3846 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! ;
4154 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+
4370 Navigator .push (
4471 context,
4572 MaterialPageRoute (
46- builder: (context) => TakePhotoWidget (todoId: widget.todo.id),
73+ builder: (context) =>
74+ TakePhotoWidget (todoId: widget.todo.id, camera: camera),
4775 ),
4876 );
4977 },
@@ -54,29 +82,25 @@ class _PhotoWidgetState extends State<PhotoWidget> {
5482 return takePhotoButton;
5583 }
5684
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..." );
7790 }
7891
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+ );
80104 });
81105 }
82106}
0 commit comments