ltx-flutter/ltx_flutter/lib/pages/categories_page.dart
2023-05-23 17:59:16 -07:00

65 lines
1.7 KiB
Dart

import 'package:ltx_flutter/appwrite/categories_api.dart';
import 'package:flutter/material.dart';
import 'package:appwrite/models.dart';
class CategoriesPage extends StatefulWidget {
const CategoriesPage({Key? key}) : super(key: key);
@override
_CategoriesPageState createState() => _CategoriesPageState();
}
class _CategoriesPageState extends State<CategoriesPage> {
final api = CategoriesAPI();
late List<Document>? categories = [];
@override
void initState() {
super.initState();
loadCategories();
}
Future loadCategories() async {
try {
final value = await api.getCategories();
setState(() {
categories = value.documents;
});
} catch (e) {
print(e);
}
}
@override
Widget build(BuildContext context) {
return RefreshIndicator(
onRefresh: loadCategories,
child: Center(
child: ListView.builder(
itemCount: categories!.length + 1,
itemBuilder: (context, index) {
if (index >= categories!.length || categories!.isEmpty) {
return ListTile(
leading: Text("New Category"),
);
} else {
Document? category = categories?[index];
Color backgroundColor =
Color(int.parse("0x${category!.data['color']}"));
Color textColor = backgroundColor.computeLuminance() > 0.2
? Colors.black
: Colors.white;
return ListTile(
tileColor: backgroundColor,
textColor: textColor,
leading: Text(category.data['number'].toString()),
title: Text(category.data['name']),
);
}
},
),
),
);
}
}