Flutter how to implement search in a List for beginners.

 

Flutter how to implement search in a List for beginners.








In this post, we just provide you with the code on how to add a search function to your flutter application without any plugins with pure dart code. This search functions fast and very easy to implement in the application. In this app, we have a list of fruits where we filter the list to get the result.In our UI we have an app bar inside it we have our search box and our app body we have a list

main.dart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
 
void main() {
  runApp(MyApp());
}
 
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}
 
class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
 
class _MyHomePageState extends State<MyHomePage> {
  var _textEditingController = TextEditingController();
  List _foods = [
    "Mango",
    "Banana",
    "Watermelon",
    "Strawberries",
    "Orange",
    "Lemon",
    "Blueberries",
    "Peach",
    "Apple",
    "Peas",
    "Chickpeas",
    "Okra",
    "Cantaloupe",
    "Avocado",
    "Pineapple",
    "Cherries",
    "Pears",
    "Limes",
    "Peanut",
    "Black pepper",
  ];
  List _filteredFoods = [];
 
  _searchFunction() {
    _filteredFoods = [];
    for (int i = 0; i < _foods.length; ++i) {
      var item = _foods[i];
 
      if (item
          .toLowerCase()
          .contains(_textEditingController.text.toLowerCase())) {
        _filteredFoods.add(item);
      }
      setState(() {});
    }
  }
 
  @override
  void dispose() {
    _textEditingController.dispose();
    super.dispose();
  }
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: TextField(
          controller: _textEditingController,
          onSubmitted: (value) {
            _textEditingController.clear();
            setState(() {});
          },
          onChanged: (value) {
            _searchFunction();
          },
        ),
      ),
      body: ListView.builder(
          itemCount: _textEditingController.text.isEmpty
              ? _foods.length
              : _filteredFoods.length,
          itemBuilder: (BuildContext context, index) {
            return Container(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: _textEditingController.text.isEmpty
                        ? Text(
                            _foods[index],
                            style: TextStyle(fontSize: 22),
                          )
                        : Text(
                            _filteredFoods[index],
                            style: TextStyle(fontSize: 22),
                          ),
                  ),
                  Divider()
                ],
              ),
            );
          }),
    );
  }
}

Popular posts from this blog

In-App Purchase with null safety in Flutter 2.5.

How to add In-App Purchase subscription in Flutter.

Flutter Native Ad Templates Implementation