Last active
August 18, 2024 02:46
-
-
Save rdbhandari/56daf5d2def0ade11bed0d6e811af7a1 to your computer and use it in GitHub Desktop.
Search Delegate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:electronics_app/data/eletronicsData/priceRequest/modelSearch/models/model_search_model.dart'; | |
| import 'package:electronics_app/utils/colors.dart'; | |
| import 'package:electronics_app/view/screens/priceRequest/price_request_view_model.dart'; | |
| import 'package:flutter/material.dart'; | |
| import 'package:flutter_screenutil/flutter_screenutil.dart'; | |
| class ModelSearchDelegate extends SearchDelegate { | |
| final PriceRequestViewModel? viewModel; | |
| final int? compId; | |
| final int? brandId; | |
| final int? productId; | |
| final int? appuserId; | |
| ModelSearchDelegate( | |
| {required this.compId, | |
| required this.brandId, | |
| required this.productId, | |
| required this.appuserId, | |
| required this.viewModel}):super (searchFieldLabel: "Click here to search", ); | |
| @override | |
| ThemeData appBarTheme(BuildContext context) { | |
| return ThemeData( | |
| appBarTheme: AppBarTheme( | |
| toolbarHeight:50.h, | |
| color: kPrimaryColor, | |
| actionsIconTheme: IconThemeData(color: kWhite), | |
| iconTheme: IconThemeData(color: kWhite), | |
| ), | |
| textSelectionTheme: const TextSelectionThemeData( | |
| cursorColor: Colors.black, | |
| ), | |
| textTheme: const TextTheme( | |
| titleLarge: TextStyle( | |
| decorationThickness: 0.0000001, | |
| color: Colors.black, | |
| fontFamily: "Montserrat" | |
| ), | |
| ), | |
| // inputDecorationTheme: InputDecorationTheme( | |
| // border: InputBorder.none, | |
| // hintStyle: Theme.of(context).textTheme.titleLarge?.copyWith(color: kWhite), | |
| // ), | |
| inputDecorationTheme: InputDecorationTheme( | |
| isDense:true, | |
| filled:true, | |
| border: InputBorder.none, | |
| fillColor: Colors.transparent, | |
| constraints: BoxConstraints(maxHeight: 30.h,), | |
| hintStyle: TextStyle( | |
| color: Colors.black, | |
| fontSize: 12.sp, | |
| fontWeight: FontWeight.w400, | |
| ), | |
| // enabledBorder: const OutlineInputBorder( | |
| // borderSide: BorderSide(color: kWhite), | |
| // borderRadius : BorderRadius.all(Radius.circular(15.0)), | |
| // ), | |
| // focusedBorder: const OutlineInputBorder( | |
| // borderSide: BorderSide(color: kBlack), | |
| // borderRadius : BorderRadius.all(Radius.circular(15.0)), | |
| // ), | |
| ), | |
| ); | |
| } | |
| @override | |
| Widget? buildFlexibleSpace(BuildContext context) { | |
| return Container( | |
| margin: EdgeInsets.only(top: 38.h, bottom: 9.h, left: 50.w, right: 10.w), | |
| height: 70.h, | |
| decoration: BoxDecoration(borderRadius: BorderRadius.circular(50), border: Border.all(width: 2 ,color: kWhite), color: kWhite), | |
| ); | |
| // return super.buildFlexibleSpace(context); | |
| } | |
| @override | |
| List<Widget>? buildActions(BuildContext context) { | |
| return [ | |
| IconButton( | |
| icon: query!=''? Icon(Icons.clear,color: kBlack,):Icon(Icons.search,color: kBlack,), | |
| onPressed: () { | |
| query!=''? query = '':null; | |
| }, | |
| ), | |
| SizedBox(width: 10.w,), | |
| // IconButton( | |
| // icon: const Icon(Icons.clear), | |
| // onPressed: () { | |
| // query = ''; | |
| // }, | |
| // ), | |
| ]; | |
| } | |
| @override | |
| Widget? buildLeading(BuildContext context) { | |
| return IconButton( | |
| icon: const Icon(Icons.arrow_back_ios), | |
| onPressed: () => Navigator.of(context).pop(), | |
| // Exit from the search screen. | |
| ); | |
| } | |
| @override | |
| Widget buildResults(BuildContext context) { | |
| if (query.length >= 3) { | |
| return FutureBuilder( | |
| future: viewModel?.searchModel( | |
| ModelSearchRequest( | |
| compid: compId, | |
| brandid: brandId, | |
| productid: productId, | |
| appuserid: appuserId, | |
| modelhint: query, | |
| ), | |
| ), | |
| builder: (context, snapshot) { | |
| if (snapshot.hasData) { | |
| final List<Model>? searchResults = snapshot.data; | |
| return (searchResults?.isEmpty ?? true) ? const Center(child: Text("No Result Found")) : ListView.builder( | |
| physics:const BouncingScrollPhysics(), | |
| itemCount: searchResults?.length, | |
| itemBuilder: (context, index) { | |
| return Column( | |
| children: [ | |
| ListTile( | |
| leading: TextButton( | |
| style: TextButton.styleFrom( | |
| padding: EdgeInsets.zero, | |
| tapTargetSize: MaterialTapTargetSize.shrinkWrap, | |
| backgroundColor: kPrimaryColor.withOpacity(0.2), | |
| shape: const CircleBorder(), | |
| ), | |
| child: const Icon( | |
| Icons.search, | |
| color: kBlack, | |
| ), | |
| onPressed: () {}, | |
| ), | |
| title: Text("${searchResults?[index].modelname}",style: const TextStyle( | |
| color: kBlack, | |
| fontFamily: "Montserrat", | |
| fontWeight: FontWeight.bold, | |
| fontSize: 14, | |
| ),), | |
| subtitle: Text("${searchResults?[index].pclsname}",style: const TextStyle( | |
| color: kBlack, | |
| fontFamily: "Montserrat", | |
| fontSize: 12, | |
| ),), | |
| trailing: Text("${searchResults?[index].mrp}",style: const TextStyle( | |
| color: kBlack, | |
| fontFamily: "Montserrat", | |
| fontSize: 12, | |
| ),), | |
| onTap: () { | |
| close(context, searchResults?[index]); | |
| }, | |
| ), | |
| const Divider(thickness: 0.3,), | |
| ], | |
| ); | |
| }); | |
| } else { | |
| return const Center( | |
| child: CircularProgressIndicator( | |
| color: kPrimaryColor, | |
| )); | |
| } | |
| }, | |
| ); | |
| } else { | |
| return const SizedBox(); | |
| } | |
| } | |
| @override | |
| Widget buildSuggestions(BuildContext context) { | |
| if (query.length >= 3) { | |
| return FutureBuilder( | |
| future: viewModel?.searchModel( | |
| ModelSearchRequest( | |
| compid: compId, | |
| brandid: brandId, | |
| productid: productId, | |
| appuserid: appuserId, | |
| modelhint: query, | |
| ), | |
| ), | |
| builder: (context, snapshot) { | |
| if (snapshot.hasData) { | |
| final List<Model>? searchResults = snapshot.data; | |
| return (searchResults?.isEmpty ?? true) ? const Center(child: Text("No Result Found")) : ListView.builder( | |
| physics:const BouncingScrollPhysics(), | |
| itemCount: searchResults?.length, | |
| itemBuilder: (context, index) { | |
| return Column( | |
| children: [ | |
| ListTile( | |
| leading: TextButton( | |
| style: TextButton.styleFrom( | |
| padding: EdgeInsets.zero, | |
| tapTargetSize: MaterialTapTargetSize.shrinkWrap, | |
| backgroundColor: kPrimaryColor.withOpacity(0.2), | |
| shape: const CircleBorder(), | |
| ), | |
| child: const Icon( | |
| Icons.search, | |
| color: kBlack, | |
| ), | |
| onPressed: () {}, | |
| ), | |
| title: Text("${searchResults?[index].modelname}",style: const TextStyle( | |
| color: kBlack, | |
| fontFamily: "Montserrat", | |
| fontWeight: FontWeight.bold, | |
| fontSize: 14, | |
| ),), | |
| subtitle: Text("${searchResults?[index].pclsname}",style: const TextStyle( | |
| color: kBlack, | |
| fontFamily: "Montserrat", | |
| fontSize: 12, | |
| ),), | |
| trailing: Text("${searchResults?[index].mrp}",style: const TextStyle( | |
| color: kBlack, | |
| fontFamily: "Montserrat", | |
| fontSize: 12, | |
| ),), | |
| onTap: () { | |
| close(context, searchResults?[index]); | |
| }, | |
| ), | |
| const Divider(thickness: 0.3,), | |
| ], | |
| ); | |
| }); | |
| } else { | |
| return const Center( | |
| child: CircularProgressIndicator( | |
| color: kPrimaryColor, | |
| )); | |
| } | |
| }, | |
| ); | |
| } else { | |
| return const SizedBox(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum SearchType { Type1, Type2 } | |
| class LocalDataSearchDelegate extends SearchDelegate { | |
| @override | |
| ThemeData appBarTheme(BuildContext context) { | |
| return ThemeData( | |
| appBarTheme: AppBarTheme( | |
| toolbarHeight:50.h, | |
| color: kPrimaryColor, | |
| actionsIconTheme: const IconThemeData(color: kWhite,), | |
| iconTheme: const IconThemeData(color: kWhite), | |
| ), | |
| textSelectionTheme: const TextSelectionThemeData( | |
| cursorColor: kBlack, | |
| ), | |
| textTheme: const TextTheme( | |
| titleLarge: TextStyle( | |
| decorationThickness: 0.0000001, | |
| color: kBlack, | |
| fontFamily: "Montserrat", | |
| ), | |
| ), | |
| inputDecorationTheme: InputDecorationTheme( | |
| isDense:true, | |
| filled:true, | |
| border: InputBorder.none, | |
| fillColor: Colors.transparent, | |
| constraints: BoxConstraints(maxHeight: 30.h,), | |
| hintStyle: TextStyle( | |
| color: Colors.black, | |
| fontSize: 12.sp, | |
| fontWeight: FontWeight.w400, | |
| ), | |
| ), | |
| ); | |
| } | |
| final List<dynamic> searchList; | |
| final SearchType searchtype; | |
| LocalDataSearchDelegate({required this.searchtype, required this.searchList}):super (searchFieldLabel: "Click here to search", );//⌕ | |
| @override | |
| Widget? buildFlexibleSpace(BuildContext context) { | |
| return Container( | |
| margin: EdgeInsets.only(top: 38.h, bottom: 9.h, left: 50.w, right: 10.w), | |
| height: 70.h, | |
| decoration: BoxDecoration(borderRadius: BorderRadius.circular(50), border: Border.all(width: 2 ,color: kWhite), color: kWhite), | |
| ); | |
| } | |
| @override | |
| List<Widget>? buildActions(BuildContext context) { | |
| return [ | |
| IconButton( | |
| icon: query!=''? Icon(Icons.clear,color: kBlack,):Icon(Icons.search,color: kBlack,), | |
| onPressed: () { | |
| query!=''? query = '':null; | |
| }, | |
| ), | |
| SizedBox(width: 10.w,), | |
| ]; | |
| } | |
| @override | |
| Widget? buildLeading(BuildContext context) { | |
| return | |
| IconButton( | |
| icon: const Icon(Icons.arrow_back_ios), | |
| onPressed: () => Navigator.of(context).pop(), | |
| ); | |
| } | |
| @override | |
| Widget buildResults(BuildContext context) { | |
| final List<dynamic> searchResults = searchResult(); | |
| if (searchResults.isEmpty) { | |
| return const Center( | |
| child: Text("No result found"), | |
| ); | |
| } | |
| return ListView.builder( | |
| itemCount: searchResults.length, | |
| itemBuilder: (context, index) { | |
| return listTile(context, searchResults[index]); | |
| }, | |
| ); | |
| } | |
| @override | |
| Widget buildSuggestions(BuildContext context) { | |
| final List<dynamic> searchResults = searchResult(); | |
| if (searchResults.isEmpty) { | |
| return const Center( | |
| child: Text("No result found"), | |
| ); | |
| } | |
| return ListView.builder( | |
| physics:const BouncingScrollPhysics(), | |
| itemCount: searchResults.length, | |
| itemBuilder: (context, index) { | |
| return listTile(context, searchResults[index]); | |
| }, | |
| ); | |
| } | |
| List<dynamic> searchResult() { | |
| List<dynamic> searchResult = []; | |
| if (query.isEmpty) { | |
| return searchList; | |
| } | |
| if (searchtype == SearchType.Type1) { | |
| searchResult = searchList.where((item) => item.Type1name!.toLowerCase().contains(query.toLowerCase()),).toList(); | |
| } else if (searchtype == SearchType.Type2) { | |
| searchResult = searchList.where((item) => item.productType2name!.toLowerCase().contains(query.toLowerCase()),).toList(); | |
| } | |
| return searchResult; | |
| } | |
| Widget listTile(BuildContext context, dynamic data) { | |
| if (searchtype == SearchType.Type1) { | |
| return Column( | |
| children: [ | |
| ListTile( | |
| leading: TextButton( | |
| style: TextButton.styleFrom( | |
| padding: EdgeInsets.zero, | |
| tapTargetSize: MaterialTapTargetSize.shrinkWrap, | |
| backgroundColor: kPrimaryColor.withOpacity(0.2), | |
| shape: const CircleBorder(), | |
| ), | |
| child: const Icon( | |
| Icons.search, | |
| color: kBlack, | |
| ), | |
| onPressed: () {}, | |
| ), | |
| title: Text("${data.Type1name}",style: const TextStyle( | |
| color: kBlack, | |
| fontFamily: "Montserrat", | |
| fontWeight: FontWeight.bold, | |
| fontSize: 14, | |
| ),), | |
| onTap: () { | |
| close(context, data); | |
| }, | |
| ), | |
| const Divider(thickness: 0.3,), | |
| ], | |
| ); | |
| } | |
| if (searchtype == SearchType.Type2) { | |
| return Column( | |
| children: [ | |
| ListTile( | |
| leading: TextButton( | |
| style: TextButton.styleFrom( | |
| padding: EdgeInsets.zero, | |
| tapTargetSize: MaterialTapTargetSize.shrinkWrap, | |
| backgroundColor: kPrimaryColor.withOpacity(0.2), | |
| shape: const CircleBorder(), | |
| ), | |
| child: const Icon( | |
| Icons.search, | |
| color: kBlack, | |
| ), | |
| onPressed: () {}, | |
| ), | |
| title: Text("${data.productType2name}",style: const TextStyle( | |
| color: kBlack, | |
| fontFamily: "Montserrat", | |
| fontWeight: FontWeight.bold, | |
| fontSize: 14 | |
| ),), | |
| onTap: () { | |
| close(context, data); | |
| }, | |
| ), | |
| const Divider(thickness: 0.3,), | |
| ], | |
| ); | |
| } | |
| return const SizedBox(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment