This commit is contained in:
Patrick Alvin Alcala 2025-02-06 11:42:58 +08:00
parent 9598e17abb
commit 064814e165
3 changed files with 87 additions and 55 deletions

View file

@ -11,7 +11,6 @@ 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 {
@ -22,20 +21,59 @@ class AddGenericsPage extends StatefulWidget {
}
class _AddGenericsPageState extends State<AddGenericsPage> {
// final _formKey = GlobalKey<FormState>();
final _refCategories = RefCategories();
final _refGenericNames = RefGenericNames();
final _nameController = TextEditingController();
final _formKey = GlobalKey<FormState>();
bool _isVisible = false;
// bool _isVisible = false;
bool _isLoading = false;
late List _categoryList = [];
late String _selectedCategory = '';
late String _categoryUUID = '';
void autoRun() async {
void _getList() async {
// if (await InternetConnectionChecker.instance.hasConnection) {
_categoryList = await _refCategories.getList();
if (_categoryList.isEmpty) {
if (mounted) {
showNotification(context, 'Error: Empty', false);
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted) {
context.push('/main');
}
});
}
}
// } else {
// if (mounted) {
// showNotification(context, 'Error: No Internet Connection', false);
// WidgetsBinding.instance.addPostFrameCallback((_) {
// if (mounted) {
// context.push('/main');
// }
// });
// }
// }
}
void autoRun() async {
if (await InternetConnectionChecker.instance.hasConnection) {
_categoryList = await _refCategories.getList();
} else {
if (mounted) {
showNotification(context, 'Error: No Internet Connection', false);
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted) {
context.push('/main');
}
});
}
}
}
void _updateCategory(dynamic category) {
@ -76,7 +114,7 @@ class _AddGenericsPageState extends State<AddGenericsPage> {
@override
void initState() {
autoRun();
// autoRun();
super.initState();
}
@ -86,7 +124,6 @@ class _AddGenericsPageState extends State<AddGenericsPage> {
_categoryList = [];
_selectedCategory = '';
_categoryUUID = '';
_isVisible = false;
_isLoading = false;
super.dispose();
}
@ -94,49 +131,41 @@ class _AddGenericsPageState extends State<AddGenericsPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: VisibilityDetector(
key: Key('AddGenericsPage'),
onVisibilityChanged: (visibilityInfo) {
if (visibilityInfo.visibleFraction > 0.5 && !_isVisible) {
setState(() {
_isVisible = true;
autoRun();
});
}
},
child: 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),
DropDownWidget(
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),
GestureDetector(
onTap: _getList,
child: 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)
],
)),
)
],
)),
),
),
const Gap(32),
if (_isLoading)
Center(child: CircularProgressIndicator(color: Colors.white))
else
ButtonWidget(text: 'Add', onPressed: saveGeneric)
],
)),
)
],
)),
),
);
}