tuuli_app/lib/pages/bottomsheets/create_table_item_bottomsheet.dart

198 lines
6.1 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_fast_forms/flutter_fast_forms.dart';
import 'package:get/get.dart';
import 'package:tuuli_app/api/api_client.dart';
import 'package:tuuli_app/api/model/table_field_model.dart';
import 'package:tuuli_app/api/model/tables_list_model.dart';
import 'package:uuid/uuid.dart';
import 'package:uuid/uuid_util.dart';
class CreateTableItemBottomSheet extends StatefulWidget {
final TableModel table;
final TableItemsData? existingItem;
const CreateTableItemBottomSheet({
super.key,
required this.table,
this.existingItem,
});
@override
State<StatefulWidget> createState() => _CreateTableItemBottomSheetState();
}
class _CreateTableItemBottomSheetState
extends State<CreateTableItemBottomSheet> {
final _formKey = GlobalKey<FormState>();
final _values = <String, dynamic>{};
@override
void initState() {
super.initState();
for (final field in widget.table.columns.where((e) => !e.isPrimary)) {
_values[field.fieldName] = null;
}
widget.existingItem?.forEach((key, value) {
_values[key] = value;
});
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(16),
child: FastForm(
formKey: _formKey,
children: [
Text(
widget.existingItem == null ? "Create new item" : "Update item",
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 16),
for (final field in widget.table.columns.where((e) => !e.isPrimary))
Card(
margin: const EdgeInsets.all(8),
child: Container(
padding: const EdgeInsets.all(8),
child: _createFormField(field),
),
),
const SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text("Cancel"),
),
ElevatedButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
Navigator.of(context).pop(_values);
return;
}
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Please fill in all fields"),
),
);
},
child:
Text(widget.existingItem == null ? "Create" : "Update"),
),
],
),
],
),
),
);
}
Widget _createFormField(TableField field) {
switch (field.fieldType) {
case "serial":
case "bigint":
// ignore: no_duplicate_case_values
case "int":
return FastTextField(
name: field.fieldName,
labelText: field.fieldName,
validator: (value) {
if (value == null ||
value.isEmpty ||
double.tryParse(value) is! double) {
return "Please enter a value";
}
return null;
},
initialValue: (_values[field.fieldName] ?? "").toString(),
onChanged: (value) {
_values[field.fieldName] = int.tryParse(value ?? "");
},
);
case "uuid":
return FastTextField(
name: field.fieldName,
labelText: field.fieldName,
validator: (value) {
if (value == null ||
value.isEmpty ||
!Uuid.isValidUUID(fromString: value)) {
return "Please enter a value";
}
return null;
},
initialValue: (_values[field.fieldName] ?? "").toString(),
onChanged: (value) {
_values[field.fieldName] =
Uuid.isValidUUID(fromString: value ?? "") ? value : null;
},
);
case "str":
return FastTextField(
name: field.fieldName,
labelText: field.fieldName,
validator: (value) {
if (value == null || value.isEmpty) {
return "Please enter a value";
}
return null;
},
initialValue: _values[field.fieldName],
onChanged: (value) {
_values[field.fieldName] = value;
},
);
case "bool":
return FastCheckbox(
name: field.fieldName,
labelText: field.fieldName,
titleText: field.fieldName,
initialValue: _values[field.fieldName],
onChanged: (value) {
_values[field.fieldName] = value;
},
);
case "date":
// ignore: no_duplicate_case_values
case "datetime":
return FastCalendar(
name: field.fieldName,
firstDate: DateTime.now().subtract(const Duration(days: 365 * 200)),
lastDate: DateTime.now().add(const Duration(days: 365 * 200)),
initialValue: DateTime.tryParse(_values[field.fieldName] ?? ""),
validator: (value) {
if (value == null) {
return "Please enter a value";
}
return null;
},
onChanged: (value) {
_values[field.fieldName] = value;
},
);
case "float":
return FastTextField(
name: field.fieldName,
labelText: field.fieldName,
validator: (value) {
if (value == null ||
value.isEmpty ||
double.tryParse(value) is! double) {
return "Please enter a value";
}
return null;
},
initialValue: (_values[field.fieldName] ?? "").toString(),
onChanged: (value) {
_values[field.fieldName] = double.tryParse(value ?? "");
},
);
default:
return const Text("Unknown field type");
}
}
}