You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudsolutions-atoms/lib/views/pages/user/gas_refill/request_gas_refill.dart

233 lines
9.0 KiB
Dart

3 years ago
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import '../../../../api/user_api_client.dart';
import '../../../../controllers/http_status_manger/http_status_manger.dart';
3 years ago
import '../../../../controllers/localization/localization.dart';
import '../../../../controllers/providers/api/gas_refill_provider.dart';
import '../../../../controllers/providers/settings/setting_provider.dart';
import '../../../../controllers/providers/user_provider.dart';
import '../../../../controllers/validator/validator.dart';
import '../../../../models/gas_refill/gas_refill_details.dart';
import '../../../../models/gas_refill/gas_refill_model.dart';
import '../../../../models/subtitle.dart';
import '../../../../models/user.dart';
import '../../../app_style/sizing.dart';
import '../../../widgets/app_text_form_field.dart';
import '../../../widgets/buttons/app_button.dart';
import '../../../widgets/gas_refill/gas_refill_create_details_item.dart';
import '../../../widgets/loaders/loading_manager.dart';
import '../../../widgets/status/gas_refill/gas_type.dart';
import '../../../widgets/titles/app_sub_title.dart';
3 years ago
class RequestGasRefill extends StatefulWidget {
static const String id = "/request-gas-refill";
const RequestGasRefill({Key? key}) : super(key: key);
3 years ago
@override
State<RequestGasRefill> createState() => _RequestGasRefillState();
}
class _RequestGasRefillState extends State<RequestGasRefill> {
bool _isLoading = false;
bool _validate = false;
Subtitle? _subtitle;
GasRefillProvider? _gasRefillProvider;
GasRefillDetails _currentDetails = GasRefillDetails(model: null);
3 years ago
final TextEditingController _requestedQuantityController = TextEditingController();
final GasRefillModel _formModel = GasRefillModel(details: []);
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final GlobalKey _DetailsKey = GlobalKey<FormState>();
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
@override
void setState(VoidCallback fn) {
if (mounted) super.setState(() {});
3 years ago
}
bool _addNewModel() {
3 years ago
_validate = true;
if (!(_formKey.currentState?.validate() ?? false)) {
3 years ago
setState(() {});
return false;
}
_formKey.currentState?.save();
if (!_currentDetails.validate()) {
setState(() {});
3 years ago
return false;
}
_formModel.details?.insert(0, _currentDetails);
3 years ago
_validate = false;
Scrollable.ensureVisible(_DetailsKey.currentContext!);
3 years ago
_requestedQuantityController.clear();
_currentDetails = GasRefillDetails(model: null);
3 years ago
setState(() {});
return true;
}
@override
void dispose() {
_requestedQuantityController.dispose();
super.dispose();
}
3 years ago
@override
Widget build(BuildContext context) {
_subtitle = AppLocalization.of(context)?.subtitle;
_gasRefillProvider = Provider.of<GasRefillProvider>(context, listen: false);
3 years ago
return Scaffold(
key: _scaffoldKey,
body: Form(
key: _formKey,
child: SafeArea(
child: LoadingManager(
isLoading: _isLoading,
isFailedLoading: false,
stateCode: 200,
onRefresh: () async {},
child: SingleChildScrollView(
padding: EdgeInsets.all(12 * AppStyle.getScaleFactor(context)),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
"Request Gas Refill",
style: Theme.of(context).textTheme.headline5?.copyWith(color: Theme.of(context).primaryColor, fontSize: 28, fontWeight: FontWeight.bold),
3 years ago
),
),
),
// const SizedBox(height: 4,),
// ASubTitle(_subtitle.title),
// if(_validate && _formModel.title == null)
// ASubTitle(_subtitle.requiredWord,color: Colors.red,),
// SizedBox(height: 4,),
// ATextFormField(
// initialValue: _formModel?.title,
// textAlign: TextAlign.center,
// style: Theme.of(context).textTheme.subtitle1,
// textInputType: TextInputType.text,
// onSaved: (value){
// _formModel.title = value;
// },
// ),
// // const SizedBox(height: 8,),
// ASubTitle(_subtitle.status),
// if(_validate && _formModel.status == null)
// ASubTitle(_subtitle.requiredWord,color: Colors.red,),
// const SizedBox(height: 4,),
// GasStatusMenu(
// initialValue: _formModel.status,
// onSelect: (status){
// _formModel.status = status;
// },
// ),
// const SizedBox(height: 8,),
// Divider(color: Theme.of(context).colorScheme.primary,),
const SizedBox(
height: 4,
),
3 years ago
const ASubTitle("Type"),
if (_validate && _currentDetails.type == null)
ASubTitle(
_subtitle?.requiredWord ?? "",
color: Colors.red,
),
const SizedBox(
height: 4,
),
3 years ago
GasTypeMenu(
initialValue: _currentDetails.type,
onSelect: (status) {
3 years ago
_currentDetails.type = status;
},
),
// const SizedBox(height: 8,),
// const ASubTitle("Cylinder Size"),
// if(_validate && _currentDetails.cylinderSize == null)
// ASubTitle(_subtitle.requiredWord,color: Colors.red,),
// const SizedBox(height: 4,),
// GasCylinderSizeMenu(
// initialValue: _currentDetails.cylinderSize,
// onSelect: (status){
// _currentDetails.cylinderSize = status;
// },
// ),
const SizedBox(
height: 8,
),
ASubTitle(_subtitle?.quantity ?? ""),
if (_validate && _currentDetails.requestedQuantity == null)
ASubTitle(
_subtitle?.requiredWord ?? "",
color: Colors.red,
),
SizedBox(
height: 4,
),
3 years ago
ATextFormField(
initialValue: (_currentDetails.requestedQuantity??"").toString(),
3 years ago
textAlign: TextAlign.center,
controller: _requestedQuantityController,
style: Theme.of(context).textTheme.subtitle1,
validator: (value) => Validator.isNumeric(value) ? null : "allow numbers only",
3 years ago
textInputType: TextInputType.number,
onSaved: (value) {
_currentDetails.requestedQuantity = int.tryParse(value!);
3 years ago
},
),
const SizedBox(height: 16),
AButton(
text: _subtitle?.add ?? "",
onPressed: _addNewModel,
3 years ago
),
if ((_formModel.details?.isNotEmpty ?? false)) const ASubTitle("Gas Requests"),
3 years ago
ListView.builder(
key: _DetailsKey,
shrinkWrap: true,
physics: const ClampingScrollPhysics(),
itemCount: _formModel.details?.length,
itemBuilder: (context, index) {
final model = _formModel.details![index];
return GasRefillCreateDetailsItem(
model: model,
onDelete: () {
_formModel.details?.remove(model);
setState(() {});
},
);
}),
const SizedBox(height: 16),
AButton(
text: _subtitle?.submit ?? "",
onPressed: () async {
if ((_formModel.details?.isEmpty ?? false)) {
if (!_addNewModel()) return;
}
await _gasRefillProvider?.createModel(
model: _formModel,
context: context
);
},
3 years ago
),
const SizedBox(
height: 100,
)
3 years ago
],
),
),
),
),
),
);
}
}