One UI in flutter
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
4.0 KiB

2 years ago
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';
2 years ago
import '../screen/md_lab_mitra/mitra_lookup_mou_provider.dart';
2 years ago
import 'fx_data_mitra.dart';
2 years ago
import 'fx_text_field.dart';
import 'provider/selectedCompanyProvider.dart';
2 years ago
// ignore: must_be_immutable
class FxAcCompany extends HookConsumerWidget {
final String? errorValidation;
2 years ago
final bool readOnly;
FxAcCompany({Key? key, this.errorValidation, this.readOnly = false})
: super(key: key);
2 years ago
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);
2 years ago
if (selectedCompany != null) {
ctrl.text = selectedCompany.mCompanyName;
}
ref.listen<AcCompanyModel?>(selectedAcCompanyProvider, ((prev, next) {
2 years ago
if (next != null) {
ref
.read(mitraLookupMouProvider.notifier)
.lookup(companyID: next.mCompanyID);
}
}));
2 years ago
return RawAutocomplete<AcCompanyModel>(
textEditingController: ctrl,
displayStringForOption: (model) => model.mCompanyName,
focusNode: fc,
fieldViewBuilder: (context, ctrl, fc, onChange) {
2 years ago
return FxTextField(
2 years ago
isReadOnly: readOnly,
isEnabled: !readOnly,
2 years ago
ctrl: ctrl,
fc: fc,
hint: "Company",
label: "Company",
errorMessage: errorValidation,
2 years ago
);
},
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) {
2 years ago
errorMessage.value = "Lookup Company Error";
2 years ago
}
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 =
2 years ago
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),
],
),
),
));
}),
),
),
);
});
}
}