You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			125 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			Dart
		
	
			
		
		
	
	
			125 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			Dart
		
	
| import 'package:flutter/material.dart';
 | |
| import 'package:provider/provider.dart';
 | |
| import 'package:test_sa/extensions/context_extension.dart';
 | |
| import 'package:test_sa/extensions/int_extensions.dart';
 | |
| import 'package:test_sa/models/lookup.dart';
 | |
| import 'package:test_sa/modules/asset_inventory_module/provider/asset_inventory_provider.dart';
 | |
| import 'package:test_sa/new_views/app_style/app_color.dart';
 | |
| import 'package:test_sa/views/app_style/sizing.dart';
 | |
| import '../../../extensions/text_extensions.dart';
 | |
| import '../../../new_views/app_style/app_text_style.dart';
 | |
| 
 | |
| class LookUpAutoCompleteField extends StatefulWidget {
 | |
|   final String initialValue;
 | |
|   final String label;
 | |
|   final num? assetId;
 | |
|   final bool forAssetName;
 | |
|   final bool  forSupplier;
 | |
|   final bool clearAfterPick, isManufacturer;
 | |
|   final Function(Lookup) onPick;
 | |
|   final Function(String) onChanged;
 | |
|    //need to pass directly url
 | |
|   const LookUpAutoCompleteField(
 | |
|       {Key? key,
 | |
|       this.isManufacturer = false,
 | |
|       required this.initialValue,
 | |
|       required this.label,
 | |
|       this.forAssetName = false,
 | |
|       this.forSupplier = false,
 | |
|       this.assetId,
 | |
|       required this.onPick,
 | |
|       this.clearAfterPick = true,
 | |
|       required this.onChanged})
 | |
|       : super(key: key);
 | |
| 
 | |
|   @override
 | |
|   _AutoCompletePartsFieldState createState() => _AutoCompletePartsFieldState();
 | |
| }
 | |
| 
 | |
| class _AutoCompletePartsFieldState extends State<LookUpAutoCompleteField> {
 | |
|   AssetInventoryProvider? assetInventoryProvider;
 | |
|   late TextEditingController _controller;
 | |
| 
 | |
|   @override
 | |
|   void initState() {
 | |
|     _controller = TextEditingController(text: widget.initialValue);
 | |
|     super.initState();
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   void didUpdateWidget(covariant LookUpAutoCompleteField oldWidget) {
 | |
|     if (widget.initialValue != oldWidget.initialValue) {
 | |
|       _controller = TextEditingController(text: widget.initialValue);
 | |
|     }
 | |
|     super.didUpdateWidget(oldWidget);
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   void dispose() {
 | |
|     _controller.dispose();
 | |
|     super.dispose();
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   Widget build(BuildContext context) {
 | |
|     assetInventoryProvider ??= Provider.of<AssetInventoryProvider>(context);
 | |
|     final border = UnderlineInputBorder(borderSide: BorderSide.none, borderRadius: BorderRadius.circular(10));
 | |
|     return Container(
 | |
|       decoration: BoxDecoration(
 | |
|         color: AppColor.background(context),
 | |
|         borderRadius: BorderRadius.circular(AppStyle.borderRadius * AppStyle.getScaleFactor(context)),
 | |
|         //  boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.05), blurRadius: 10)],
 | |
|       ),
 | |
|       child: Autocomplete<Lookup>(
 | |
|         optionsBuilder: (TextEditingValue textEditingValue) async {
 | |
|           if (textEditingValue.text.isEmpty) {
 | |
|             return const Iterable<Lookup>.empty();
 | |
|           }
 | |
|           return (await assetInventoryProvider!.getAutoCompleteDetails(query: textEditingValue.text, isManufacturer: widget.isManufacturer, type: widget.forAssetName?1:widget.forSupplier?2:0));
 | |
|         },
 | |
|         displayStringForOption: (Lookup option) => option.name ?? "",
 | |
|         fieldViewBuilder: (BuildContext context, TextEditingController fieldTextEditingController, FocusNode fieldFocusNode, VoidCallback onFieldSubmitted) {
 | |
|           return TextField(
 | |
|             controller: _controller,
 | |
|             focusNode: fieldFocusNode,
 | |
|             style: AppTextStyles.bodyText.copyWith(color: AppColor.black10),
 | |
|             textAlign: TextAlign.start,
 | |
|             decoration: InputDecoration(
 | |
|               border: border,
 | |
|               disabledBorder: border,
 | |
|               focusedBorder: border,
 | |
|               enabledBorder: border,
 | |
|               errorBorder: border,
 | |
|               contentPadding: EdgeInsets.symmetric(vertical: 8.toScreenHeight, horizontal: 16.toScreenWidth),
 | |
|               constraints: const BoxConstraints(),
 | |
|               suffixIconConstraints: const BoxConstraints(minWidth: 0),
 | |
|               filled: true,
 | |
|               fillColor: AppColor.fieldBgColor(context),
 | |
|               errorStyle: AppTextStyle.tiny.copyWith(color: context.isDark ? AppColor.red50 : AppColor.red60),
 | |
|               floatingLabelStyle: AppTextStyle.body1.copyWith(fontWeight: FontWeight.w500, color: context.isDark ? null : AppColor.neutral20),
 | |
|               labelText: widget.label,
 | |
|               labelStyle: AppTextStyles.tinyFont.copyWith(color: AppColor.textColor(context)),
 | |
|             ),
 | |
|             textInputAction: TextInputAction.search,
 | |
|             onChanged: (text) {
 | |
|               widget.onChanged(text);
 | |
|               fieldTextEditingController.text = text;
 | |
|             },
 | |
|             onSubmitted: (String value) {
 | |
|               onFieldSubmitted();
 | |
|             },
 | |
|           );
 | |
|         },
 | |
|         onSelected: (Lookup selection) {
 | |
|           if (widget.clearAfterPick) {
 | |
|             _controller.clear();
 | |
|           } else {
 | |
|             _controller.text = selection.name ?? '';
 | |
|           }
 | |
|           widget.onPick(selection);
 | |
|         },
 | |
|       ),
 | |
|     );
 | |
|   }
 | |
| }
 |