Created
August 3, 2020 12:09
-
-
Save JaiAravindh/1e1928903e41b84bb822188a97ec8f5b to your computer and use it in GitHub Desktop.
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:flutter/material.dart'; | |
import 'package:flutter/rendering.dart'; | |
void main(){ | |
runApp(new MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: new BookSearch(), | |
)); | |
} | |
class BookSearch extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text("search App"), | |
actions : <Widget>[ | |
IconButton(icon : Icon(Icons.search), onPressed : (){ | |
showSearch(context: context, delegate: DataSearch()); | |
}) | |
] | |
), | |
drawer: Drawer(), | |
); | |
} | |
} | |
class DataSearch extends SearchDelegate<String>{ | |
final books = [ | |
'The Alchemist', | |
"Veronica Decides to Die", | |
"Winner Stands Alone", | |
"Eleven Minutes", | |
"Brida", | |
"By the River Piedra", | |
"Manuscript", | |
"Maktub", | |
"The Zahir", | |
"The Witch of Portobello", | |
"Da vinci code", | |
"Angels and demons" | |
"Origin", | |
"Digital Fortress" | |
]; | |
//hard coded searches | |
final recentBooks = [ | |
"Brida", | |
"By the River Piedra", | |
"Manuscript", | |
"Maktub", | |
]; | |
@override | |
List<Widget> buildActions(BuildContext context) { | |
// actions for AppBar | |
return [ | |
IconButton(icon: Icon(Icons.clear), onPressed: () { | |
query = " "; | |
}), | |
]; | |
} | |
@override | |
Widget buildLeading(BuildContext context) { | |
// leading icon on left of the Appbar | |
return IconButton( | |
icon: AnimatedIcon( | |
icon: AnimatedIcons.menu_arrow, | |
progress : transitionAnimation | |
), | |
onPressed: (){ | |
close(context, null); | |
} | |
); | |
} | |
@override | |
Widget buildResults(BuildContext context) { | |
// Tshow result based on search selection | |
return Card( | |
color: Colors.red, | |
shape: StadiumBorder(), | |
child : Center(child: Text(query), | |
) | |
); | |
} | |
@override | |
Widget buildSuggestions(BuildContext context) { | |
// show search | |
final suggestionList = query.isEmpty ? recentBooks : books.where((p) => p.startsWith(query)).toList(); | |
return ListView.builder( | |
itemBuilder: (context, index) => ListTile( | |
onTap: (){ | |
showResults(context); | |
}, | |
leading: Icon(Icons.location_city), | |
title: RichText( | |
text: TextSpan( | |
text: suggestionList[index].substring(0,query.length), | |
style: TextStyle(color: Colors.black,fontWeight : FontWeight.bold), | |
children: [TextSpan(text: suggestionList[index].substring(query.length), | |
style : TextStyle(color : Colors.grey)) | |
] | |
) | |
), | |
), | |
itemCount: suggestionList.length, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment