import 'package:flutter/material.dart'; import 'package:test_sa/controllers/localization/localization.dart'; import 'package:test_sa/extensions/context_extension.dart'; import 'package:test_sa/models/device/device_transfer_search.dart'; import 'package:test_sa/views/app_style/sizing.dart'; import 'package:test_sa/views/widgets/buttons/app_button.dart'; import 'package:test_sa/views/widgets/buttons/app_small_button.dart'; import 'package:test_sa/views/widgets/equipment/device_button.dart'; import '../../../controllers/providers/api/hospitals_provider.dart'; import '../app_text_form_field.dart'; import '../gas_refill/building_type_menu.dart'; import '../gas_refill/department_type_menu.dart'; import '../gas_refill/floor_type_menu.dart'; import '../hospitals/hospital_auto_complete_field.dart'; import '../switch_button.dart'; class AssetTransferSearchDialog extends StatefulWidget { final DeviceTransferSearch initialSearchValue; final bool expandedSearch; final Function(DeviceTransferSearch) onSearch; const AssetTransferSearchDialog({Key key, this.initialSearchValue, this.expandedSearch, this.onSearch}) : super(key: key); @override AssetTransferSearchDialogState createState() => AssetTransferSearchDialogState(); } class AssetTransferSearchDialogState extends State with TickerProviderStateMixin { DeviceTransferSearch _search; final GlobalKey _formKey = GlobalKey(); bool _isLoading = false; @override void initState() { super.initState(); _search = DeviceTransferSearch(); _search.fromSearch(widget.initialSearchValue); } @override Widget build(BuildContext context) { return Padding( padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom), child: ClipRRect( borderRadius: const BorderRadius.only(topLeft: Radius.circular(15), topRight: Radius.circular(15)), clipBehavior: Clip.antiAliasWithSaveLayer, child: Container( color: Colors.white, height: MediaQuery.of(context).size.height / 1.3, padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 20), child: Form( key: _formKey, child: ListView( padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16), children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ ASmallButton( text: context.translation.cancel, onPressed: () { Navigator.of(context).pop(); }, ), ASmallButton( text: context.translation.search, onPressed: () { if (!_formKey.currentState.validate()) { return; } _formKey.currentState.save(); Navigator.of(context).pop(_search); }, ) ], ), ASwitchButton( title: "Most Recent", value: _search.mostRecent ?? false, onChange: (value) { _search.mostRecent = value; setState(() {}); }, ), SizedBox(height: 8.0 * AppStyle.getScaleFactor(context)), DeviceButton( device: _search.device, onDevicePick: (device) { _search.device = device; setState(() {}); }, ), SizedBox(height: 8.0 * AppStyle.getScaleFactor(context)), ATextFormField( initialValue: _search.title, hintText: context.translation.title, style: Theme.of(context).textTheme.titleLarge, textInputAction: TextInputAction.search, onAction: () { if (!_formKey.currentState.validate()) { return; } _formKey.currentState.save(); Navigator.of(context).pop(_search); }, onSaved: (value) { _search.title = value; }, ), SizedBox(height: 8.0 * AppStyle.getScaleFactor(context)), HospitalAutoCompleteField( initialValue: _search?.hospital?.name, onSearch: (selected) async { _search.building = null; _search.floor = null; _search.department = null; _search.buildingsList = null; _search.floorsList = null; _search.departmentsList = null; _isLoading = true; setState(() {}); await HospitalsProvider().getHospitalsListByVal(searchVal: selected?.name ?? "").then((value) { _search.hospital = value?.firstWhere((element) => element.name == selected.name, orElse: () => null); _search.buildingsList = _search.hospital?.buildings; }); _isLoading = false; setState(() {}); }, ), const SizedBox(height: 8), BuildingTypeMenu( initialValue: _search?.building, building: _search.buildingsList, enabled: !_isLoading, onSelect: (status) { _search.building = status; _search.floorsList = status?.floors; setState(() {}); }, ), const SizedBox(height: 8), FloorTypeMenu( initialValue: _search?.floor, floors: _search.floorsList, enabled: !_isLoading, onSelect: (status) { _search.floor = status; _search.departmentsList = _search.floor?.departments; setState(() {}); }, ), const SizedBox(height: 8), DepartmentTypeMenu( initialValue: _search?.department, departments: _search.departmentsList, enabled: !_isLoading, onSelect: (status) { _search.department = status; setState(() {}); }, ), SizedBox(height: 8.0 * AppStyle.getScaleFactor(context)), ATextFormField( initialValue: _search.room, hintText: context.translation.room, style: Theme.of(context).textTheme.titleLarge, textInputAction: TextInputAction.search, onAction: () { if (!_formKey.currentState.validate()) { return; } _formKey.currentState.save(); Navigator.of(context).pop(_search); }, onSaved: (value) { _search.room = value; }, ), SizedBox(height: 16.0 * AppStyle.getScaleFactor(context)), Visibility( visible: (_search.toMap()..remove("mostRecent"))?.isNotEmpty ?? false, child: Padding( padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16), child: AButton( padding: EdgeInsets.zero, text: context.translation.clearSearch, onPressed: () { _search = DeviceTransferSearch(); Navigator.of(context).pop(_search); }, ), ), ), ], ), ), ), ), ); } }