Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented the Search Feature in the app #34

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 55 additions & 52 deletions lib/ui/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,48 @@ import 'package:event_app/ui/widgets/card_event_this_month.dart';
import 'package:event_app/ui/widgets/card_popular_event.dart';
import 'package:event_app/ui/widgets/custom_app_bar.dart';
import 'package:event_app/ui/widgets/my_navigation_bar.dart';
import 'package:event_app/ui/widgets/search_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/svg.dart';

import 'view_all_page.dart';


class HomePage extends StatefulWidget {
HomePage({super.key});

@override
_HomePageState createState() => _HomePageState();

}
class _HomePageState extends State<HomePage> {

String address_locality="";
String address_country="";
class _HomePageState extends State<HomePage> {
String address_locality = "";
String address_country = "";

late LocationController _controller;
late locationModel lc;

@override
void initState(){

void initState() {
super.initState();
_controller=LocationController();
lc=locationModel(country: "", locality: "");
_controller = LocationController();
lc = locationModel(country: "", locality: "");
_updateLocation();
}

Future<void> _updateLocation() async{
try{
Future<void> _updateLocation() async {
try {
lc = await _controller.getCurrentLocation();
setState(() {
address_locality = lc.locality;
address_country = lc.country;
});
}catch(e){
} catch (e) {
print('Error updating location: $e');
setState(() {
address_locality="No address";
address_country="";
address_locality = "No address";
address_country = "";
});
}
}
Expand Down Expand Up @@ -95,16 +93,17 @@ class _HomePageState extends State<HomePage> {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const ViewAllPage(isPopularEvent: true,)),
builder: (context) => const ViewAllPage(
isPopularEvent: true,
)),
);
},
child: const Text(
"View All",
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 14,
color: AppColors.primaryColor
),
color: AppColors.primaryColor),
),
)
],
Expand All @@ -123,39 +122,41 @@ class _HomePageState extends State<HomePage> {
},
),
const SizedBox(height: 12),
Divider(height: 1,
endIndent: 16,
indent: 16,
thickness: 0.5,
color: Theme.of(context).disabledColor.withOpacity(0.1),
Divider(
height: 1,
endIndent: 16,
indent: 16,
thickness: 0.5,
color: Theme.of(context).disabledColor.withOpacity(0.1),
),
const SizedBox(height: 8),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 24,vertical: 12),
padding:
const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text(
"Event This Month",
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 18),
fontWeight: FontWeight.w600, fontSize: 18),
),
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const ViewAllPage(isPopularEvent: false,)),
builder: (context) => const ViewAllPage(
isPopularEvent: false,
)),
);
},
child: const Text(
"View All",
style: TextStyle(
fontWeight: FontWeight.w400,
fontSize: 12,
color: AppColors.primaryColor
),
color: AppColors.primaryColor),
),
)
],
Expand Down Expand Up @@ -191,9 +192,9 @@ class _HomePageState extends State<HomePage> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(children: [
Image.asset(
'assets/images/location.png',
Row(children: [
Image.asset(
'assets/images/location.png',
width: 20,
),
const SizedBox(width: 8),
Expand All @@ -206,7 +207,7 @@ class _HomePageState extends State<HomePage> {
),
),
]),
const SizedBox(height: 8),
const SizedBox(height: 8),
Text(
address_locality,
style: const TextStyle(
Expand All @@ -217,7 +218,7 @@ class _HomePageState extends State<HomePage> {
],
),
),
const Spacer(),
const Spacer(),
GestureDetector(
onTap: () {
// Navigate to the specific page here
Expand Down Expand Up @@ -253,18 +254,25 @@ class _HomePageState extends State<HomePage> {
borderRadius: BorderRadius.all(Radius.circular(50)),
color: AppColors.greyLightColor,
),
child: Row(
children: [
Image.asset('assets/images/search.png', width: 16),
const SizedBox(width: 8),
const Text(
"Search event...",
style: TextStyle(
color: AppColors.greyTextColor,
fontWeight: FontWeight.w500
),
)
],
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const MySearchWidget()),
);
},
child: Row(
children: [
Image.asset('assets/images/search.png', width: 16),
const SizedBox(width: 8),
const Text(
"Search event...",
style: TextStyle(
color: AppColors.greyTextColor,
fontWeight: FontWeight.w500),
)
],
),
),
);

