maheshmnj / searchfield

A highly customizable simple and easy to use flutter Widget to add a searchfield to your Flutter Application.This Widget allows you to search and select from list of suggestions.
MIT License
72 stars 55 forks source link

Controller is not woking #140

Open rishi46 opened 1 month ago

rishi46 commented 1 month ago

Describe the bug A clear and concise description of what the bug is. After passing the controller it does not give any value entered in the search field

Code sample

Show code sample ```dart class SearchFieldWidget extends StatelessWidget { const SearchFieldWidget({super.key}); @override Widget build(BuildContext context) { final controller = Get.find(); final textEditingController = TextEditingController(); log("textEditingController ${textEditingController.text}"); return Obx( () => SearchField( controller: textEditingController, onSearchTextChanged: (query) { final filter = controller.distributors .where( (distributor) => "${distributor.customerNameTxt.toLowerCase()} (${distributor.customerCd.toLowerCase()})" .contains(query.toLowerCase()), ) .toList(); return filter .map( (distributor) => SearchFieldListItem( "${distributor.customerNameTxt.capitalize} (${distributor.customerCd})", child: Padding( padding: const EdgeInsets.symmetric(vertical: 4.0), child: Text( "${distributor.customerNameTxt.capitalize} (${distributor.customerCd})", style: Theme.of(context).textTheme.bodyMedium), ), ), ) .toList(); }, suggestions: controller.distributors .map( (distributor) => SearchFieldListItem( "${distributor.customerNameTxt.capitalize} (${distributor.customerCd})", item: jsonEncode({ "distributorId": distributor.customerCd, "distributorName": distributor.customerNameTxt }), // distributor.customerCd ), ) .toList(), searchStyle: Theme.of(context).textTheme.bodyMedium, suggestionStyle: Theme.of(context).textTheme.bodyMedium, searchInputDecoration: AppInputDecorations.textFieldInputDecoration("Search Distributor") .copyWith( suffixIcon: textEditingController.text.isEmpty ? null : IconButton( icon: Icon(Icons.clear), onPressed: () { textEditingController.clear(); // Clear selected distributor details if needed controller.selectedDistributorId.value = ''; controller.selectedDistributorName.value = ''; }, ), ), focusNode: controller.focus, onSuggestionTap: (SearchFieldListItem x) { controller.focus.unfocus(); log("onSuggestionTap ${x.item != null} ${x.toString()}"); if (x.item != null) { Map selectedValueDropDown = json.decode(x.item!); controller.selectedDistributorId.value = selectedValueDropDown['distributorId']!; controller.selectedDistributorName.value = selectedValueDropDown['distributorName']!; controller.getSelectedDistributorData(selectedValueDropDown); } }, ), ); } } ```
maheshmnj commented 1 month ago

Hi @rishi46,

I am unable to reproduce the issue can you share a minimal and complete code sample without the third party package? or you could try modifying this code to create a minimal reproducible code sample.

code sample ```dart import 'package:flutter/material.dart'; import 'package:searchfield/searchfield.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter App', theme: ThemeData( colorSchemeSeed: Colors.indigo, useMaterial3: true, brightness: Brightness.light, ), darkTheme: ThemeData( colorSchemeSeed: Colors.blue, useMaterial3: true, brightness: Brightness.dark, ), home: SearchFieldSample(), debugShowCheckedModeBanner: false, ); } } class SearchFieldSample extends StatefulWidget { const SearchFieldSample({Key? key}) : super(key: key); @override State createState() => _SearchFieldSampleState(); } class _SearchFieldSampleState extends State { int suggestionsCount = 12; final focus = FocusNode(); @override void initState() { suggestions = [ 'United States', 'Germany', 'Canada', 'United Kingdom', 'France', 'Italy', 'Spain', 'Australia', 'India', 'China', 'Japan', 'Brazil', 'South Africa', 'Mexico', 'Argentina', 'Russia', 'Indonesia', 'Turkey', 'Saudi Arabia', 'Nigeria', 'Egypt', ]; searchController.addListener(() { print(searchController.text); }); super.initState(); } final TextEditingController searchController = TextEditingController(); var suggestions = []; int counter = 0; @override Widget build(BuildContext context) { Widget searchChild(x) => Padding( padding: const EdgeInsets.symmetric(vertical: 4.0, horizontal: 12), child: Text(x, style: TextStyle(fontSize: 18, color: Colors.black)), ); return Scaffold( appBar: AppBar(title: Text('Searchfield Keyboard Support')), body: Padding( padding: const EdgeInsets.all(8.0), child: ListView( children: [ SearchField( onSearchTextChanged: (query) { final filter = suggestions .where((element) => element.toLowerCase().contains(query.toLowerCase())) .toList(); return filter .map((e) => SearchFieldListItem(e, child: searchChild(e))) .toList(); }, initialValue: SearchFieldListItem('United States'), controller: searchController, onSubmit: (x) {}, autofocus: false, hint: 'Search by country name', itemHeight: 50, suggestions: suggestions .map((e) => SearchFieldListItem(e, child: searchChild(e))) .toList(), focusNode: focus, suggestionState: Suggestion.expand, onSuggestionTap: (SearchFieldListItem x) {}, ), ], ), )); } } ```