Table dropping implemented
This commit is contained in:
parent
529ec239d1
commit
d973954a75
4 changed files with 114 additions and 4 deletions
242
lib/pages/bottomsheets/edit_table_bottomsheet.dart
Normal file
242
lib/pages/bottomsheets/edit_table_bottomsheet.dart
Normal file
|
|
@ -0,0 +1,242 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:recase/recase.dart';
|
||||
import 'package:tuuli_app/api/model/table_field_model.dart';
|
||||
import 'package:tuuli_app/api/model/tables_list_model.dart';
|
||||
import 'package:tuuli_app/widgets/table_field_widget.dart';
|
||||
|
||||
class EditTableBottomSheetResult {
|
||||
final String tableName;
|
||||
final List<TableField> fields;
|
||||
|
||||
EditTableBottomSheetResult(this.tableName, this.fields);
|
||||
}
|
||||
|
||||
class EditTableBottomSheet extends StatefulWidget {
|
||||
final TableModel? table;
|
||||
|
||||
const EditTableBottomSheet({super.key, this.table});
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _EditTableBottomSheetState();
|
||||
}
|
||||
|
||||
class _EditTableBottomSheetState extends State<EditTableBottomSheet> {
|
||||
var tableName = "".obs;
|
||||
|
||||
late final List<TableField> fields;
|
||||
|
||||
final newFieldName = TextEditingController();
|
||||
String? newFieldType;
|
||||
var newFieldPrimary = false;
|
||||
var newFieldUnique = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
fields = widget.table?.columns ?? [];
|
||||
if (widget.table != null) {
|
||||
tableName.value = widget.table!.tableName;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SingleChildScrollView(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Obx(
|
||||
() => Text(
|
||||
tableName.isEmpty
|
||||
? "Edit table"
|
||||
: "Edit table \"${tableName.value.pascalCase}\"",
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
IconButton(
|
||||
onPressed: Get.back,
|
||||
icon: const Icon(Icons.cancel),
|
||||
)
|
||||
],
|
||||
),
|
||||
if (widget.table == null) const Divider(),
|
||||
if (widget.table == null)
|
||||
TextFormField(
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Table name',
|
||||
hintText: 'Enter table name',
|
||||
),
|
||||
readOnly: widget.table != null,
|
||||
maxLength: 15,
|
||||
onChanged: (value) => tableName.value = value,
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter table name';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const Divider(),
|
||||
Text(
|
||||
"Fields",
|
||||
style: Theme.of(context).textTheme.titleLarge,
|
||||
),
|
||||
if (fields.isEmpty) const Text("No fields"),
|
||||
...fields
|
||||
.map((e) => TableFieldWidget(
|
||||
field: e,
|
||||
onRemove: () => _removeColumn(e.fieldName),
|
||||
))
|
||||
.toList(growable: false),
|
||||
if (widget.table == null) const SizedBox(width: 16),
|
||||
if (widget.table == null)
|
||||
Card(
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
margin: const EdgeInsets.all(8),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text("Add new field"),
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
children: [
|
||||
Flexible(
|
||||
child: TextFormField(
|
||||
controller: newFieldName,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Column name',
|
||||
hintText: 'Enter column name',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Flexible(
|
||||
child: DropdownButtonFormField(
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Column type',
|
||||
hintText: 'Choose column type',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
items: possibleFieldTypes.keys
|
||||
.map((e) => DropdownMenuItem(
|
||||
value: e,
|
||||
child: Text(e.pascalCase),
|
||||
))
|
||||
.toList(growable: false),
|
||||
value: newFieldType,
|
||||
onChanged: (value) => newFieldType = value,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
ToggleButtons(
|
||||
isSelected: [
|
||||
newFieldPrimary,
|
||||
newFieldUnique,
|
||||
!newFieldPrimary && !newFieldUnique
|
||||
],
|
||||
onPressed: (index) {
|
||||
setState(() {
|
||||
newFieldPrimary = index == 0;
|
||||
newFieldUnique = index == 1;
|
||||
});
|
||||
},
|
||||
children: const [
|
||||
Text("Primary"),
|
||||
Text("Unique"),
|
||||
Text("Normal"),
|
||||
],
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
ElevatedButton(
|
||||
onPressed: _addNewField,
|
||||
child: const Text("Add field"),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 32),
|
||||
ElevatedButton(
|
||||
onPressed: _saveTable,
|
||||
child: const Text("Save table"),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _addNewField() {
|
||||
if (newFieldType == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final fieldName = newFieldName.text;
|
||||
if (fieldName.isEmpty ||
|
||||
fields.any((element) => element.fieldName == fieldName)) {
|
||||
Get.defaultDialog(
|
||||
title: "Error",
|
||||
middleText: "Field name is empty or already exists",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
final field = TableField.parseTableField(
|
||||
"$fieldName:$newFieldType${newFieldUnique ? ":unique" : ""}${newFieldPrimary ? ":primary" : ""}",
|
||||
);
|
||||
|
||||
if (field.isPrimary && !field.canBePrimary()) {
|
||||
Get.defaultDialog(
|
||||
title: "Error",
|
||||
middleText: "Field type \"${field.fieldType}\" can't be primary",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
newFieldName.clear();
|
||||
newFieldType = null;
|
||||
newFieldPrimary = false;
|
||||
newFieldUnique = false;
|
||||
fields.add(field);
|
||||
});
|
||||
}
|
||||
|
||||
void _saveTable() {
|
||||
if (tableName.isEmpty) {
|
||||
Get.defaultDialog(
|
||||
title: "Error",
|
||||
middleText: "Table name is empty",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (fields.isEmpty) {
|
||||
Get.defaultDialog(
|
||||
title: "Error",
|
||||
middleText: "Table must have at least one field",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
Get.back(result: EditTableBottomSheetResult(tableName.value, fields));
|
||||
}
|
||||
|
||||
void _removeColumn(String name) {
|
||||
setState(() {
|
||||
fields.removeWhere((element) => element.fieldName == name);
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue