182 lines
5.5 KiB
Dart
182 lines
5.5 KiB
Dart
import 'package:gap/gap.dart';
|
|
import 'package:flutter/material.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';
|
|
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:go_router/go_router.dart';
|
|
|
|
class AddGenericsPage extends StatefulWidget {
|
|
const AddGenericsPage({super.key});
|
|
|
|
@override
|
|
State<AddGenericsPage> createState() => _AddGenericsPageState();
|
|
}
|
|
|
|
class _AddGenericsPageState extends State<AddGenericsPage> {
|
|
final _refCategories = RefCategories();
|
|
final _refGenericNames = RefGenericNames();
|
|
final _nameController = TextEditingController();
|
|
final _formKey = GlobalKey<FormState>();
|
|
|
|
bool _isLoading = false;
|
|
|
|
late List _categoryList = [];
|
|
late String _selectedCategory = '';
|
|
late String _categoryUUID = '';
|
|
|
|
void _getList() async {
|
|
_categoryList = await _refCategories.getList();
|
|
|
|
if (_categoryList.isEmpty) {
|
|
if (mounted) {
|
|
showNotification(context, 'Error: No Categories Found', false);
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (mounted) {
|
|
context.push('/main');
|
|
}
|
|
});
|
|
}
|
|
} else {
|
|
setState(() => {});
|
|
}
|
|
}
|
|
|
|
void autoRun() async {
|
|
if (await InternetConnectionChecker.instance.hasConnection) {
|
|
_getList();
|
|
} else {
|
|
if (mounted) {
|
|
showNotification(context, 'Error: No Internet Connection', false);
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (mounted) {
|
|
context.push('/main');
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
void _updateCategory(dynamic category) {
|
|
_selectedCategory = category;
|
|
}
|
|
|
|
void saveGeneric() async {
|
|
setState(() => _isLoading = true);
|
|
|
|
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
|
|
void initState() {
|
|
autoRun();
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_nameController.dispose();
|
|
_categoryList = [];
|
|
_selectedCategory = '';
|
|
_categoryUUID = '';
|
|
_isLoading = false;
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: PageBackgroundWidget(
|
|
child: Center(
|
|
child: Column(
|
|
children: [
|
|
const Gap(96),
|
|
const TitleWidget(firstTextSize: 20, secondTextSize: 32),
|
|
const Gap(32),
|
|
const TextWidget(text: 'Add Generics'),
|
|
const Gap(16),
|
|
FormBorderWidget(
|
|
color: 'blue',
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
children: [
|
|
InputWidget(label: 'Name', controller: _nameController),
|
|
const Gap(16),
|
|
if (_categoryList.isEmpty)
|
|
Column(
|
|
children: [
|
|
const Gap(8),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
spacing: 16,
|
|
children: [
|
|
CircularProgressIndicator(color: Colors.white),
|
|
TextWidget(
|
|
text: 'Fetching Categories',
|
|
size: 16,
|
|
)
|
|
],
|
|
),
|
|
],
|
|
)
|
|
else
|
|
Column(
|
|
children: [
|
|
DropDownWidget(
|
|
label: 'Category',
|
|
list: _categoryList,
|
|
listTitle: 'category_name',
|
|
onChanged: _updateCategory),
|
|
const Gap(32),
|
|
if (_isLoading)
|
|
Center(child: CircularProgressIndicator(color: Colors.white))
|
|
else
|
|
ButtonWidget(text: 'Add', onPressed: saveGeneric)
|
|
],
|
|
),
|
|
],
|
|
)),
|
|
)
|
|
],
|
|
)),
|
|
),
|
|
);
|
|
}
|
|
}
|