From 647aeae1df0eb7ebb8ab1261b86bbd02945f56ff Mon Sep 17 00:00:00 2001 From: summerthia Date: Sun, 17 Jul 2022 13:12:37 +0800 Subject: [PATCH] search renting --- lib/models/AllRenting.dart | 88 ++++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 32 deletions(-) diff --git a/lib/models/AllRenting.dart b/lib/models/AllRenting.dart index 90104c4..e2f5592 100644 --- a/lib/models/AllRenting.dart +++ b/lib/models/AllRenting.dart @@ -10,7 +10,7 @@ import 'package:justsharelah_v1/models/feedTitle.dart'; import 'package:justsharelah_v1/models/listings.dart'; // class _AllBorrowingState extends State { -class AllRenting extends StatelessWidget { +class AllRenting extends StatefulWidget { AllRenting({ Key? key, this.userEmailToDisplay = "", @@ -18,32 +18,37 @@ class AllRenting extends StatelessWidget { }) : super(key: key); late String? userEmailToDisplay; - // Future> borrowingData = - // ForBorrowing().getBorrowListingData(); - // get the borrowing listing data put into future of the build context UI + @override + State createState() => _AllRentingState(); +} + +class _AllRentingState extends State { + String searchListingText = ""; + // Future> borrowingData = @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - leading: IconButton( - icon: const Icon(Icons.arrow_back_rounded), - onPressed: () { - Navigator.pop(context); - }, - ), - title: Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Image.asset('images/location.png', width: 40, height: 30), - const SizedBox(width: 10.0), - Text( - "NUS, Singapore", - ) - ], - ), - ), + leading: IconButton( + icon: const Icon(Icons.arrow_back_rounded), + onPressed: () { + Navigator.pop(context); + }, + ), + title: TextFormField( + onChanged: ((value) { + setState(() { + searchListingText = value.toLowerCase(); + print(searchListingText); + }); + }), + decoration: kTextFormFieldDecoration.copyWith( + hintText: "Search for Listings to Borrow", + border: InputBorder.none, + suffixIcon: Icon(Icons.search_rounded)), + )), body: ListView(padding: const EdgeInsets.all(defaultPadding), children: [ Column(crossAxisAlignment: CrossAxisAlignment.start, children: [ @@ -54,22 +59,41 @@ class AllRenting extends StatelessWidget { fontSize: 35, fontWeight: FontWeight.w500), ), const Text( - 'Listings For Borrowing', + 'Listings For Rentiing', style: TextStyle(fontSize: 15.0, color: Colors.blueGrey), ), const SizedBox(height: defaultPadding), - Form( - child: TextFormField( - decoration: kTextFormFieldDecoration.copyWith( - hintText: "Search for Listings to Rent", - prefixIcon: Icon(Icons.search_rounded)), - )), ]), StreamBuilder( - stream: FirebaseFirestore.instance - .collection('listings') - .where('forRent', isEqualTo: true) - .snapshots(), + stream: (searchListingText != "") + ? widget.userEmailToDisplay!.isEmpty || + widget.userEmailToDisplay == null + ? FirebaseFirestore.instance + .collection('listings') + .where('forRent', isEqualTo: true) + .where("searchIndex", + arrayContains: searchListingText.toLowerCase()) + .snapshots() + : FirebaseFirestore.instance + .collection('listings') + .where('createdByEmail', + isEqualTo: widget.userEmailToDisplay) + .where('forRent', isEqualTo: true) + .where("searchIndex", + arrayContains: searchListingText.toLowerCase()) + .snapshots() + : widget.userEmailToDisplay!.isEmpty || + widget.userEmailToDisplay == null + ? FirebaseFirestore.instance + .collection('listings') + .where('forRent', isEqualTo: true) + .snapshots() + : FirebaseFirestore.instance + .collection('listings') + .where('createdByEmail', + isEqualTo: widget.userEmailToDisplay) + .where('forRent', isEqualTo: true) + .snapshots(), builder: (context, AsyncSnapshot>> snapshot) { if (snapshot.connectionState == ConnectionState.waiting) {