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'; class CategoriesPage extends StatefulWidget { const CategoriesPage({Key? key}) : super(key: key); @override _CategoriesPageState createState() => _CategoriesPageState(); } class _CategoriesPageState extends State { @override void initState() { super.initState(); } @override Widget build(BuildContext context) { return Scaffold( floatingActionButton: FloatingActionButton( onPressed: () => Navigator.push( context, MaterialPageRoute(builder: (context) => const NewCategoryPage()), ).then((value) { setState(() {}); }), tooltip: "New Category", child: Icon(Icons.add), ), body: Consumer(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( thickness: 1, ), 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 Row( children: [ Container( color: category.backgroundColor, child: SizedBox.square( dimension: 70, 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({super.key}); @override State createState() => _NewCategoryPageState(); } class _NewCategoryPageState extends State { String _name = ""; final _formKey = GlobalKey(); @override void initState() { super.initState(); _name = "New Category"; } @override Widget build(BuildContext context) { final CategoriesAPI api = context.watch(); 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; api.addCategory( 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"), ), 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', ), SizedBox(height: 25), FormBuilderTextField( decoration: InputDecoration( labelText: "Name", ), name: 'categoryName', ), SizedBox(height: 25), Consumer( builder: (context, categories, child) { print(categories.colors); return FormBuilderDropdown( decoration: InputDecoration( labelText: "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', decoration: InputDecoration( labelText: "Description", ), ), SizedBox(height: 25), FormBuilderTextField( keyboardType: TextInputType.number, name: 'categoryParent', decoration: InputDecoration( labelText: "Parent", ), ), ], ), ), SizedBox(height: 40), OutlinedButton( onPressed: () => saveCategory(_formKey.currentState, context), child: Text("Save"), ), ], ), ), ); } }