Items workflow done
This commit is contained in:
parent
d973954a75
commit
6ba57612fa
5 changed files with 565 additions and 5 deletions
198
lib/pages/bottomsheets/create_table_item_bottomsheet.dart
Normal file
198
lib/pages/bottomsheets/create_table_item_bottomsheet.dart
Normal file
|
|
@ -0,0 +1,198 @@
|
|||
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(
|
||||
"Create new 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue