import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:test_sa/controllers/localization/localization.dart'; import 'package:test_sa/models/lookup.dart'; import 'package:test_sa/models/subtitle.dart'; import 'package:test_sa/models/visits/visits_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/date_and_time/from_to_date_bar.dart'; import 'package:test_sa/views/widgets/hospitals/hospital_auto_complete_field.dart'; import 'package:test_sa/views/widgets/titles/app_sub_title.dart'; import '../app_text_form_field.dart'; import 'filter_item.dart'; class VisitsSearchDialog extends StatefulWidget { final VisitsSearch initialSearchValue; final bool expandedSearch; final Function(VisitsSearch) onSearch; const VisitsSearchDialog({ Key key, this.initialSearchValue, this.expandedSearch, this.onSearch }) : super(key: key); @override _VisitsSearchDialogState createState() => _VisitsSearchDialogState(); } class _VisitsSearchDialogState extends State with TickerProviderStateMixin{ VisitsSearch _search; List status = [ Lookup(label: "Done", id: 0,), Lookup(label: "Not Yet", id: 1), Lookup(label: "On Hold", id: 2,), ]; List contactStatus = [ Lookup(label: "Hospital Employee", key: "H",), Lookup(label: "Under Warranty", key: "CW"), Lookup(label: "Under Maintenance Contract", key: "CC",), ]; final GlobalKey _formKey = GlobalKey(); @override void initState() { super.initState(); _search = VisitsSearch(); _search.fromSearch(widget.initialSearchValue); } @override Widget build(BuildContext context) { Subtitle _subtitle = AppLocalization.of(context).subtitle; DateTime today = DateTime.now(); return SizedBox( height: MediaQuery.of(context).size.height / 1.3, child: Form( key: _formKey, child: ListView( padding: const EdgeInsets.symmetric(vertical: 8,horizontal: 16), children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ ASmallButton( text: _subtitle.cancel, onPressed: (){ Navigator.of(context).pop(); }, ), ASmallButton( text: _subtitle.search, onPressed: (){ if(!_formKey.currentState.validate()) return; _formKey.currentState.save(); Navigator.of(context).pop(_search); }, ) ], ), ATextFormField( initialValue: _search.deviceSerialNumber, hintText: _subtitle.serialNumber, style: Theme.of(context).textTheme.headline6, textInputAction: TextInputAction.search, onAction: (){ if(!_formKey.currentState.validate()) return; _formKey.currentState.save(); Navigator.of(context).pop(_search); }, onSaved: (value){ _search.deviceSerialNumber = value; }, ), SizedBox(height: 8.0 * AppStyle.getScaleFactor(context),), HospitalAutoCompleteField( initialValue: _search.hospitalName, onSave: (value){ _search.hospitalName = value; }, onSearch: (value){ _search.hospitalName = value; Navigator.of(context).pop(_search); }, ), SizedBox(height: 8.0 * AppStyle.getScaleFactor(context),), ATextFormField( initialValue: _search.brand, hintText: _subtitle.brand, style: Theme.of(context).textTheme.headline6, textInputAction: TextInputAction.search, onAction: (){ if(!_formKey.currentState.validate()) return; _formKey.currentState.save(); Navigator.of(context).pop(_search); }, onSaved: (value){ _search.brand = value; }, ), SizedBox(height: 8.0 * AppStyle.getScaleFactor(context),), ATextFormField( initialValue: _search.model, hintText: _subtitle.model, style: Theme.of(context).textTheme.headline6, textInputAction: TextInputAction.search, onAction: (){ if(!_formKey.currentState.validate()) return; _formKey.currentState.save(); Navigator.of(context).pop(_search); }, onSaved: (value){ _search.model = value; }, ), SizedBox(height: 8.0 * AppStyle.getScaleFactor(context),), ASubTitle(_subtitle.status), SizedBox(height: 4.0 * AppStyle.getScaleFactor(context),), Wrap( spacing: 10, runSpacing: 10, children: List.generate( status.length, (index) { bool isSelected = _search.statusValue == status[index].id; return FilterItem( isSelected: isSelected, onSelected: (){ if(isSelected) _search.statusValue = null; else _search.statusValue = status[index].id; setState(() {}); }, status: status[index], ); } ), ), SizedBox(height: 8.0 * AppStyle.getScaleFactor(context),), ASubTitle(_subtitle.contactStatus), SizedBox(height: 4.0 * AppStyle.getScaleFactor(context),), Wrap( spacing: 10, runSpacing: 10, children: List.generate( contactStatus.length, (index) { bool isSelected = _search.contactStatus == contactStatus[index].key; return FilterItem( isSelected: isSelected, onSelected: (){ if(isSelected) _search.contactStatus = null; else _search.contactStatus = contactStatus[index].key; setState(() {}); }, status: contactStatus[index], ); } ), ), SizedBox(height: 8.0 * AppStyle.getScaleFactor(context),), ASubTitle(_subtitle.actualDate), SizedBox(height: 4.0 * AppStyle.getScaleFactor(context),), FromToDateBar( from: _search.actualDateFrom , to: _search.actualDateTo, onPickFrom: (date){ _search.actualDateFrom = date; }, onPickTo: (date){ _search.actualDateTo = date; }, ), SizedBox(height: 8.0 * AppStyle.getScaleFactor(context),), ASubTitle(_subtitle.expectDate), SizedBox(height: 4.0 * AppStyle.getScaleFactor(context),), FromToDateBar( from: _search.expectedDateFrom ?? DateTime(today.year, today.month, 1), to: _search.expectedDateTo ?? DateTime(today.year, (today.month +1).clamp(1, 12) , today.month == 12 ? 31 : 0), onPickFrom: (date){ _search.expectedDateFrom = date; }, onPickTo: (date){ _search.expectedDateTo = date; }, ), Visibility( visible: _search.toSearchString().isNotEmpty, child: Padding( padding: const EdgeInsets.symmetric(vertical: 8,horizontal: 16), child: AButton( padding: EdgeInsets.zero, text: _subtitle.clearSearch, onPressed: (){ _search = VisitsSearch(); Navigator.of(context).pop(_search); }, ), ), ), ], ), ), ); } }