This commit is contained in:
Patrick Alvin Alcala 2025-02-05 17:06:13 +08:00
parent 87d8bb483e
commit 17a1430ef0
11 changed files with 271 additions and 25 deletions

View file

@ -1,6 +1,6 @@
import 'package:gap/gap.dart';
import 'package:flutter/material.dart';
// import 'package:pharmacy_mobile/auth/auth_service.dart';
import 'package:internet_connection_checker/internet_connection_checker.dart';
import 'package:pharmacy_mobile/tables/ref_categories.dart';
import 'package:pharmacy_mobile/tables/ref_generic_names.dart';
import 'package:pharmacy_mobile/widgets/button_widget.dart';
@ -8,9 +8,11 @@ import 'package:pharmacy_mobile/widgets/dropdown_widget.dart';
import 'package:pharmacy_mobile/widgets/form_border_widget.dart';
import 'package:pharmacy_mobile/widgets/input_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:visibility_detector/visibility_detector.dart';
import 'package:go_router/go_router.dart';
class AddGenericsPage extends StatefulWidget {
const AddGenericsPage({super.key});
@ -26,6 +28,7 @@ class _AddGenericsPageState extends State<AddGenericsPage> {
final _nameController = TextEditingController();
final _formKey = GlobalKey<FormState>();
bool _isVisible = false;
bool _isLoading = false;
late List _categoryList = [];
late String _selectedCategory = '';
@ -40,9 +43,35 @@ class _AddGenericsPageState extends State<AddGenericsPage> {
}
void saveGeneric() async {
_categoryUUID = await _refCategories.getUUID(_selectedCategory);
setState(() => _isLoading = true);
await _refGenericNames.postGeneric(_nameController.text, _categoryUUID);
try {
if (await InternetConnectionChecker.instance.hasConnection) {
_categoryUUID = await _refCategories.getUUID(_selectedCategory);
await _refGenericNames.postGeneric(_nameController.text, _categoryUUID);
if (mounted) {
showNotification(context, 'Generic Name Saved', true);
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted) {
context.push('/main');
}
});
}
} else {
if (mounted) {
showNotification(context, 'Error: No Internet Connection', false);
}
}
} catch (e) {
if (mounted) {
showNotification(context, 'Error: $e', false);
}
} finally {
setState(() => _isLoading = false);
}
}
@override
@ -58,6 +87,7 @@ class _AddGenericsPageState extends State<AddGenericsPage> {
_selectedCategory = '';
_categoryUUID = '';
_isVisible = false;
_isLoading = false;
super.dispose();
}
@ -96,8 +126,11 @@ class _AddGenericsPageState extends State<AddGenericsPage> {
list: _categoryList,
listTitle: 'category_name',
onChanged: _updateCategory),
const Gap(16),
ButtonWidget(text: 'Add', onPressed: saveGeneric)
const Gap(32),
if (_isLoading)
Center(child: CircularProgressIndicator(color: Colors.white))
else
ButtonWidget(text: 'Add', onPressed: saveGeneric)
],
)),
)