103 lines
3.8 KiB
Dart
103 lines
3.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:uuid/uuid.dart';
|
|
import 'house_note_model.dart';
|
|
import 'house_note_api.dart';
|
|
|
|
class HouseNoteForm extends StatefulWidget {
|
|
final House? house; // 若傳入則為編輯
|
|
const HouseNoteForm({this.house, super.key});
|
|
|
|
@override
|
|
State<HouseNoteForm> createState() => _HouseNoteFormState();
|
|
}
|
|
|
|
class _HouseNoteFormState extends State<HouseNoteForm> {
|
|
final _formKey = GlobalKey<FormState>();
|
|
final _api = HouseNoteApiService();
|
|
|
|
late String _title;
|
|
String? _address;
|
|
double? _totalPrice;
|
|
double? _totalPing;
|
|
String? _propertyType = '電梯大樓';
|
|
|
|
void _submit() async {
|
|
if (_formKey.currentState!.validate()) {
|
|
_formKey.currentState!.save();
|
|
|
|
final newHouse = House(
|
|
id: widget.house?.id ?? const Uuid().v4(), // 自動取得 UUID
|
|
title: _title,
|
|
address: _address,
|
|
totalPrice: _totalPrice,
|
|
totalPing: _totalPing,
|
|
propertyType: _propertyType,
|
|
);
|
|
|
|
await _api.createHouse(newHouse);
|
|
if (mounted) Navigator.pop(context, true);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: Text(widget.house == null ? '新增看屋筆記' : '編輯物件')),
|
|
body: Form(
|
|
key: _formKey,
|
|
child: ListView(
|
|
padding: const EdgeInsets.all(20),
|
|
children: [
|
|
TextFormField(
|
|
initialValue: widget.house?.title,
|
|
decoration: const InputDecoration(labelText: '物件名稱 *', hintText: '例:桃園藝文特區三房'),
|
|
validator: (v) => v!.isEmpty ? '請輸入名稱' : null,
|
|
onSaved: (v) => _title = v!,
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextFormField(
|
|
initialValue: widget.house?.address,
|
|
decoration: const InputDecoration(labelText: '地址', suffixIcon: Icon(Icons.my_location)),
|
|
onSaved: (v) => _address = v,
|
|
),
|
|
const SizedBox(height: 16),
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: TextFormField(
|
|
initialValue: widget.house?.totalPrice?.toString(),
|
|
decoration: const InputDecoration(labelText: '開價 (萬)', prefixIcon: Icon(Icons.monetization_on_outlined)),
|
|
keyboardType: TextInputType.number,
|
|
onSaved: (v) => _totalPrice = double.tryParse(v ?? ''),
|
|
),
|
|
),
|
|
const SizedBox(width: 16),
|
|
Expanded(
|
|
child: TextFormField(
|
|
initialValue: widget.house?.totalPing?.toString(),
|
|
decoration: const InputDecoration(labelText: '坪數', prefixIcon: Icon(Icons.square_foot)),
|
|
keyboardType: TextInputType.number,
|
|
onSaved: (v) => _totalPing = double.tryParse(v ?? ''),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 16),
|
|
DropdownButtonFormField<String>(
|
|
value: _propertyType,
|
|
items: ['電梯大樓', '公寓', '華廈', '透天'].map((e) => DropdownMenuItem(value: e, child: Text(e))).toList(),
|
|
onChanged: (v) => setState(() => _propertyType = v),
|
|
decoration: const InputDecoration(labelText: '建物型態'),
|
|
),
|
|
const SizedBox(height: 40),
|
|
ElevatedButton(
|
|
onPressed: _submit,
|
|
style: ElevatedButton.styleFrom(minimumSize: const Size(double.infinity, 54), shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10))),
|
|
child: const Text('儲存筆記', style: TextStyle(fontSize: 18)),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
} |