ltx-flutter/ltx_flutter/lib/pages/categories_page.dart

265 lines
7.9 KiB
Dart

import 'package:ltx_flutter/appwrite/categories_api.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
class CategoriesPage extends StatefulWidget {
const CategoriesPage({Key? key}) : super(key: key);
@override
_CategoriesPageState createState() => _CategoriesPageState();
}
class _CategoriesPageState extends State<CategoriesPage> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => NewCategoryPage(category: CategoryData()),
),
),
tooltip: "New Category",
child: Icon(Icons.add),
),
body: Consumer<CategoriesAPI>(builder: (context, categories, child) {
return ListView.builder(
itemCount: categories.total + 1,
itemBuilder: (context, i) {
if (i < categories.total) {
Category category = categories.get(i);
if (!category.hasParent() && category.number != 0) {
return Column(
children: [
Divider(
height: 0,
thickness: 2,
),
CategoryRow(category: category),
],
);
}
return CategoryRow(category: category);
}
},
);
}),
);
}
}
class CategoryRow extends StatelessWidget {
const CategoryRow({
super.key,
required this.category,
});
final Category category;
@override
Widget build(BuildContext context) {
return GestureDetector(
onLongPress: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => NewCategoryPage(
category: category.data(),
)),
);
},
child: Row(
children: [
Container(
constraints: BoxConstraints(maxHeight: 80, maxWidth: 70),
color: category?.backgroundColor,
child: SizedBox.expand(
child: Center(
child: Text(
style: TextStyle(
fontWeight: FontWeight.bold,
color: category?.foregroundColor,
fontSize: 18,
),
category!.number.toString())),
),
),
Expanded(
child: Padding(
padding: EdgeInsets.only(left: category!.leftPadding()),
child: Text(
style: TextStyle(
fontSize: 17,
),
category!.name,
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Text(
style: TextStyle(
fontSize: 13,
color: Colors.white38,
),
"${category?.description}",
),
),
),
],
),
);
}
}
class NewCategoryPage extends StatefulWidget {
const NewCategoryPage({Key? key, required this.category}) : super(key: key);
final CategoryData category;
@override
State<NewCategoryPage> createState() => _NewCategoryPageState();
}
class _NewCategoryPageState extends State<NewCategoryPage> {
String _name = "";
bool _isNew = true;
final _formKey = GlobalKey<FormBuilderState>();
@override
void initState() {
super.initState();
_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;
var number = form?.fields['categoryNumber'].value;
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),
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);
}
return Scaffold(
appBar: AppBar(
title: Text(_name),
),
body: Padding(
padding: const EdgeInsets.only(left: 50, right: 50),
child: Column(
children: [
FormBuilder(
key: _formKey,
child: Column(
children: [
SizedBox(height: 25),
FormBuilderTextField(
keyboardType: TextInputType.number,
decoration: InputDecoration(
labelText: "Number",
),
name: 'categoryNumber',
initialValue: widget.category.number.toString(),
),
SizedBox(height: 25),
FormBuilderTextField(
decoration: InputDecoration(
labelText: "Name",
),
name: 'categoryName',
initialValue: widget.category.name,
),
SizedBox(height: 25),
Consumer<CategoriesAPI>(
builder: (context, categories, child) {
return FormBuilderDropdown(
decoration: InputDecoration(
labelText: "Color",
),
initialValue: widget.category.color,
items: [
for (var c in categories.colors)
DropdownMenuItem(
value: c.hex,
child: Container(
decoration:
BoxDecoration(color: c.backgroundColor()),
padding: EdgeInsets.all(20),
child: Text(
c.name(),
style: TextStyle(color: c.foregroundColor()),
),
),
)
],
name: 'categoryColor',
);
},
),
SizedBox(height: 25),
FormBuilderTextField(
name: 'categoryDescription',
initialValue: widget.category.description,
decoration: InputDecoration(
labelText: "Description",
),
),
SizedBox(height: 25),
FormBuilderTextField(
keyboardType: TextInputType.number,
name: 'categoryParent',
initialValue: widget.category.parent.toString(),
decoration: InputDecoration(
labelText: "Parent",
),
),
],
),
),
SizedBox(height: 40),
OutlinedButton(
onPressed: () => saveCategory(_formKey.currentState, context),
child: Text("Save"),
),
],
),
),
);
}
}