173 lines
5.2 KiB
Dart
173 lines
5.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
import './house_note_model.dart';
|
|
import './house_note_api.dart';
|
|
// import 'package:uuid/uuid.dart';
|
|
|
|
class HouseEvaluationForm extends StatefulWidget {
|
|
final String houseId;
|
|
final String houseTitle;
|
|
|
|
const HouseEvaluationForm({required this.houseId, required this.houseTitle, super.key});
|
|
|
|
@override
|
|
State<HouseEvaluationForm> createState() => _HouseEvaluationFormState();
|
|
}
|
|
|
|
class _HouseEvaluationFormState extends State<HouseEvaluationForm> {
|
|
final _api = HouseNoteApiService();
|
|
bool _isLoading = true;
|
|
|
|
List<CheckCategory> _categories = [];
|
|
List<CheckItem> _allItems = [];
|
|
final Map<String, HouseEvaluation> _evalMap = {}; // Key: checkItemId
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadData();
|
|
}
|
|
|
|
Future<void> _loadData() async {
|
|
try {
|
|
final results = await Future.wait([
|
|
_api.fetchCategories(),
|
|
_api.fetchAllCheckItems(),
|
|
_api.fetchEvaluations(widget.houseId),
|
|
]);
|
|
|
|
if (mounted) {
|
|
setState(() {
|
|
_categories = results[0] as List<CheckCategory>;
|
|
_allItems = results[1] as List<CheckItem>;
|
|
|
|
// 將已有的評估紀錄放入 Map
|
|
final existingEvals = results[2] as List<HouseEvaluation>;
|
|
for (var e in existingEvals) {
|
|
_evalMap[e.checkItemId] = e;
|
|
}
|
|
|
|
_isLoading = false;
|
|
});
|
|
}
|
|
} catch (e) {
|
|
debugPrint("載入失敗: $e");
|
|
// 【關鍵修正】避免發生錯誤時畫面永遠轉圈圈(無反應)
|
|
if (mounted) {
|
|
setState(() => _isLoading = false);
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('資料載入失敗,請檢查網路或 API: $e')),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> _saveAll() async {
|
|
setState(() => _isLoading = true);
|
|
|
|
// 只儲存有打分數 (score > 0) 的項目
|
|
final toSave = _evalMap.values.where((e) => e.score > 0).toList();
|
|
|
|
for (var eval in toSave) {
|
|
await _api.saveSingleEvaluation(eval);
|
|
}
|
|
|
|
if (mounted) Navigator.pop(context, true);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text('${widget.houseTitle} - 賞屋評估'),
|
|
actions: [
|
|
IconButton(icon: const Icon(Icons.check), onPressed: _isLoading ? null : _saveAll),
|
|
],
|
|
),
|
|
body: _isLoading
|
|
? const Center(child: CircularProgressIndicator())
|
|
: ListView.builder(
|
|
itemCount: _categories.length,
|
|
itemBuilder: (context, catIdx) {
|
|
final cat = _categories[catIdx];
|
|
final items = _allItems.where((i) => i.categoryId == cat.id).toList();
|
|
|
|
return ExpansionTile(
|
|
initiallyExpanded: catIdx == 0,
|
|
title: Text(cat.name, style: const TextStyle(fontWeight: FontWeight.bold, color: Colors.blue)),
|
|
children: items.map((item) => _buildItemRow(item)).toList(),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildItemRow(CheckItem item) {
|
|
// 取得或初始化該項目的評估狀態
|
|
final eval = _evalMap.putIfAbsent(
|
|
item.id,
|
|
() => HouseEvaluation(
|
|
houseId: widget.houseId,
|
|
checkItemId: item.id,
|
|
itemName: item.name, // 【關鍵修正】把項目的中文名稱也傳進去
|
|
)
|
|
);
|
|
|
|
return Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(item.name, style: const TextStyle(fontSize: 15)),
|
|
const SizedBox(height: 4),
|
|
Row(
|
|
children: [
|
|
// 1-5 分評分 (星星)
|
|
...List.generate(5, (index) {
|
|
int starValue = index + 1;
|
|
return IconButton(
|
|
icon: Icon(
|
|
starValue <= eval.score ? Icons.star : Icons.star_border,
|
|
color: Colors.orange,
|
|
),
|
|
onPressed: () => setState(() => eval.score = starValue),
|
|
);
|
|
}),
|
|
const Spacer(),
|
|
// 備註按鈕
|
|
IconButton(
|
|
icon: Icon(Icons.note_alt_outlined, color: (eval.itemMemo?.isNotEmpty ?? false) ? Colors.blue : Colors.grey),
|
|
onPressed: () => _editMemo(eval),
|
|
),
|
|
],
|
|
),
|
|
const Divider(),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
void _editMemo(HouseEvaluation eval) {
|
|
final controller = TextEditingController(text: eval.itemMemo);
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text('項目備註'),
|
|
content: TextField(
|
|
controller: controller,
|
|
maxLines: 3,
|
|
decoration: const InputDecoration(hintText: '輸入屋況細節 (如:疑似壁癌...)'),
|
|
),
|
|
actions: [
|
|
TextButton(onPressed: () => Navigator.pop(context), child: const Text('取消')),
|
|
TextButton(
|
|
onPressed: () {
|
|
setState(() => eval.itemMemo = controller.text);
|
|
Navigator.pop(context);
|
|
},
|
|
child: const Text('確定')
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
} |