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 '../model/ac_company_response_model.dart'; import '../provider/dio_provider.dart'; import '../repository/mitra_repository.dart'; import '../screen/md_lab_mitra/mitra_lookup_mou_provider.dart'; import 'fx_data_mitra.dart'; import 'fx_text_field.dart'; import 'provider/selectedCompanyProvider.dart'; // ignore: must_be_immutable class FxAcCompany extends HookConsumerWidget { final String? errorValidation; FxAcCompany({ 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 selectedCompany = ref.read(selectedAcCompanyProvider); if (selectedCompany != null) { ctrl.text = selectedCompany.mCompanyName; } ref.listen(selectedAcCompanyProvider, ((prev, next) { if (next != null) { ref .read(mitraLookupMouProvider.notifier) .lookup(companyID: next.mCompanyID); } })); return RawAutocomplete( textEditingController: ctrl, displayStringForOption: (model) => model.mCompanyName, focusNode: fc, fieldViewBuilder: (context, ctrl, fc, onChange) { return FxTextField( ctrl: ctrl, fc: fc, hint: "Company", label: "Company", errorMessage: errorValidation, ); }, optionsBuilder: (tv) async { try { final dio = ref.read(dioProvider); await Future.delayed(const Duration(milliseconds: 300)); final resp = await MitraRepository(dio: dio) .lookupCompany(query: ctrl.text, cancelToken: cancelToken); return resp; } catch (e) { errorMessage.value = "Lookup Company 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(selectedAcCompanyProvider.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.mCompanyName, isBold: true, ), const SizedBox(height: 5), FxNormalBlueText( title: model.mCompanyAddress), ], ), ), )); }), ), ), ); }); } }