Expand All @@ -275,13 +283,8 @@ class _HomePageState extends State<HomePage> {
physics: const BouncingScrollPhysics(),
scrollDirection: Axis.horizontal,
autoplay: true,
pagination: const SwiperPagination(
alignment: Alignment.bottomCenter
),
control: const SwiperControl(
size: 0,
padding: EdgeInsets.all(0)
),
pagination: const SwiperPagination(alignment: Alignment.bottomCenter),
control: const SwiperControl(size: 0, padding: EdgeInsets.all(0)),
itemCount: events.length,
itemBuilder: (context, index) => GestureDetector(
onTap: () => Navigator.pushNamed(
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/widgets/card_event_this_month.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CardEventThisMonth extends StatelessWidget {
return Container(
height: 95,
margin: const EdgeInsets.only(bottom: 12),
padding: const EdgeInsets.symmetric(horizontal: 0, vertical: 8),
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
color: AppColors.whiteColor,
Expand Down
154 changes: 154 additions & 0 deletions lib/ui/widgets/search_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import 'dart:convert';

import 'package:event_app/app/resources/constant/named_routes.dart';
import 'package:event_app/data/event_model.dart';
import 'package:event_app/ui/widgets/card_event_this_month.dart';
import 'package:flutter/material.dart';

void main() {
runApp(const MaterialApp(
debugShowCheckedModeBanner: false,
home: MySearchWidget(),
));
}

class MySearchWidget extends StatefulWidget {
const MySearchWidget({super.key});

@override
// ignore: library_private_types_in_public_api
_MySearchWidgetState createState() => _MySearchWidgetState();
}

class _MySearchWidgetState extends State<MySearchWidget> {
String searchText = '';
late List<EventModel> eventList;
List<EventModel> filteredList = [];

@override
void initState() {
super.initState();
_loadEvents().then((events) {
setState(() {
eventList = events;
});
}).catchError((error) {
Error();
});
}

@override
Widget build(BuildContext context) {
filterEvents(); // Call the function to filter events based on searchText
return SafeArea(
child: Scaffold(
body: Padding(
padding: const EdgeInsets.only(top: 15),
child: Column(
children: [
Container(
height: 48,
width: double.infinity,
margin: const EdgeInsets.symmetric(horizontal: 24),
padding: const EdgeInsets.symmetric(horizontal: 16),
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(
Radius.circular(24)), // Smaller radius
color: Colors.grey[200], // Light grey background
),
child: Row(
children: [
const Padding(
padding: EdgeInsets.all(8.0),
child: Icon(Icons.search,
color: Colors.grey), // Grey search icon
),
const SizedBox(width: 8),
Expanded(
child: TextField(
onChanged: (text) {
setState(() {
searchText = text;
});
},
style: const TextStyle(
color: Colors.black,
fontWeight: FontWeight.w400,
),
decoration: InputDecoration(
hintText: 'Search...',
hintStyle: TextStyle(
color: Colors.grey.withOpacity(0.5),
fontWeight: FontWeight.w400,
),
border: InputBorder.none,
contentPadding: EdgeInsets.zero,
),
),
),
IconButton(
icon: const Icon(Icons.clear,
color: Colors.grey), // Grey clear icon
onPressed: () {
setState(() {
searchText = ''; // Clear search text
Navigator.pop(context);
});
},
),
],
),
),
const SizedBox(height: 15),
const Text("Searched Result"),
const SizedBox(height: 15),
searchText != ""
? Expanded(
child: ListView.builder(
itemCount: filteredList.length,
itemBuilder: (context, index) {
return GestureDetector(
onTap: (() => Navigator.pushNamed(
arguments: eventList[index].toJson(),
context,
NamedRoutes.detailScreen)),
child: CardEventThisMonth(
eventModel: filteredList[index],
),
);
},
),
)
: const Center(child: Text("No Events")),
],
),
),
),
);
}

void filterEvents() {
if (searchText.isEmpty) {
setState(() {
filteredList = []; // If search text is empty, show an empty list
});
} else {
List<EventModel> tempFilteredList = [];
for (var event in eventList) {
if (event.title.toLowerCase().contains(searchText.toLowerCase())) {
print(event.title.toLowerCase());
tempFilteredList.add(event);
}
}
setState(() {
filteredList = tempFilteredList; // Set the filtered
});
}
}

Future<List<EventModel>> _loadEvents() async {
String jsonString = await EventModel.getJson();
List<dynamic> jsonList = json.decode(jsonString);
return jsonList.map((json) => EventModel.fromJson(json)).toList();
}
}
1 change: 1 addition & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import FlutterMacOS
import Foundation

import geolocator_apple
import path_provider_foundation

Expand Down
Loading
Loading