0

I've created ListView.builder inside the FutureBuilder from my JSON (local) file. Also i've created a TextField widget to make it search bar of my ListView.builder. But I don't know how implement search bar syntax into my TextField and ListView.builder.

This is my FutureBuilder widget :

FutureBuilder(
  future: _isInit ? fetchDoa(context) : Future(null),
  builder: (context, _) {
    if (doaList != null) {
      return ListView.builder(
        itemCount: doaList.length,
        itemBuilder: (BuildContext context, int index) {
          Doa doa = doaList[index];
          return _itemList(index);
        },
      );
    } else {
      CircularProgressIndicator();
    }
    return CircularProgressIndicator();
  }),

Here is _itemList(index) :

_itemList(index) {
    Doa doa = doaList[index];
    return Container(
        margin: EdgeInsets.symmetric(horizontal: 24),
        child: ListTile(
            title: Text(doa.judul,
                style: TextStyle(fontFamily: "Poppins", fontSize: 16)),
            onTap: () {
              Navigator.of(context).push(MaterialPageRoute(
                  builder: (BuildContext context) => DetailDoa(doa: doa)));
            },
            trailing: Icon(
              Icons.arrow_forward_ios_rounded,
              color: Colors.teal,
            )));
  }

The search bar is separated from FutureBuilder because I used Stack in my project. And this is my TextField that I want to use as search bar :

_searchBar() {
    return SingleChildScrollView(
      child: Center(
        child: Container(
            margin: EdgeInsets.only(top: 60),
            width: context.widthPct(.8),
            height: context.widthPct(.8) / 6,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(34),
              color: Colors.grey[200],
            ),
            child: Container(
                child: Theme(
              data: new ThemeData(
                primaryColor: Colors.grey[200],
              ),
              child: TextField(
                onSubmitted: (String str) {
                  str = str.toLowerCase();
                  setState(() {});
                },
                autofocus: false,
                style:
                    TextStyle(color: Colors.grey[700], fontFamily: "Poppins"),
                cursorColor: Colors.white,
                decoration: InputDecoration(
                    suffixIcon: Icon(
                      Icons.search,
                      color: Colors.grey[700],
                    ),
                    contentPadding: EdgeInsets.fromLTRB(24, 16, 24, 16),
                    hintText: "Cari doa dulu yuk",
                    hintStyle: TextStyle(
                        color: Colors.grey[700], fontFamily: "Poppins"),
                    border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(200),
                        borderSide: new BorderSide(color: Colors.grey[200]))),
              ),
            ))),
      ),
    );
  }

Here is the preview of my app :

enter image description here

Thank you very much for your help :)

2
  • How can use regex for searching Commented Apr 17, 2021 at 11:58
  • @ProblematicDude how to use that??? I'm sorry because i'm still newbie in Flutter :( Commented Apr 17, 2021 at 12:03

1 Answer 1

2

To achieve search functionality,

  • You have to return _itemList(index); conditionally.

  • return _itemList(index); when search string is empty.

  • If search string isn't empty, check any matches of search string with doaList[index].judul if matches then, return _itemList(index); or return Container().

  • Search String is the input user given in search field.

  • Check below snippet with minimal logic to search two string with your case.

    String searchSrc = doaList[index].judul.toString().toLowerCase();
    String searchTarget = searchString.toString().toLowerCase();
    
    if(searchTarget == "") return _itemList(index);
    
    if (searchSrc.contains(searchTarget)) {
      // found
      return _itemList(index);
    } else {
      // not found
      return Container();
    }
    
Sign up to request clarification or add additional context in comments.

3 Comments

replit.com/join/mhwfyckk-nabilrei here is my full code. can you help me to create a syntax for search bar?? thank you :)
Updated, Please check.
WOW it's work ! THANK YOU VERY MUCH PATHIK :))) Hope you always success

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.