103 lines
3.2 KiB
Dart
103 lines
3.2 KiB
Dart
import 'dart:io';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
import './house_note_api.dart';
|
|
|
|
class HousePhotoForm extends StatefulWidget {
|
|
final String houseId;
|
|
final String houseTitle;
|
|
|
|
const HousePhotoForm({required this.houseId, required this.houseTitle, super.key});
|
|
|
|
@override
|
|
State<HousePhotoForm> createState() => _HousePhotoFormState();
|
|
}
|
|
|
|
class _HousePhotoFormState extends State<HousePhotoForm> {
|
|
final _api = HouseNoteApiService();
|
|
final _noteController = TextEditingController();
|
|
File? _image;
|
|
bool _isSubmitting = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_takePhoto(); // 進入頁面直接開啟相機
|
|
}
|
|
|
|
Future<void> _takePhoto() async {
|
|
final picker = ImagePicker();
|
|
final pickedFile = await picker.pickImage(source: ImageSource.camera, imageQuality: 70);
|
|
if (pickedFile != null) {
|
|
setState(() => _image = File(pickedFile.path));
|
|
}
|
|
}
|
|
|
|
void _submit() async {
|
|
if (_image == null) return;
|
|
|
|
setState(() => _isSubmitting = true);
|
|
try {
|
|
// 1. 上傳檔案
|
|
String? fileName = await _api.uploadImage(_image!);
|
|
if (fileName != null) {
|
|
// 2. 寫入資料庫
|
|
await _api.saveHousePhoto(widget.houseId, fileName, _noteController.text);
|
|
if (mounted) Navigator.pop(context, true);
|
|
}
|
|
} catch (e) {
|
|
debugPrint("提交失敗: $e");
|
|
} finally {
|
|
if (mounted) setState(() => _isSubmitting = false);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: Text('${widget.houseTitle} - 拍照紀錄')),
|
|
body: _isSubmitting
|
|
? const Center(child: CircularProgressIndicator())
|
|
: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(20),
|
|
child: Column(
|
|
children: [
|
|
GestureDetector(
|
|
onTap: _takePhoto,
|
|
child: Container(
|
|
height: 300,
|
|
width: double.infinity,
|
|
decoration: BoxDecoration(
|
|
color: Colors.grey[200],
|
|
borderRadius: BorderRadius.circular(12),
|
|
border: Border.all(color: Colors.grey.shade300),
|
|
),
|
|
child: _image == null
|
|
? const Center(child: Icon(Icons.add_a_photo, size: 50, color: Colors.grey))
|
|
: ClipRRect(
|
|
borderRadius: BorderRadius.circular(12),
|
|
child: Image.file(_image!, fit: BoxFit.cover),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 20),
|
|
TextField(
|
|
controller: _noteController,
|
|
decoration: const InputDecoration(
|
|
labelText: '照片註記 (例如:客廳採光、疑似壁癌)',
|
|
border: OutlineInputBorder(),
|
|
),
|
|
maxLines: 3,
|
|
),
|
|
const SizedBox(height: 30),
|
|
ElevatedButton(
|
|
onPressed: _image == null ? null : _submit,
|
|
style: ElevatedButton.styleFrom(minimumSize: const Size(double.infinity, 50)),
|
|
child: const Text('上傳照片紀錄'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
} |