Search
 
SCRIPT & CODE EXAMPLE
 

DART

how to perform a text search over json data in flutter

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Contacts Demo',
      debugShowCheckedModeBanner: false,
      home: StorageUpload(),
    );
  }
}

class StorageUpload extends StatefulWidget {
  @override
  StorageUploadState createState() => new StorageUploadState();
}

class StorageUploadState extends State<StorageUpload> {
  List results = [];

  var rows = [];
  String query = '';
  TextEditingController tc;

  @override
  void initState() {
    super.initState();
    tc = TextEditingController();
    rows = [
      {
        'contact_name': 'Test User 1',
        'contact_phone': '066 560 4900',
      },
      {
        'contact_name': 'Test User 2',
        'contact_phone': '066 560 7865',
      },
      {
        'contact_name': 'Test User 3',
        'contact_phone': '906 500 4334',
      }
    ];
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(
          "Delivero Contacts",
          style: new TextStyle(
            color: Colors.white,
          ),
        ),
      ),
      body: Container(
        color: Colors.white,
        padding: EdgeInsets.all(10),
        child: Stack(
          children: [
            Column(
              children: [
                Padding(
                  padding: const EdgeInsets.all(10.0),
                  child: TextField(
                    controller: tc,
                    decoration: InputDecoration(hintText: 'Search...'),
                    onChanged: (v) {
                      setState(() {
                        query = v;
                        setResults(query);
                      });
                    },
                  ),
                ),
                Container(
                  color: Colors.white,
                  child: query.isEmpty
                      ? ListView.builder(
                          shrinkWrap: true,
                          itemCount: rows.length,
                          itemBuilder: (con, ind) {
                            return ListTile(
                              title: Text(rows[ind]['contact_name']),
                              subtitle: Text(rows[ind]['contact_phone']),
                              onTap: () {
                                setState(() {
                                  tc.text = rows[ind]['contact_name'];
                                  query = rows[ind]['contact_name'];
                                  setResults(query);
                                });
                              },
                            );
                          },
                        )
                      : ListView.builder(
                          shrinkWrap: true,
                          itemCount: results.length,
                          itemBuilder: (con, ind) {
                            return ListTile(
                              title: Text(results[ind]['contact_name']),
                              subtitle: Text(results[ind]['contact_phone']),
                              onTap: () {
                                setState(() {
                                  tc.text = results[ind]['contact_name'];
                                  query = results[ind]['contact_name'];
                                  setResults(query);
                                });
                              },
                            );
                          },
                        ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  void setResults(String query) {
    results = rows
        .where((elem) =>
            elem['contact_name']
                .toString()
                .toLowerCase()
                .contains(query.toLowerCase()) ||
            elem['contact_phone']
                .toString()
                .toLowerCase()
                .contains(query.toLowerCase()))
        .toList();
  }
}
Comment

PREVIOUS NEXT
Code Example
Dart :: dart destructor 
Dart :: a function body must be provided. try adding a function body. flutter 
Dart :: extract common elements from lists dart 
Dart :: dart multi line print statement 
Dart :: search in array dart 
Dart :: flutter padding symmetric 
Dart :: app bar for chat flutter 
Dart :: convert seconds to minutes in Dart 
Dart :: round border container flutter 
Swift :: center text swiftui 
Swift :: swift change button text 
Swift :: swift array to string 
Swift :: delete padding list swiftui 
Swift :: swift uicollectionviewcell how to know when off screen 
Swift :: date formatter swift 
Swift :: use timer swift 
Swift :: switches xcode 
Swift :: Save structure in userdefaults ios swift 
Swift :: swiftui circle 
Swift :: swift set uicolor 
Swift :: dismiss two view controllers at once swift 
Swift :: swift animate add subview 
Swift :: hex color extension swift 
Swift :: swiftui create search bar 
Swift :: swift 5 make image fit uiimageview 
Swift :: if else if and else statments in swift language 
Swift :: swift add navigation bar 
Swift :: swift check if regex is in string 
Swift :: swift dictionary 
Swift :: swift reload view 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =