ianldgs / material_search

https://pub.dartlang.org/packages/material_search
MIT License
75 stars 39 forks source link

How to search for something else and show something else on result list ? #33

Closed jagdishjadeja closed 6 years ago

jagdishjadeja commented 6 years ago

Hello there I have 2 items to search for data = [ { title:"some title 1",content:"does not contain any word related to title 1" }, { title:"some title 2",content:"does not contain any word related to title 2" } ] now i want to search inside of content and so title as MaterialResult how can i do that ?

ianldgs commented 6 years ago

What you want is filter by the content, but show the titles?

The type of your data (which will be passed to onSelect) will be String or the object with the title and content?

jagdishjadeja commented 6 years ago

Hi that problem is solved now but i have one another issue

jagdishjadeja commented 6 years ago

problem i am having is that i want to send selected whole object to somewhere else

data = [
{ title:"some title 1",content:"does not contain any word related to title 1", ....some more fields },
{ title:"some title 2",content:"does not contain any word related to title 2" , ....some more fields}
]

after filtering data i can have only title or content because i am using text as title of object and value as content of my object in MaterialSearchResult so is there any way that i can save my whole object somewhere ?

ianldgs commented 6 years ago

By "saving the whole object" you mean getting it in the onSelect callback?

ianldgs commented 6 years ago

Please, post your current code.

jagdishjadeja commented 6 years ago

Here is my current code

builder: (BuildContext context) {
          return new Material(
            child: new MaterialSearch<String>(
              placeholder: 'Search',
              getResults: (String criteria) async {
                var data = await loadAsset();
                var data2 = json.decode(data);
                List<MaterialSearchResult<String>> _msResult = List();
                if (_rList.length > 0) _rList.clear();          
                 //some loops starts     
                 _msResult.add(
                      MaterialSearchResult<String>(
                          text: MyObject[i][title],
                          value: MyObject[i][content]
                       ) 
                    );
                 //some loops ends
                   return _msResult;
              },
              filter: (dynamic value, String criteria) {
                return value.toLowerCase().trim().contains(
                    new RegExp(r'' + criteria.toLowerCase().trim() + ''));
              },
              onSelect: (dynamic value) => print("select $value"),
              onSubmit: (String value) => print("submit $value"),
            ),
          );
        });

now what i am trying to do is get this whole myobject at onSelect

ianldgs commented 6 years ago

You are going to need something like that:

class MyObj {
    MyObj({this.title, this.content});

    final String title;
    final String content;
}

new Material(
    child: new MaterialSearch<MyObj>(
        placeholder: 'Search',
        getResults: (String criteria) async {
            var data = await loadAsset();
            var data2 = json.decode(data);
            List<MaterialSearchResult<String>> _msResult = List();

            if (_rList.length > 0) {
                _rList.clear();
            }

            _msResult.add(
                MaterialSearchResult<MyObj>(
                    text: title,
                    value: MyObj({title: title, content: content})
                )
            );

            return _msResult;
        },
        filter: (dynamic value, String criteria) {
            return value.toLowerCase().trim().contains(
                new RegExp(r'' + criteria.toLowerCase().trim() + ''));
        },
        onSelect: (dynamic value) => print("select ${value.title} ${value.content}"),
        onSubmit: (String value) => print("submit $value"),
    ),
);

Try it out and let me know if it worked.

jagdishjadeja commented 6 years ago

I am not getting any value Here

onSelect: (dynamic value) => print("select ${value.title} ${value.content}"),