icemanbsi / searchable_dropdown

MIT License
107 stars 164 forks source link

keyboardType, validator, label, searchFn, mutipleSelection + format + version + description #11

Closed lcuis closed 4 years ago

lcuis commented 4 years ago

Label and error returned by function as Widget:

      validator: (value){return(value==null?Row(
        children: <Widget>[
          Icon(Icons.error,color: Colors.red,size: 14,),
          SizedBox(width: 5,),
          Text("Mandatory",style: TextStyle(color: Colors.red,fontSize: 13),),
        ],
      ):null);},
      label: (value){return(Row(
        children: <Widget>[
          Icon(Icons.info,color: Colors.blueAccent,size: 14,),
          SizedBox(width: 5,),
          Text("Oil producer",style: TextStyle(color: Colors.blueAccent,fontSize: 13),),
        ],
      ));},

image Or as strings:

      validator: (value){return(value==null?"Mandatory":null);},
      label: (value){return("Oil producer");},

image

Possibility to set the keyboard type for searches as follows:

keyboardType: TextInputType.number,

image

New searchFn lets the developer define the search function through which the list items are filtered. For example, the string typed by the user can be considered as keywords separated by spaces:

      searchFn: (String keyword, List<DropdownMenuItem<MyData>> items) {
        List<int> ret = List<int>();
        if (keyword != null && items != null) {
          keyword.split(" ").forEach((k) {
            int i = 0;
            items.forEach((item) {
              if (keyword.isEmpty || (k.isNotEmpty &&
                  (item.value.toString().toLowerCase().contains(k.toLowerCase())))) {
                ret.add(i);
              }
              i++;
            });
          });
        }
        if(keyword.isEmpty){
          ret = Iterable<int>.generate(items.length).toList();
        }
        return (ret);
      },

Here is an example of result:

image

Multiple selection

Multiple selection is available through the multipleSelection parameter:

multipleSelection: true,

image This disables onChanged callback function. See below.

There is a way to customize the display of the selected and unselected items through the displayItemWhenMultiple parameter function:

      displayItemWhenMultiple: (item,selected) {
        return (Row(children: [
          selected ? Icon(Icons.check, color: Colors.green,) : Icon(
            Icons.check_box_outline_blank, color: Colors.grey,),
          SizedBox(width: 7),
          item
        ]));
      },

image

The done button is displayed only when in multiple selection mode. It can be customized either by setting the String parameter doneText or by setting the function parameter doneButtonFn:

doneText: "OK",

image Or:

      doneButtonFn: (newContext){return FlatButton(
          onPressed: () {
            Navigator.pop(newContext);
          }, child: Text("I'm through"));},

image

The multiple items set by default can be set through the parameter selectedItems as follows:

  List<int> selectedItems = [1,3];
...
      selectedItems: selectedItems,

Where the int values are the indexes of the selected items in the list of items. image

Once the selection is done, the selectedItems list above is updated or the values can be retrieved through the onChangedMultiple function parameter:

      onChangedMultiple: (List<int> selectedIndexes){
        selectedIndexes.forEach((item){
          print("selected index: $item");
        });
      },

The indexes of the selected items are sent to this callback function. Note that in the multiple selection case, the onChanged callback function is not called as the parameter type is not a list.

The validator function can also be used in the multiple selection case:

validator: (List<int>selectedIndexes){return(selectedIndexes==null||selectedIndexes.length==0?"Mandatory":null);},

The selectedValueWidgetFn function can be used in the mutiple selection case the same way it is used in the single selection case:

      selectedValueWidgetFn: (value) => Padding(
          padding: EdgeInsets.fromLTRB(20, 20, 20, 20),
          child:Text(
            value.toString(),
            style: TextStyle(color:Colors.green),
          )
      ),

The clear button works the same way in multiple and single cases.

Ran:

flutter format lib/searchable_dropdown.dart

As suggested here: https://pub.dev/packages/searchable_dropdown#-analysis-tab-

Improved package description as suggested here: https://pub.dev/packages/searchable_dropdown#-analysis-tab- Increased version from 1.1.0 to 1.1.1

Added to the README.md file:

lcuis commented 4 years ago

Hello @icemanbsi , I think I have finished this PR. I don't think I will add a "Select all"/"Deselect all" button now. Cheers, Louis.

lcuis commented 4 years ago

Hello @icemanbsi ,

I fell I changed things a bit too much for a PR to make sense now. I hope you don't mind I closed it? I am happy to reopen it if you'd like though.

Cheers.

icemanbsi commented 4 years ago

Hi @lcuis , sorry for a long time not responding on your PR. Yeah, that's really a lot of things, and I think i can merge this PR. Do you mind if I add you as a collaborator? so you can add more features to this widget freely.. :D

lcuis commented 4 years ago

Hello @icemanbsi ,

Thank you very much for the merge and the contributor proposal!

This PR was against my master branch which changed significantly since the PR proposal. It contained many more changes than those listed in the description as this is a different plugin:

This has several consequences for the users:

This also has consequences for your repository:

I will try to correct the consequences for the users so that they are not affected and I will probably remove the automated testing, the CI and the CD. Please let me know if you have a different point of view.

icemanbsi commented 4 years ago

yes, I'm very aware of it. since I haven't published any update on flutter pub, I think it should be ok. Your version is much more better than I am. So it should be a good point for the user to change to the newer one with so much more flexibility. that's why I approved the PR and merged it.

lcuis commented 4 years ago

Ok, thanks, I am working on the next PR to correct things for your plugin publication.