Skip to content

Commit

Permalink
feat: Adds search capability
Browse files Browse the repository at this point in the history
  • Loading branch information
abhibhaw committed Jan 26, 2024
1 parent 9007f31 commit 59c96d0
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 90 deletions.
9 changes: 9 additions & 0 deletions milkton_executive/lib/cubit/query/query_cubit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:flutter_bloc/flutter_bloc.dart';

class QueryCubit extends Cubit<String> {
QueryCubit() : super("");

void setQuery(String query) {
emit(query);
}
}
16 changes: 16 additions & 0 deletions milkton_executive/lib/cubit/search/search_cubit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

part 'search_state.dart';

class SearchCubit extends Cubit<SearchState> {
SearchCubit() : super(SearchInitial());

void activateSearch() {
emit(SearchActive());
}

void closeSearch() {
emit(SearchInitial());
}
}
8 changes: 8 additions & 0 deletions milkton_executive/lib/cubit/search/search_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
part of 'search_cubit.dart';

@immutable
sealed class SearchState {}

final class SearchInitial extends SearchState {}

final class SearchActive extends SearchState {}
4 changes: 4 additions & 0 deletions milkton_executive/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import 'package:milkton_executive/configs/graphql_client.dart';
import 'package:milkton_executive/configs/remote_config.dart';
import 'package:milkton_executive/cubit/all_orders/all_orders_cubit.dart';
import 'package:milkton_executive/cubit/auth/auth_cubit.dart';
import 'package:milkton_executive/cubit/query/query_cubit.dart';
import 'package:milkton_executive/cubit/search/search_cubit.dart';
import 'package:milkton_executive/cubit/status/status_cubit.dart';
import 'package:milkton_executive/cubit/user/user_cubit.dart';
import 'package:milkton_executive/firebase_options.dart';
Expand Down Expand Up @@ -34,6 +36,8 @@ class MilktonExecutive extends StatelessWidget {
BlocProvider<StatusCubit>(create: (context) => StatusCubit()),
BlocProvider<UserCubit>(create: (context) => UserCubit()),
BlocProvider<AllOrdersCubit>(create: (context) => AllOrdersCubit()),
BlocProvider<SearchCubit>(create: (context) => SearchCubit()),
BlocProvider<QueryCubit>(create: (context) => QueryCubit()),
],
child: MaterialApp(
title: 'Milkton Executive',
Expand Down
244 changes: 154 additions & 90 deletions milkton_executive/lib/presentation/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/svg.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:milkton_executive/cubit/all_orders/all_orders_cubit.dart';
import 'package:milkton_executive/cubit/query/query_cubit.dart';
import 'package:milkton_executive/cubit/search/search_cubit.dart';
import 'package:milkton_executive/cubit/status/status_cubit.dart';
import 'package:milkton_executive/cubit/user/user_cubit.dart';
import 'package:milkton_executive/graphql/query.dart';
Expand Down Expand Up @@ -39,10 +41,42 @@ class HomeScreen extends StatelessWidget {
appBar: AppBar(
foregroundColor: Colors.white,
backgroundColor: Colors.deepPurple,
title: const Text("Milkton Executive"),
actions: const [
Icon(Icons.search),
SizedBox(width: 12),
title: BlocBuilder<SearchCubit, SearchState>(
builder: (context, state) {
if (state is SearchActive) {
return TextField(
autofocus: true,
decoration: const InputDecoration(
hintText: "Search",
hintStyle: TextStyle(color: Colors.white),
border: InputBorder.none,
),
cursorColor: Colors.white,
style: const TextStyle(color: Colors.white),
onChanged: (value) {
context.read<QueryCubit>().setQuery(value);
});
}
return const Text("Milkton Executive");
},
),
actions: [
BlocBuilder<SearchCubit, SearchState>(
builder: (context, state) {
return IconButton(
onPressed: () {
if (state is SearchActive) {
context.read<QueryCubit>().setQuery("");
context.read<SearchCubit>().closeSearch();
} else {
context.read<SearchCubit>().activateSearch();
}
},
icon: Icon(
state is SearchActive ? Icons.close : Icons.search));
},
),
const SizedBox(width: 12),
],
),
drawer: const TopNavDrawer(),
Expand Down Expand Up @@ -92,95 +126,125 @@ class HomeScreen extends StatelessWidget {
}
context.read<AllOrdersCubit>().getOrders(orders);

return Padding(
padding: const EdgeInsets.all(12.0),
child: SingleChildScrollView(
child: BlocBuilder<StatusCubit, StatusState>(
builder: (context, state) {
var filteredOrders = orders
.where((order) =>
context.read<StatusCubit>().state.title == "ALL"
? true
: context.read<StatusCubit>().state.title ==
order['status'])
.toList();
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: buttonStates
.map(
(buttonState) => Row(
children: <Widget>[
SecondaryButton(
isActive: buttonState["title"] ==
context
.read<StatusCubit>()
.state
.title,
title: buttonState["title"],
count: buttonState["title"] == "ALL"
? orders.length
: orders
.where((order) =>
order['status'] ==
buttonState["title"])
.length,
onPressed: () {
context.read<StatusCubit>().setStatus(
buttonState["state"]
as StatusState);
},
),
const SizedBox(width: 12),
],
),
)
.toList(),
),
),
filteredOrders.isEmpty
? Padding(
padding: const EdgeInsets.all(36.0),
child: Column(
children: [
SvgPicture.asset('assets/images/cart.svg'),
const Text("No orders here",
style: TextStyle(fontSize: 18)),
],
))
: Column(
children: <Widget>[
...orders
.where((order) => context
return BlocBuilder<QueryCubit, String>(
builder: (context, queryState) {
return Padding(
padding: const EdgeInsets.all(12.0),
child: SingleChildScrollView(
child: BlocBuilder<StatusCubit, StatusState>(
builder: (context, state) {
var filteredOrders = orders
.where((order) {
if (queryState.isEmpty) {
return true;
} else {
return order['customer']['firstName']
.toString()
.toLowerCase()
.contains(queryState.toLowerCase()) ||
order['customer']['lastName']
.toString()
.toLowerCase()
.contains(queryState.toLowerCase()) ||
order['customer']['phone']
.toString()
.toLowerCase()
.contains(queryState.toLowerCase()) ||
order['customer']['address']
.toString()
.toLowerCase()
.contains(queryState.toLowerCase());
}
})
.where((order) =>
context.read<StatusCubit>().state.title == "ALL"
? true
: context.read<StatusCubit>().state.title ==
order['status'])
.toList();
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: buttonStates
.map(
(buttonState) => Row(
children: <Widget>[
SecondaryButton(
isActive: buttonState["title"] ==
context
.read<StatusCubit>()
.state
.title,
title: buttonState["title"],
count: buttonState["title"] == "ALL"
? filteredOrders.length
: filteredOrders
.where((order) =>
order['status'] ==
buttonState["title"])
.length,
onPressed: () {
context
.read<StatusCubit>()
.state
.title ==
"ALL"
? true
: context
.read<StatusCubit>()
.state
.title ==
order['status'])
.map(
(order) => Column(
children: [
const SizedBox(height: 12),
OrderCard(
order: Order.fromJson(order)),
],
),
.setStatus(
buttonState["state"]
as StatusState);
},
),
const SizedBox(width: 12),
],
),
],
)
.toList(),
),
],
);
},
),
),
),
filteredOrders.isEmpty
? Padding(
padding: const EdgeInsets.all(36.0),
child: Column(
children: [
SvgPicture.asset(
'assets/images/cart.svg'),
const Text("No orders here",
style: TextStyle(fontSize: 18)),
],
))
: Column(
children: <Widget>[
...filteredOrders
.where((order) => context
.read<StatusCubit>()
.state
.title ==
"ALL"
? true
: context
.read<StatusCubit>()
.state
.title ==
order['status'])
.map(
(order) => Column(
children: [
const SizedBox(height: 12),
OrderCard(
order:
Order.fromJson(order)),
],
),
),
],
),
],
);
},
),
),
);
},
);
},
));
Expand Down

0 comments on commit 59c96d0

Please sign in to comment.