refactor: Projektstruktur in app/ und backend/ aufgeteilt
This commit is contained in:
227
app/lib/tasks/barcode_capture_screen.dart
Normal file
227
app/lib/tasks/barcode_capture_screen.dart
Normal file
@@ -0,0 +1,227 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:mobile_scanner/mobile_scanner.dart';
|
||||
import '../l10n/app_localizations.dart';
|
||||
import '../models/tasks/barcode_task.dart';
|
||||
import '../widgets/offline_banner.dart';
|
||||
|
||||
class BarcodeCaptureScreen extends StatefulWidget {
|
||||
final BarcodeTask task;
|
||||
final Function(List<String>) onBarcodesCompleted;
|
||||
|
||||
const BarcodeCaptureScreen({super.key, required this.task, required this.onBarcodesCompleted});
|
||||
|
||||
@override
|
||||
State<BarcodeCaptureScreen> createState() => _BarcodeCaptureScreenState();
|
||||
}
|
||||
|
||||
class _BarcodeCaptureScreenState extends State<BarcodeCaptureScreen> {
|
||||
final List<String> _scannedBarcodes = [];
|
||||
final List<TextEditingController> _textControllers = [];
|
||||
MobileScannerController? _scannerController;
|
||||
bool _isMobilePlatform = false;
|
||||
bool _isScannerInitialized = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_detectPlatformAndInit();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_scannerController?.dispose();
|
||||
for (final controller in _textControllers) {
|
||||
controller.dispose();
|
||||
}
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _detectPlatformAndInit() {
|
||||
// Determine if we're on a mobile platform
|
||||
if (kIsWeb) {
|
||||
_isMobilePlatform = false;
|
||||
_initializeDesktopMode();
|
||||
} else {
|
||||
switch (defaultTargetPlatform) {
|
||||
case TargetPlatform.android:
|
||||
case TargetPlatform.iOS:
|
||||
_isMobilePlatform = true;
|
||||
_initializeMobileScanner();
|
||||
break;
|
||||
case TargetPlatform.macOS:
|
||||
case TargetPlatform.windows:
|
||||
case TargetPlatform.linux:
|
||||
_isMobilePlatform = false;
|
||||
_initializeDesktopMode();
|
||||
break;
|
||||
default:
|
||||
_isMobilePlatform = false;
|
||||
_initializeDesktopMode();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _initializeMobileScanner() {
|
||||
try {
|
||||
_scannerController = MobileScannerController();
|
||||
setState(() {
|
||||
_isScannerInitialized = true;
|
||||
});
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('${AppLocalizations.of(context).cameraError}: $e')));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _initializeDesktopMode() {
|
||||
// Create text controllers for desktop input fields
|
||||
for (int i = 0; i < widget.task.maxBarcodeCount; i++) {
|
||||
_textControllers.add(TextEditingController());
|
||||
}
|
||||
setState(() {
|
||||
_isScannerInitialized = true;
|
||||
});
|
||||
}
|
||||
|
||||
void _onBarcodeDetected(BarcodeCapture capture) {
|
||||
final List<Barcode> barcodes = capture.barcodes;
|
||||
for (final barcode in barcodes) {
|
||||
final String? code = barcode.rawValue;
|
||||
if (code != null && code.isNotEmpty && !_scannedBarcodes.contains(code)) {
|
||||
if (_scannedBarcodes.length < widget.task.maxBarcodeCount) {
|
||||
setState(() {
|
||||
_scannedBarcodes.add(code);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _removeBarcode(int index) {
|
||||
setState(() {
|
||||
_scannedBarcodes.removeAt(index);
|
||||
});
|
||||
}
|
||||
|
||||
void _finishTask() {
|
||||
final List<String> barcodes;
|
||||
if (_isMobilePlatform) {
|
||||
barcodes = _scannedBarcodes;
|
||||
} else {
|
||||
// Collect barcodes from text fields
|
||||
barcodes = [];
|
||||
for (final controller in _textControllers) {
|
||||
if (controller.text.trim().isNotEmpty) {
|
||||
barcodes.add(controller.text.trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Navigate back to task view first
|
||||
Navigator.of(context).pop();
|
||||
|
||||
// Then call the completion callback
|
||||
widget.onBarcodesCompleted(barcodes);
|
||||
}
|
||||
|
||||
bool _canFinish() {
|
||||
if (_isMobilePlatform) {
|
||||
return _scannedBarcodes.length >= widget.task.minBarcodeCount;
|
||||
} else {
|
||||
int filledFields = 0;
|
||||
for (final controller in _textControllers) {
|
||||
if (controller.text.trim().isNotEmpty) {
|
||||
filledFields++;
|
||||
}
|
||||
}
|
||||
return filledFields >= widget.task.minBarcodeCount;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(appBar: AppBar(title: Text(AppLocalizations.of(context).barcodeScan), backgroundColor: Colors.deepPurple[100], leading: IconButton(icon: const Icon(Icons.arrow_back), onPressed: () => Navigator.of(context).pop())), body: Column(children: [OfflineBanner(), Expanded(child: _isScannerInitialized ? (_isMobilePlatform ? _buildMobileView() : _buildDesktopView()) : const Center(child: CircularProgressIndicator()))]));
|
||||
}
|
||||
|
||||
Widget _buildMobileView() {
|
||||
return Column(
|
||||
children: [
|
||||
// Scanner view
|
||||
Expanded(
|
||||
flex: 3,
|
||||
child: Stack(
|
||||
children: [
|
||||
MobileScanner(controller: _scannerController, onDetect: _onBarcodeDetected),
|
||||
// Overlay with scanning frame
|
||||
Container(decoration: BoxDecoration(color: Colors.black.withValues(alpha: 0.5)), child: Center(child: Container(width: 250, height: 250, decoration: BoxDecoration(border: Border.all(color: Colors.white, width: 2), borderRadius: BorderRadius.circular(12)), child: Container(margin: const EdgeInsets.all(20), decoration: BoxDecoration(border: Border.all(color: Colors.green, width: 2), borderRadius: BorderRadius.circular(8)))))),
|
||||
],
|
||||
),
|
||||
),
|
||||
// Scanned barcodes list
|
||||
Expanded(
|
||||
flex: 2,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('${AppLocalizations.of(context).scannedBarcodes} (${_scannedBarcodes.length}/${widget.task.maxBarcodeCount})', style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
|
||||
const SizedBox(height: 8),
|
||||
Text('${AppLocalizations.of(context).minBarcodes} ${widget.task.minBarcodeCount} ${AppLocalizations.of(context).barcodesRequired}', style: TextStyle(fontSize: 14, color: Colors.grey[600])),
|
||||
const SizedBox(height: 16),
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
itemCount: _scannedBarcodes.length,
|
||||
itemBuilder: (context, index) {
|
||||
return Card(child: ListTile(leading: const Icon(Icons.qr_code), title: Text(_scannedBarcodes[index]), trailing: IconButton(icon: const Icon(Icons.delete, color: Colors.red), onPressed: () => _removeBarcode(index))));
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
SizedBox(width: double.infinity, child: ElevatedButton(onPressed: _canFinish() ? _finishTask : null, style: ElevatedButton.styleFrom(padding: const EdgeInsets.symmetric(vertical: 16)), child: Text(AppLocalizations.of(context).finish))),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDesktopView() {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(AppLocalizations.of(context).enterBarcode, style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold)),
|
||||
const SizedBox(height: 8),
|
||||
Text('${AppLocalizations.of(context).barcodeEnterDescription} (${widget.task.minBarcodeCount}-${widget.task.maxBarcodeCount})', style: TextStyle(fontSize: 16, color: Colors.grey[600])),
|
||||
const SizedBox(height: 24),
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
itemCount: widget.task.maxBarcodeCount,
|
||||
itemBuilder: (context, index) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 12),
|
||||
child: TextField(
|
||||
controller: _textControllers[index],
|
||||
decoration: InputDecoration(labelText: index < widget.task.minBarcodeCount ? AppLocalizations.of(context).barcodeNumberRequired(index + 1) : AppLocalizations.of(context).barcodeNumberOptional(index + 1), border: const OutlineInputBorder(), prefixIcon: const Icon(Icons.qr_code)),
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
// Trigger rebuild to update button state
|
||||
});
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
SizedBox(width: double.infinity, child: ElevatedButton(onPressed: _canFinish() ? _finishTask : null, style: ElevatedButton.styleFrom(padding: const EdgeInsets.symmetric(vertical: 16)), child: Text(AppLocalizations.of(context).finish))),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
679
app/lib/tasks/photo_capture_screen.dart
Normal file
679
app/lib/tasks/photo_capture_screen.dart
Normal file
@@ -0,0 +1,679 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:camera/camera.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:file_selector/file_selector.dart' as fsel;
|
||||
import 'package:votianlt_app/services/developer.dart' as developer;
|
||||
import '../l10n/app_localizations.dart';
|
||||
import '../models/tasks/photo_task.dart';
|
||||
import '../widgets/offline_banner.dart';
|
||||
|
||||
class PhotoCaptureScreen extends StatefulWidget {
|
||||
final PhotoTask task;
|
||||
final Function(List<Uint8List>) onPhotosCompleted;
|
||||
|
||||
const PhotoCaptureScreen({
|
||||
super.key,
|
||||
required this.task,
|
||||
required this.onPhotosCompleted,
|
||||
});
|
||||
|
||||
@override
|
||||
State<PhotoCaptureScreen> createState() => _PhotoCaptureScreenState();
|
||||
}
|
||||
|
||||
class _PhotoCaptureScreenState extends State<PhotoCaptureScreen> {
|
||||
CameraController? _cameraController; // Android/iOS/Web
|
||||
List<CameraDescription>? _cameras;
|
||||
final List<Uint8List> _capturedPhotos = [];
|
||||
final PageController _pageController = PageController();
|
||||
int _currentPhotoIndex = 0;
|
||||
bool _isCameraInitialized = false;
|
||||
bool _isCameraSupportedOnThisPlatform = false;
|
||||
bool _useFilePickerMode = false; // desktop fallback
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_detectPlatformSupportAndInit();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_cameraController?.dispose();
|
||||
_pageController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _detectPlatformSupportAndInit() {
|
||||
// Requirement: Desktop (macOS/Windows/Linux) uses file picker; Android/iOS uses camera.
|
||||
if (kIsWeb) {
|
||||
// Keep web behavior using camera if available
|
||||
_isCameraSupportedOnThisPlatform = true;
|
||||
_initializeCamera();
|
||||
return;
|
||||
}
|
||||
switch (defaultTargetPlatform) {
|
||||
case TargetPlatform.android:
|
||||
case TargetPlatform.iOS:
|
||||
_isCameraSupportedOnThisPlatform = true; // enable capture button
|
||||
_useFilePickerMode = false;
|
||||
_initializeCamera();
|
||||
return;
|
||||
case TargetPlatform.macOS:
|
||||
case TargetPlatform.windows:
|
||||
case TargetPlatform.linux:
|
||||
// Desktop → use file picker instead of camera
|
||||
_isCameraSupportedOnThisPlatform = true; // enable button
|
||||
_useFilePickerMode = true;
|
||||
return;
|
||||
default:
|
||||
_isCameraSupportedOnThisPlatform = false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _initializeCamera() async {
|
||||
try {
|
||||
// Android/iOS/Web camera initialization
|
||||
_cameras = await availableCameras();
|
||||
if (_cameras != null && _cameras!.isNotEmpty) {
|
||||
_cameraController = CameraController(
|
||||
_cameras![0],
|
||||
ResolutionPreset.medium,
|
||||
);
|
||||
await _cameraController!.initialize();
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isCameraInitialized = true;
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (e, stackTrace) {
|
||||
developer.log('Error initializing camera: $e', name: 'PhotoCaptureScreen');
|
||||
developer.log('Stack trace: $stackTrace', name: 'PhotoCaptureScreen');
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('${AppLocalizations.of(context).cameraError}: $e')),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _capturePhoto() async {
|
||||
try {
|
||||
// Camera plugin path (Android/iOS/Web)
|
||||
if (_cameraController != null && _isCameraInitialized) {
|
||||
final XFile photo = await _cameraController!.takePicture();
|
||||
final Uint8List photoBytes = await photo.readAsBytes();
|
||||
|
||||
setState(() {
|
||||
_capturedPhotos.add(photoBytes);
|
||||
});
|
||||
|
||||
// Navigate to the newly added photo (even if it's the first one). If the
|
||||
// PageView is not attached yet, _showPhotoAt will schedule a post-frame jump.
|
||||
_showPhotoAt(_capturedPhotos.length - 1);
|
||||
} else {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(AppLocalizations.of(context).cameraNotReady)),
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (e, stackTrace) {
|
||||
developer.log('Error capturing photo: $e', name: 'PhotoCaptureScreen');
|
||||
developer.log('Stack trace: $stackTrace', name: 'PhotoCaptureScreen');
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('${AppLocalizations.of(context).photoError}: $e')),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _pickPhotoFromFile() async {
|
||||
try {
|
||||
// Use file_selector for desktop and web for robust platform support
|
||||
final bool useFileSelector = kIsWeb ||
|
||||
defaultTargetPlatform == TargetPlatform.macOS ||
|
||||
defaultTargetPlatform == TargetPlatform.windows ||
|
||||
defaultTargetPlatform == TargetPlatform.linux;
|
||||
|
||||
if (useFileSelector) {
|
||||
final typeGroup = fsel.XTypeGroup(
|
||||
label: 'images',
|
||||
extensions: ['jpg', 'jpeg', 'png', 'heic', 'bmp', 'gif', 'webp'],
|
||||
);
|
||||
final fsel.XFile? picked = await fsel.openFile(acceptedTypeGroups: [typeGroup]);
|
||||
if (picked != null) {
|
||||
final data = await picked.readAsBytes();
|
||||
setState(() {
|
||||
_capturedPhotos.add(data);
|
||||
});
|
||||
|
||||
_showPhotoAt(_capturedPhotos.length - 1);
|
||||
}
|
||||
} else {
|
||||
// On Android/iOS, use file_picker which integrates with platform pickers
|
||||
final result = await FilePicker.platform.pickFiles(
|
||||
allowMultiple: false,
|
||||
type: FileType.image,
|
||||
withData: true,
|
||||
);
|
||||
if (result != null && result.files.isNotEmpty) {
|
||||
final file = result.files.first;
|
||||
final bytes = file.bytes;
|
||||
if (bytes != null) {
|
||||
setState(() {
|
||||
_capturedPhotos.add(bytes);
|
||||
});
|
||||
|
||||
_showPhotoAt(_capturedPhotos.length - 1);
|
||||
} else {
|
||||
// On some platforms, bytes may be null if withData was false; try path
|
||||
final path = file.path;
|
||||
if (path != null) {
|
||||
final data = await XFile(path).readAsBytes();
|
||||
setState(() {
|
||||
_capturedPhotos.add(data);
|
||||
});
|
||||
if (_capturedPhotos.length > 1) {
|
||||
_showPhotoAt(_capturedPhotos.length - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e, stackTrace) {
|
||||
developer.log('Error picking photo from file: $e', name: 'PhotoCaptureScreen');
|
||||
developer.log('Stack trace: $stackTrace', name: 'PhotoCaptureScreen');
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('${AppLocalizations.of(context).photoError}: $e')),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _deletePhoto(int index) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: Text(AppLocalizations.of(context).deletePhoto),
|
||||
content: Text(AppLocalizations.of(context).deletePhotoConfirm),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: Text(AppLocalizations.of(context).cancel),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
// Remove and navigate to a valid photo if any remain
|
||||
int targetIndex = _currentPhotoIndex;
|
||||
setState(() {
|
||||
_capturedPhotos.removeAt(index);
|
||||
if (_capturedPhotos.isEmpty) {
|
||||
_currentPhotoIndex = 0;
|
||||
} else {
|
||||
if (targetIndex >= _capturedPhotos.length) {
|
||||
targetIndex = _capturedPhotos.length - 1;
|
||||
}
|
||||
_currentPhotoIndex = targetIndex;
|
||||
}
|
||||
});
|
||||
if (_capturedPhotos.isNotEmpty) {
|
||||
_showPhotoAt(_currentPhotoIndex);
|
||||
}
|
||||
},
|
||||
style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
|
||||
child: Text(AppLocalizations.of(context).delete, style: const TextStyle(color: Colors.white)),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
bool get _canComplete {
|
||||
return _capturedPhotos.length >= widget.task.minPhotoCount &&
|
||||
_capturedPhotos.length <= widget.task.maxPhotoCount;
|
||||
}
|
||||
|
||||
bool get _canTakeMore {
|
||||
return _capturedPhotos.length < widget.task.maxPhotoCount;
|
||||
}
|
||||
|
||||
bool get _isDesktopPlatform {
|
||||
if (kIsWeb) return false;
|
||||
switch (defaultTargetPlatform) {
|
||||
case TargetPlatform.macOS:
|
||||
case TargetPlatform.windows:
|
||||
case TargetPlatform.linux:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void _showPhotoAt(int index) {
|
||||
if (_capturedPhotos.isEmpty) return;
|
||||
final int clamped = index.clamp(0, _capturedPhotos.length - 1);
|
||||
if (!mounted) return;
|
||||
|
||||
// Always schedule navigation after the current frame so that
|
||||
// PageView has rebuilt with the latest itemCount before we jump/animate.
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (!mounted) return;
|
||||
if (_pageController.hasClients) {
|
||||
try {
|
||||
_pageController.animateToPage(
|
||||
clamped,
|
||||
duration: const Duration(milliseconds: 250),
|
||||
curve: Curves.easeInOut,
|
||||
);
|
||||
} catch (e, stackTrace) {
|
||||
developer.log('Error animating to page: $e', name: 'PhotoCaptureScreen');
|
||||
developer.log('Stack trace: $stackTrace', name: 'PhotoCaptureScreen');
|
||||
_pageController.jumpToPage(clamped);
|
||||
}
|
||||
setState(() {
|
||||
_currentPhotoIndex = clamped;
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _goToPreviousPhoto() {
|
||||
if (_currentPhotoIndex > 0) {
|
||||
_showPhotoAt(_currentPhotoIndex - 1);
|
||||
}
|
||||
}
|
||||
|
||||
void _goToNextPhoto() {
|
||||
if (_currentPhotoIndex < _capturedPhotos.length - 1) {
|
||||
_showPhotoAt(_currentPhotoIndex + 1);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(AppLocalizations.of(context).photoCapture),
|
||||
backgroundColor: Colors.blue,
|
||||
foregroundColor: Colors.white,
|
||||
actions: [
|
||||
if (_canComplete)
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
widget.onPhotosCompleted(_capturedPhotos);
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
child: Text(
|
||||
AppLocalizations.of(context).finish,
|
||||
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
OfflineBanner(),
|
||||
// Task info header
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: EdgeInsets.all(16),
|
||||
color: Colors.grey[100],
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'${AppLocalizations.of(context).requiredPhotos}: ${widget.task.minPhotoCount}-${widget.task.maxPhotoCount}',
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||
),
|
||||
Text(
|
||||
'${AppLocalizations.of(context).photosTaken}: ${_capturedPhotos.length}',
|
||||
style: TextStyle(fontSize: 14, color: Colors.grey[600]),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// Camera preview, photo gallery or empty state
|
||||
Expanded(
|
||||
child: _capturedPhotos.isEmpty
|
||||
? _buildCameraOrEmptyState()
|
||||
: _buildPhotoGallery(),
|
||||
),
|
||||
|
||||
// Bottom controls
|
||||
Container(
|
||||
padding: EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.grey.withValues(alpha: 0.3),
|
||||
spreadRadius: 1,
|
||||
blurRadius: 5,
|
||||
offset: Offset(0, -3),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
// Camera or file select button
|
||||
Expanded(
|
||||
child: ElevatedButton.icon(
|
||||
onPressed: _canTakeMore && _isCameraSupportedOnThisPlatform
|
||||
? (_useFilePickerMode
|
||||
? _pickPhotoFromFile
|
||||
: (_isCameraInitialized ? _capturePhoto : null))
|
||||
: null,
|
||||
icon: Icon(_useFilePickerMode ? Icons.photo_library : Icons.camera_alt),
|
||||
label: Text(
|
||||
!_isCameraSupportedOnThisPlatform
|
||||
? AppLocalizations.of(context).cameraNotSupportedOnPlatform
|
||||
: (!_canTakeMore
|
||||
? AppLocalizations.of(context).maxPhotosReached
|
||||
: (_useFilePickerMode
|
||||
? AppLocalizations.of(context).selectPhoto
|
||||
: (_isCameraInitialized ? AppLocalizations.of(context).takePhoto : (defaultTargetPlatform == TargetPlatform.macOS ? AppLocalizations.of(context).cameraReadyNoPreview : AppLocalizations.of(context).cameraLoading)))),
|
||||
),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: _canTakeMore && (_useFilePickerMode || _isCameraInitialized) ? Colors.blue : Colors.grey,
|
||||
foregroundColor: Colors.white,
|
||||
padding: EdgeInsets.symmetric(vertical: 12),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
if (_capturedPhotos.isNotEmpty) ...[
|
||||
SizedBox(width: 16),
|
||||
// Delete current photo button
|
||||
ElevatedButton.icon(
|
||||
onPressed: () => _deletePhoto(_currentPhotoIndex),
|
||||
icon: Icon(Icons.delete),
|
||||
label: Text(AppLocalizations.of(context).delete),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.red,
|
||||
foregroundColor: Colors.white,
|
||||
padding: EdgeInsets.symmetric(vertical: 12, horizontal: 16),
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
SizedBox(height: 12),
|
||||
// Bottom 'Fertig' button placed under the row
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: ElevatedButton(
|
||||
onPressed: _canComplete
|
||||
? () {
|
||||
widget.onPhotosCompleted(_capturedPhotos);
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
: null,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: _canComplete ? Colors.green : Colors.grey,
|
||||
foregroundColor: Colors.white,
|
||||
padding: EdgeInsets.symmetric(vertical: 14),
|
||||
),
|
||||
child: Text(AppLocalizations.of(context).finish, style: const TextStyle(fontWeight: FontWeight.bold)),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildCameraOrEmptyState() {
|
||||
// If platform not supported, show informative message
|
||||
if (!_isCameraSupportedOnThisPlatform) {
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(24),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.desktop_windows, size: 80, color: Colors.grey[400]),
|
||||
SizedBox(height: 16),
|
||||
Text(
|
||||
AppLocalizations.of(context).cameraNotAvailable,
|
||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600, color: Colors.grey[700]),
|
||||
),
|
||||
SizedBox(height: 8),
|
||||
Text(
|
||||
AppLocalizations.of(context).cameraNotSupportedMessage,
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(fontSize: 14, color: Colors.grey[600]),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// macOS fallback: explain file selection
|
||||
if (_useFilePickerMode) {
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(24),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.photo_library, size: 80, color: Colors.grey[400]),
|
||||
SizedBox(height: 16),
|
||||
Text(
|
||||
AppLocalizations.of(context).addPhotos,
|
||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600, color: Colors.grey[700]),
|
||||
),
|
||||
SizedBox(height: 8),
|
||||
Text(
|
||||
AppLocalizations.of(context).addPhotosInstruction,
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(fontSize: 14, color: Colors.grey[600]),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Show camera preview if available on any platform
|
||||
if (_isCameraInitialized && _cameraController != null) {
|
||||
return Container(
|
||||
margin: EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.grey.withValues(alpha: 0.3),
|
||||
spreadRadius: 2,
|
||||
blurRadius: 8,
|
||||
offset: Offset(0, 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
child: CameraPreview(_cameraController!),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// When camera is not available, show empty state
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.camera_alt,
|
||||
size: 80,
|
||||
color: Colors.grey[400],
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
Text(
|
||||
AppLocalizations.of(context).cameraInitializing,
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
color: Colors.grey[600],
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
SizedBox(height: 8),
|
||||
Text(
|
||||
AppLocalizations.of(context).cameraLoadingMessage,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Colors.grey[500],
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPhotoGallery() {
|
||||
return Column(
|
||||
children: [
|
||||
// Photo counter (if more than one photo)
|
||||
if (_capturedPhotos.length > 1)
|
||||
Container(
|
||||
padding: EdgeInsets.symmetric(vertical: 8),
|
||||
child: Text(
|
||||
'${_currentPhotoIndex + 1} ${AppLocalizations.of(context).photoOf} ${_capturedPhotos.length}',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: Colors.grey[700],
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// Photo viewer with swipe gestures and navigation arrows
|
||||
Expanded(
|
||||
child: Stack(
|
||||
children: [
|
||||
PageView.builder(
|
||||
controller: _pageController,
|
||||
onPageChanged: (index) {
|
||||
setState(() {
|
||||
_currentPhotoIndex = index;
|
||||
});
|
||||
},
|
||||
itemCount: _capturedPhotos.length,
|
||||
itemBuilder: (context, index) {
|
||||
return Container(
|
||||
margin: EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.grey.withValues(alpha: 0.3),
|
||||
spreadRadius: 2,
|
||||
blurRadius: 8,
|
||||
offset: Offset(0, 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
child: Image.memory(
|
||||
_capturedPhotos[index],
|
||||
fit: BoxFit.contain,
|
||||
errorBuilder: (context, error, stackTrace) {
|
||||
return Container(
|
||||
color: Colors.grey[300],
|
||||
child: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.error, size: 50, color: Colors.grey[600]),
|
||||
SizedBox(height: 8),
|
||||
Text(AppLocalizations.of(context).photoError),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
if (_capturedPhotos.length > 1 && _isDesktopPlatform)
|
||||
Positioned(
|
||||
left: 8,
|
||||
top: 0,
|
||||
bottom: 0,
|
||||
child: Center(
|
||||
child: IconButton(
|
||||
onPressed: _currentPhotoIndex > 0
|
||||
? _goToPreviousPhoto
|
||||
: null,
|
||||
icon: Icon(Icons.chevron_left, size: 36),
|
||||
style: IconButton.styleFrom(
|
||||
backgroundColor: Colors.white.withValues(alpha: 0.7),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
if (_capturedPhotos.length > 1 && _isDesktopPlatform)
|
||||
Positioned(
|
||||
right: 8,
|
||||
top: 0,
|
||||
bottom: 0,
|
||||
child: Center(
|
||||
child: IconButton(
|
||||
onPressed: _currentPhotoIndex < _capturedPhotos.length - 1
|
||||
? _goToNextPhoto
|
||||
: null,
|
||||
icon: Icon(Icons.chevron_right, size: 36),
|
||||
style: IconButton.styleFrom(
|
||||
backgroundColor: Colors.white.withValues(alpha: 0.7),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// Photo indicators (if more than one photo)
|
||||
if (_capturedPhotos.length > 1)
|
||||
Container(
|
||||
padding: EdgeInsets.symmetric(vertical: 16),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: _capturedPhotos.asMap().entries.map((entry) {
|
||||
return Container(
|
||||
width: 8,
|
||||
height: 8,
|
||||
margin: EdgeInsets.symmetric(horizontal: 4),
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: _currentPhotoIndex == entry.key
|
||||
? Colors.blue
|
||||
: Colors.grey[400],
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
260
app/lib/tasks/signature_capture_screen.dart
Normal file
260
app/lib/tasks/signature_capture_screen.dart
Normal file
@@ -0,0 +1,260 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:signature/signature.dart';
|
||||
import '../l10n/app_localizations.dart';
|
||||
import '../models/tasks/signature_task.dart';
|
||||
import '../widgets/offline_banner.dart';
|
||||
|
||||
class SignatureCaptureScreen extends StatefulWidget {
|
||||
final SignatureTask task;
|
||||
final void Function(String svg) onSignatureCompleted;
|
||||
|
||||
const SignatureCaptureScreen({
|
||||
super.key,
|
||||
required this.task,
|
||||
required this.onSignatureCompleted,
|
||||
});
|
||||
|
||||
@override
|
||||
State<SignatureCaptureScreen> createState() => _SignatureCaptureScreenState();
|
||||
}
|
||||
|
||||
class _SignatureCaptureScreenState extends State<SignatureCaptureScreen> {
|
||||
late final SignatureController _controller;
|
||||
bool _hasSignature = false;
|
||||
bool _isMobilePlatform = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_controller = SignatureController(
|
||||
penStrokeWidth: 3,
|
||||
penColor: Colors.black,
|
||||
exportBackgroundColor: Colors.white,
|
||||
);
|
||||
|
||||
// Listen to signature controller changes
|
||||
_controller.addListener(_onSignatureChanged);
|
||||
|
||||
_detectPlatformAndSetOrientation();
|
||||
}
|
||||
|
||||
void _onSignatureChanged() {
|
||||
final bool hasPoints = _controller.points.isNotEmpty;
|
||||
if (hasPoints != _hasSignature) {
|
||||
setState(() {
|
||||
_hasSignature = hasPoints;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _detectPlatformAndSetOrientation() {
|
||||
// Check if we're on a mobile platform
|
||||
if (!kIsWeb) {
|
||||
switch (defaultTargetPlatform) {
|
||||
case TargetPlatform.android:
|
||||
case TargetPlatform.iOS:
|
||||
_isMobilePlatform = true;
|
||||
// Rotate screen 90 degrees to the right (landscape left)
|
||||
SystemChrome.setPreferredOrientations([
|
||||
DeviceOrientation.landscapeLeft,
|
||||
]);
|
||||
break;
|
||||
default:
|
||||
_isMobilePlatform = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _restoreOrientation() {
|
||||
// Restore original orientation when leaving the screen
|
||||
if (_isMobilePlatform) {
|
||||
SystemChrome.setPreferredOrientations([
|
||||
DeviceOrientation.portraitUp,
|
||||
DeviceOrientation.portraitDown,
|
||||
DeviceOrientation.landscapeLeft,
|
||||
DeviceOrientation.landscapeRight,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.removeListener(_onSignatureChanged);
|
||||
_controller.dispose();
|
||||
_restoreOrientation();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
String _buildSvgFromPoints(List<Point?> points, {double strokeWidth = 3.0, String strokeColor = '#000000'}) {
|
||||
// Convert collected signature points (with null separators for stroke breaks) into an SVG string
|
||||
// Determine bounds
|
||||
double? minX, minY, maxX, maxY;
|
||||
for (final p in points) {
|
||||
if (p == null) continue;
|
||||
final x = p.offset.dx;
|
||||
final y = p.offset.dy;
|
||||
if (minX == null || x < minX) minX = x;
|
||||
if (minY == null || y < minY) minY = y;
|
||||
if (maxX == null || x > maxX) maxX = x;
|
||||
if (maxY == null || y > maxY) maxY = y;
|
||||
}
|
||||
// Fallback bounds if empty or degenerate
|
||||
if (minX == null || minY == null || maxX == null || maxY == null) {
|
||||
minX = 0;
|
||||
minY = 0;
|
||||
maxX = 1;
|
||||
maxY = 1;
|
||||
}
|
||||
double width = (maxX - minX);
|
||||
double height = (maxY - minY);
|
||||
if (width <= 0) width = 1;
|
||||
if (height <= 0) height = 1;
|
||||
|
||||
final StringBuffer d = StringBuffer();
|
||||
bool newStroke = true;
|
||||
for (final p in points) {
|
||||
if (p == null) {
|
||||
newStroke = true;
|
||||
continue;
|
||||
}
|
||||
final x = (p.offset.dx - minX);
|
||||
final y = (p.offset.dy - minY);
|
||||
if (newStroke) {
|
||||
d.write('M${x.toStringAsFixed(2)} ${y.toStringAsFixed(2)} ');
|
||||
newStroke = false;
|
||||
} else {
|
||||
d.write('L${x.toStringAsFixed(2)} ${y.toStringAsFixed(2)} ');
|
||||
}
|
||||
}
|
||||
|
||||
final String svg = '<svg xmlns="http://www.w3.org/2000/svg" width="${width.toStringAsFixed(2)}" height="${height.toStringAsFixed(2)}" viewBox="0 0 ${width.toStringAsFixed(2)} ${height.toStringAsFixed(2)}"><path d="${d.toString().trim()}" fill="none" stroke="$strokeColor" stroke-width="$strokeWidth" stroke-linecap="round" stroke-linejoin="round"/></svg>';
|
||||
return svg;
|
||||
}
|
||||
|
||||
Future<void> _finish() async {
|
||||
try {
|
||||
// Ensure there is at least one non-null point in the signature
|
||||
final hasAnyPoint = _controller.points.isNotEmpty;
|
||||
if (!hasAnyPoint) {
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(AppLocalizations.of(context).signatureRequired)),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Build SVG from the captured signature points
|
||||
final String svg = _buildSvgFromPoints(_controller.points);
|
||||
|
||||
// Close this screen first to show the updated TaskView quickly
|
||||
if (!mounted) return;
|
||||
_restoreOrientation();
|
||||
Navigator.of(context).pop();
|
||||
|
||||
// Then notify the caller (SVG only)
|
||||
widget.onSignatureCompleted(svg);
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('${AppLocalizations.of(context).signatureError}: $e')),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(AppLocalizations.of(context).signatureCapture),
|
||||
backgroundColor: Colors.deepPurple[100],
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
onPressed: () {
|
||||
_restoreOrientation();
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
),
|
||||
actions: [
|
||||
IconButton(
|
||||
tooltip: AppLocalizations.of(context).delete,
|
||||
onPressed: () {
|
||||
_controller.clear();
|
||||
// The listener will automatically update _hasSignature when points are cleared
|
||||
},
|
||||
icon: const Icon(Icons.delete_outline),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
OfflineBanner(),
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
AppLocalizations.of(context).signatureInstruction,
|
||||
style: TextStyle(color: Colors.grey[700]),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Expanded(
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: Colors.grey[400]!),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withValues(alpha: 0.05),
|
||||
blurRadius: 6,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
child: Signature(
|
||||
controller: _controller,
|
||||
backgroundColor: Colors.white,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
children: [
|
||||
OutlinedButton.icon(
|
||||
onPressed: () {
|
||||
_controller.clear();
|
||||
// The listener will automatically update _hasSignature when points are cleared
|
||||
},
|
||||
icon: const Icon(Icons.refresh),
|
||||
label: Text(AppLocalizations.of(context).clear),
|
||||
),
|
||||
const Spacer(),
|
||||
SizedBox(
|
||||
width: 160,
|
||||
child: ElevatedButton(
|
||||
onPressed: _hasSignature ? _finish : null,
|
||||
style: ElevatedButton.styleFrom(
|
||||
padding: const EdgeInsets.symmetric(vertical: 14),
|
||||
),
|
||||
child: Text(AppLocalizations.of(context).finish),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user