Added ability to update categories
This commit is contained in:
parent
c9c3f69886
commit
70aa37f3f5
@ -4,6 +4,28 @@ import 'package:ltx_flutter/constants/constants.dart';
|
||||
import 'package:ltx_flutter/constants/colors.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class CategoryData {
|
||||
late String? color;
|
||||
late num? number;
|
||||
late num? parent;
|
||||
late String? name;
|
||||
late String? description;
|
||||
|
||||
CategoryData({
|
||||
this.color,
|
||||
this.number,
|
||||
this.parent,
|
||||
this.name,
|
||||
this.description,
|
||||
}) {
|
||||
color = color;
|
||||
number = number;
|
||||
parent = parent;
|
||||
name = name;
|
||||
description = description;
|
||||
}
|
||||
}
|
||||
|
||||
class Category {
|
||||
late final Color backgroundColor;
|
||||
late final Color foregroundColor;
|
||||
@ -11,8 +33,10 @@ class Category {
|
||||
late final num? parent;
|
||||
late final String name;
|
||||
late final String? description;
|
||||
late final String hexColor;
|
||||
|
||||
Category(Document doc) {
|
||||
hexColor = doc.data['color'];
|
||||
backgroundColor = _getBackgroundColor(doc.data['color']);
|
||||
foregroundColor = _getForegroundColor(doc.data['color']);
|
||||
number = doc.data['number'];
|
||||
@ -38,6 +62,16 @@ class Category {
|
||||
double leftPadding() {
|
||||
return hasParent() ? 50.0 : 20.0;
|
||||
}
|
||||
|
||||
CategoryData data() {
|
||||
return CategoryData(
|
||||
description: description,
|
||||
color: hexColor,
|
||||
name: name,
|
||||
number: number,
|
||||
parent: parent,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class CategoriesAPI extends ChangeNotifier {
|
||||
@ -72,6 +106,7 @@ class CategoriesAPI extends ChangeNotifier {
|
||||
databaseId: CATEGORIES_DB,
|
||||
collectionId: COLLECTION,
|
||||
queries: [
|
||||
Query.limit(100),
|
||||
Query.orderAsc("parent"),
|
||||
Query.orderAsc("number"),
|
||||
]);
|
||||
@ -106,6 +141,36 @@ class CategoriesAPI extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
Future<Document>? updateCategory({
|
||||
required String name,
|
||||
required double number,
|
||||
required String color,
|
||||
String? description,
|
||||
int? parentId,
|
||||
}) async {
|
||||
try {
|
||||
print("Updating '${"category-${number.toString()}"}'");
|
||||
var x = await databases.updateDocument(
|
||||
databaseId: CATEGORIES_DB,
|
||||
collectionId: COLLECTION,
|
||||
documentId: "category-${number.toString()}",
|
||||
data: {
|
||||
'name': name,
|
||||
'number': number,
|
||||
'color': color,
|
||||
'parent': parentId,
|
||||
'description': description,
|
||||
});
|
||||
print(x);
|
||||
return x;
|
||||
} catch (e) {
|
||||
print(e);
|
||||
throw "Didn't work";
|
||||
} finally {
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<dynamic> deleteCategory({required double number}) {
|
||||
return databases.deleteDocument(
|
||||
databaseId: CATEGORIES_DB,
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
import 'package:ltx_flutter/appwrite/categories_api.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:appwrite/models.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:flutter_form_builder/flutter_form_builder.dart';
|
||||
|
||||
@ -23,10 +22,10 @@ class _CategoriesPageState extends State<CategoriesPage> {
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () => Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => const NewCategoryPage()),
|
||||
).then((value) {
|
||||
setState(() {});
|
||||
}),
|
||||
MaterialPageRoute(
|
||||
builder: (context) => NewCategoryPage(category: CategoryData()),
|
||||
),
|
||||
),
|
||||
tooltip: "New Category",
|
||||
child: Icon(Icons.add),
|
||||
),
|
||||
@ -40,7 +39,8 @@ class _CategoriesPageState extends State<CategoriesPage> {
|
||||
return Column(
|
||||
children: [
|
||||
Divider(
|
||||
thickness: 1,
|
||||
height: 0,
|
||||
thickness: 2,
|
||||
),
|
||||
CategoryRow(category: category),
|
||||
],
|
||||
@ -65,30 +65,40 @@ class CategoryRow extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Row(
|
||||
return GestureDetector(
|
||||
onLongPress: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => NewCategoryPage(
|
||||
category: category.data(),
|
||||
)),
|
||||
);
|
||||
},
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
color: category.backgroundColor,
|
||||
child: SizedBox.square(
|
||||
dimension: 70,
|
||||
constraints: BoxConstraints(maxHeight: 80, maxWidth: 70),
|
||||
color: category?.backgroundColor,
|
||||
child: SizedBox.expand(
|
||||
child: Center(
|
||||
child: Text(
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: category.foregroundColor,
|
||||
color: category?.foregroundColor,
|
||||
fontSize: 18,
|
||||
),
|
||||
category.number.toString())),
|
||||
category!.number.toString())),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.only(left: category.leftPadding()),
|
||||
padding: EdgeInsets.only(left: category!.leftPadding()),
|
||||
child: Text(
|
||||
style: TextStyle(
|
||||
fontSize: 17,
|
||||
),
|
||||
category.name,
|
||||
category!.name,
|
||||
),
|
||||
),
|
||||
),
|
||||
@ -100,17 +110,20 @@ class CategoryRow extends StatelessWidget {
|
||||
fontSize: 13,
|
||||
color: Colors.white38,
|
||||
),
|
||||
"${category.description}",
|
||||
"${category?.description}",
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class NewCategoryPage extends StatefulWidget {
|
||||
const NewCategoryPage({super.key});
|
||||
const NewCategoryPage({Key? key, required this.category}) : super(key: key);
|
||||
|
||||
final CategoryData category;
|
||||
|
||||
@override
|
||||
State<NewCategoryPage> createState() => _NewCategoryPageState();
|
||||
@ -118,16 +131,22 @@ class NewCategoryPage extends StatefulWidget {
|
||||
|
||||
class _NewCategoryPageState extends State<NewCategoryPage> {
|
||||
String _name = "";
|
||||
bool _isNew = true;
|
||||
final _formKey = GlobalKey<FormBuilderState>();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_name = "New Category";
|
||||
_isNew = true;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (widget.category.number != null) {
|
||||
_isNew = false;
|
||||
}
|
||||
_name = _isNew ? "New Category" : "Edit Category";
|
||||
|
||||
final CategoriesAPI api = context.watch<CategoriesAPI>();
|
||||
saveCategory(form, context) {
|
||||
var name = form?.fields['categoryName'].value;
|
||||
@ -135,6 +154,8 @@ class _NewCategoryPageState extends State<NewCategoryPage> {
|
||||
var color = form?.fields['categoryColor'].value;
|
||||
var description = form?.fields['categoryDescription'].value;
|
||||
var parent = form?.fields['categoryParent'].value;
|
||||
|
||||
if (_isNew) {
|
||||
api.addCategory(
|
||||
name: name,
|
||||
number: double.parse(number),
|
||||
@ -142,13 +163,21 @@ class _NewCategoryPageState extends State<NewCategoryPage> {
|
||||
description: description,
|
||||
parentId: int.parse(parent),
|
||||
);
|
||||
} else {
|
||||
api.updateCategory(
|
||||
name: name,
|
||||
number: double.parse(number),
|
||||
color: color,
|
||||
description: description,
|
||||
parentId: int.parse(parent),
|
||||
);
|
||||
}
|
||||
Navigator.pop(context);
|
||||
api.getCategories();
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text("New Category"),
|
||||
title: Text(_name),
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.only(left: 50, right: 50),
|
||||
@ -165,6 +194,7 @@ class _NewCategoryPageState extends State<NewCategoryPage> {
|
||||
labelText: "Number",
|
||||
),
|
||||
name: 'categoryNumber',
|
||||
initialValue: widget.category.number.toString(),
|
||||
),
|
||||
SizedBox(height: 25),
|
||||
FormBuilderTextField(
|
||||
@ -172,15 +202,16 @@ class _NewCategoryPageState extends State<NewCategoryPage> {
|
||||
labelText: "Name",
|
||||
),
|
||||
name: 'categoryName',
|
||||
initialValue: widget.category.name,
|
||||
),
|
||||
SizedBox(height: 25),
|
||||
Consumer<CategoriesAPI>(
|
||||
builder: (context, categories, child) {
|
||||
print(categories.colors);
|
||||
return FormBuilderDropdown(
|
||||
decoration: InputDecoration(
|
||||
labelText: "Color",
|
||||
),
|
||||
initialValue: widget.category.color,
|
||||
items: [
|
||||
for (var c in categories.colors)
|
||||
DropdownMenuItem(
|
||||
@ -203,6 +234,7 @@ class _NewCategoryPageState extends State<NewCategoryPage> {
|
||||
SizedBox(height: 25),
|
||||
FormBuilderTextField(
|
||||
name: 'categoryDescription',
|
||||
initialValue: widget.category.description,
|
||||
decoration: InputDecoration(
|
||||
labelText: "Description",
|
||||
),
|
||||
@ -211,6 +243,7 @@ class _NewCategoryPageState extends State<NewCategoryPage> {
|
||||
FormBuilderTextField(
|
||||
keyboardType: TextInputType.number,
|
||||
name: 'categoryParent',
|
||||
initialValue: widget.category.parent.toString(),
|
||||
decoration: InputDecoration(
|
||||
labelText: "Parent",
|
||||
),
|
||||
|
||||
Loading…
Reference in New Issue
Block a user