import 'package:flutter/material.dart'; import 'package:gap/gap.dart'; import 'package:internet_connection_checker/internet_connection_checker.dart'; import 'package:pharmacy_mobile/blocs/caches/categorylist/functions/cache_setcategorylist.dart'; import 'package:pharmacy_mobile/functions/checkexisting_function.dart'; import 'package:pharmacy_mobile/tables/ref_categories.dart'; import 'package:pharmacy_mobile/widgets/buttonwithprogress_widget.dart'; import 'package:pharmacy_mobile/widgets/form_border_widget2.dart'; import 'package:pharmacy_mobile/widgets/input_form_widget.dart'; import 'package:pharmacy_mobile/widgets/page_background_widget.dart'; import 'package:pharmacy_mobile/widgets/snackbar_widget.dart'; import 'package:pharmacy_mobile/widgets/text_widget.dart'; import 'package:pharmacy_mobile/widgets/title_widget.dart'; import 'package:go_router/go_router.dart'; class AddCategoryPage extends StatefulWidget { const AddCategoryPage({super.key}); @override State createState() => _AddCategoryPageState(); } class _AddCategoryPageState extends State { final _formKey = GlobalKey(); final _categoryController = TextEditingController(); final _refCategories = RefCategories(); bool _isLoading = false; Future _getCategoryListCache() async { final categoryList = await _refCategories.getList(); if (categoryList.isNotEmpty) { // ignore: use_build_context_synchronously final setCache = await cacheSetCategoryList(context, categoryList); if (!setCache) {} } } void _saveCategory() async { setState(() => _isLoading = true); try { if (await InternetConnectionChecker.instance.hasConnection) { final existing = await checkExisting(_refCategories, _categoryController); if (existing && mounted) { showNotification(context, 'Category already listed', false); return; } final post = await _refCategories.postCategory(_categoryController.text.toUpperCase()); if (post && mounted) { _getCategoryListCache(); showNotification(context, 'Category saved', true); WidgetsBinding.instance.addPostFrameCallback((_) { if (mounted) { context.pop(); } }); } else { if (mounted) { showNotification(context, 'Category not saved', false); } } } else { if (mounted) { showNotification(context, 'Error: No Internet Connection', false); } } } catch (e) { if (mounted) { showNotification(context, 'Error: $e', false); } } finally { setState(() => _isLoading = false); } } @override Widget build(BuildContext context) { return Scaffold( body: PageBackgroundWidget( child: Center( child: Column( children: [ const Gap(96), const TitleWidget( firstTextSize: 14, secondTextSize: 24, logoSize: 90, ), const Gap(32), const TextWidget( text: 'Add Category', title: true, ), const Gap(16), FormBorderWidget2( color: 'blue', child: Form( key: _formKey, child: Center( child: Column( children: [ InputFormWidget(label: 'Category Name', controller: _categoryController), const Gap(32), // if (_isLoading) // const Center(child: CircularProgressIndicator(color: Colors.white)) // else // ButtonWidget(text: 'Save Category', onPressed: _saveCategory) ButtonWithProgressWidget( trigger: _isLoading, progressText: 'Adding Category', buttonText: 'Save', onPressed: _saveCategory) ], ), )), ) ], ), )), ); } }