131 lines
4.4 KiB
Dart
131 lines
4.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:gap/gap.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:internet_connection_checker/internet_connection_checker.dart';
|
|
import 'package:pharmacy_mobile/blocs/caches/manufacturerlist/functions/cache_setmanufacturerlist.dart';
|
|
import 'package:pharmacy_mobile/functions/checkexisting_function.dart';
|
|
import 'package:pharmacy_mobile/tables/ref_manufacturers.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';
|
|
|
|
class AddManufacturerPage extends StatefulWidget {
|
|
const AddManufacturerPage({super.key});
|
|
|
|
@override
|
|
State<AddManufacturerPage> createState() => _AddManufacturerPageState();
|
|
}
|
|
|
|
class _AddManufacturerPageState extends State<AddManufacturerPage> {
|
|
final _formKey = GlobalKey<FormState>();
|
|
final _nameController = TextEditingController();
|
|
final _addressController = TextEditingController();
|
|
final _refManufacturers = RefManufacturers();
|
|
|
|
late bool _isLoading = false;
|
|
|
|
Future<void> _getManufacturerListCache() async {
|
|
final manufacturerList = await _refManufacturers.getList();
|
|
|
|
if (manufacturerList.isNotEmpty) {
|
|
// ignore: use_build_context_synchronously
|
|
final setCache = await cacheSetManufacturerList(context, manufacturerList);
|
|
if (!setCache) {}
|
|
}
|
|
}
|
|
|
|
void _saveManufacturer() async {
|
|
setState(() => _isLoading = true);
|
|
|
|
try {
|
|
if (await InternetConnectionChecker.instance.hasConnection) {
|
|
final existing = await checkExisting(_refManufacturers, _nameController);
|
|
|
|
if (existing && mounted) {
|
|
showNotification(context, 'Manufacturer already listed', false);
|
|
return;
|
|
}
|
|
|
|
final post = await _refManufacturers.postManufacturer(_nameController.text, _addressController.text);
|
|
|
|
if (post && mounted) {
|
|
_getManufacturerListCache();
|
|
showNotification(context, 'Manufacturer added to list', true);
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (mounted) {
|
|
context.pop();
|
|
}
|
|
});
|
|
}
|
|
} 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 dispose() {
|
|
_nameController.dispose();
|
|
_addressController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@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 Manufacturer',
|
|
title: true,
|
|
),
|
|
const Gap(16),
|
|
FormBorderWidget2(
|
|
color: 'blue',
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
children: [
|
|
InputFormWidget(label: 'Name', controller: _nameController),
|
|
const Gap(16),
|
|
InputFormWidget(label: 'Address', controller: _addressController),
|
|
const Gap(32),
|
|
// if (_isLoading)
|
|
// const Center(child: CircularProgressIndicator(color: Colors.white))
|
|
// else
|
|
// ButtonWidget(text: 'Save Manufacturer', onPressed: _saveManufacturer)
|
|
ButtonWithProgressWidget(
|
|
trigger: _isLoading,
|
|
progressText: 'Adding Manufacturer',
|
|
buttonText: 'Save',
|
|
onPressed: _saveManufacturer)
|
|
],
|
|
)))
|
|
],
|
|
),
|
|
)),
|
|
);
|
|
}
|
|
}
|