firestore_indexing_search 0.0.2 copy "firestore_indexing_search: ^0.0.2" to clipboard
firestore_indexing_search: ^0.0.2 copied to clipboard

A Flutter package that enables instant search on your firestore project

example/lib/main.dart

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firestore_indexing_search/firestore_indexing_search.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter indexing demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  SearchController searchController;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    signIn();
  }

  void signIn() async {
    // await FirebaseAuth.instance.signInAnonymously().then((value) {
    //   setState(() {
    //     print('user Signed in anonymously');
    //   });
    // });
    await Indexer.indexDatabase(inCollection: 'xxx', withFieldToIndex: 'xxx');
    
    // await SearchController.removeAllIndex(inCollection: 'xxx', indexedField: 'xxx', showLogs: true);
    // await SearchController.isField('xxx', indexedInCollection: 'xxx',);

    // Indexer.addInCollection('xxxy', fieldToIndex: 'xxx', fieldToIndexContent: 'my names is emile', others: {
    //   'car name': 'mercedes x23',
    //   'car year': '2019',

    // }, allowEmptyAndNullCollection: true, showLogs: true);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: IconButton(
            icon: Icon(Icons.nature_people),
            onPressed: () {
              searchController = SearchController();
              searchController.cursorColor = Colors.red;

              searchController.custonResultWidget = WidgetToDisplayInAsAResult(
                searchController: searchController,
              );
              FirestoreIndexingSearch.showSearch(context,
                  //This is the name of the collection where the search will happen
                  inCollection: 'xxx',
                  //This is the name of the field you want to seach
                  indexedField: 'xxx',
                  searchController: searchController);
            }),
      ),
      // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

class WidgetToDisplayInAsAResult extends StatefulWidget {
  final SearchController searchController;

  const WidgetToDisplayInAsAResult({Key key, this.searchController})
      : super(key: key);
  @override
  _WidgetToDisplayInAsAResultState createState() =>
      _WidgetToDisplayInAsAResultState();
}

class _WidgetToDisplayInAsAResultState
    extends State<WidgetToDisplayInAsAResult> {
  int _persistedIndex;
  @override
  void initState() {
    super.initState();
    //YOU MUST DO THIS IN YOUR CODE
    _persistedIndex = widget.searchController.internalIndex;
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        print('the index is $_persistedIndex');
        print(
            '${widget.searchController.searchResult[_persistedIndex]['xxx']} was just tapped now');
      },
      child: Container(
        decoration: BoxDecoration(
          color: Colors.transparent,
        ),
        height: (MediaQuery.of(context).size.height / 100) * 12,
        width: (MediaQuery.of(context).size.width / 100) * 15,
        child: Card(
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(30)),
          child: Center(
            child: RichText(text: TextSpan(text: widget.searchController.texFieldValue,
            //Looks like when you do not add the color, the text gets invisible lol
            style: TextStyle(color:Colors.black,fontWeight: FontWeight.bold),
             children: [
              TextSpan(text: widget.searchController.searchResult[widget.searchController.internalIndex]['xxx'].substring(widget.searchController.texFieldValue.length), style: TextStyle(color: Colors.black38, fontWeight: FontWeight.normal))
            ])) ,
          ),
        ),
      ),
    );
  }
}