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