import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:onemd/widget/provider/selectedDoctorProvider.dart'; import '../model/ac_doctor_model.dart'; import '../provider/dio_provider.dart'; import '../repository/mitra_repository.dart'; import 'fx_data_mitra.dart'; import 'fx_text_field.dart'; import 'provider/doctor_address_lookup_provider.dart'; // ignore: must_be_immutable class FxAcDoctor extends HookConsumerWidget { final String? errorValidation; FxAcDoctor({Key? key, this.errorValidation}) : super(key: key); CancelToken? cancelToken; @override Widget build(BuildContext context, WidgetRef ref) { final errorMessage = useState(""); final ctrl = useTextEditingController(text: ""); cancelToken = CancelToken(); final fc = FocusNode(); final selectedDoctor = ref.read(selectedAcDoctorProvider); if (selectedDoctor != null) { ctrl.text = selectedDoctor.fullName; } ref.listen(selectedAcDoctorProvider, ((prev, next) { if (next != null) { ref .read(doctorAddressLookupProvider.notifier) .lookup(doctorID: next.mDoctorID); } })); return RawAutocomplete( textEditingController: ctrl, displayStringForOption: (model) => model.fullName, focusNode: fc, fieldViewBuilder: (context, ctrl, fc, onChange) { return FxTextField( label: "Doctor", hint: "Doctor", fc: fc, ctrl: ctrl, errorMessage: errorValidation, ); }, optionsBuilder: (tv) async { try { final dio = ref.read(dioProvider); await Future.delayed(const Duration(milliseconds: 300)); final resp = await MitraRepository(dio: dio) .lookupDoctor(query: ctrl.text, cancelToken: cancelToken); return resp; } catch (e) { errorMessage.value = "lookupDoctor Error"; } return []; }, optionsViewBuilder: (context, onSelect, listModel) { return Align( alignment: Alignment.topLeft, child: SizedBox( width: 400, child: Material( child: ListView.builder( itemCount: listModel.length, itemBuilder: (context, idx) { final model = listModel.elementAt(idx); return InkWell( onTap: () { ref.read(selectedAcDoctorProvider.notifier).state = model; onSelect(model); }, child: Container( color: (idx % 2 == 1) ? Colors.blue.shade100.withOpacity(0.2) : null, child: Padding( padding: const EdgeInsets.all(8.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ FxNormalBlueText( title: model.fullName, isBold: true, ), ], ), ), )); }), ), ), ); }); } }