Reputation:
Here is the code for my search bar, whenever i enter some value in the search bar i want it to show items available on the list which matches the entered value.
class HeaderWithSearchBox extends StatelessWidget {
const HeaderWithSearchBox({
Key key,
@required this.size,
}) : super(key: key);
final Size size;
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(bottom: kDefaultPadding * 1.5),
// It will cover 20% of our total height
// height: size.height * 0.2,
child: Stack(
children: <Widget>[
Container(
padding: EdgeInsets.only(
left: kDefaultPadding,
right: kDefaultPadding,
bottom: 36 + kDefaultPadding,
),
//height: size.height * 0.2 - 27,
decoration: BoxDecoration(
//color: kPrimaryColor,
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(36),
bottomRight: Radius.circular(36),
),
),
),
Positioned(
bottom: 0,
left: 0,
right: 0,
top: 20,
child: Container(
alignment: Alignment.center,
margin: EdgeInsets.symmetric(horizontal: kDefaultPadding),
padding: EdgeInsets.symmetric(horizontal: kDefaultPadding),
height: 54,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
offset: Offset(0, 10),
blurRadius: 50,
color: kPrimaryColor.withOpacity(0.23),
),
],
),
child: Row(
children: <Widget>[
Expanded(
child: Center(
child: TextField(
onChanged: (value) {
},
decoration: InputDecoration(
hintText: "Search",
hintStyle: TextStyle(
color: kPrimaryColor.withOpacity(0.5),
),
enabledBorder: InputBorder.none,
focusedBorder: InputBorder.none,
),
),
),
),
Icon(Icons.search),
],
),
),
),
],
),
);
}
}
whenever i enter some value in the search bar i want it to show items available on the list which matches the entered value. when the item is clicked i want to open new page, how can i do that? Any help will be appreciated.
Upvotes: 0
Views: 11045
Reputation: 60
SearchBar
widget has been added to the material package
https://master-api.flutter.dev/flutter/material/SearchBar-class.html
https://github.com/flutter/flutter/pull/122309
Upvotes: 0
Reputation: 34180
Use Dependency:
dependencies:
flappy_search_bar: ^1.7.2
Code:
import 'dart:math';
import 'package:flappy_search_bar/flappy_search_bar.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Home(),
);
}
}
class Post {
final String title;
final String body;
Post(this.title, this.body);
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
final SearchBarController<Post> _searchBarController = SearchBarController();
bool isReplay = false;
Future<List<Post>> _getALlPosts(String text) async {
List<Post> posts = [];
var random = new Random();
for (int i = 0; i < 10; i++) {
posts
.add(Post("$text $i", "body random number : ${random.nextInt(100)}"));
}
return posts;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: SearchBar<Post>(
minimumChars: 1,
searchBarPadding: EdgeInsets.symmetric(horizontal: 10),
headerPadding: EdgeInsets.symmetric(horizontal: 10),
listPadding: EdgeInsets.symmetric(horizontal: 10),
onSearch: _getALlPosts,
searchBarController: _searchBarController,
placeHolder: Center(
child: Text(
"PlaceHolder",
style: TextStyle(fontSize: 30),
)),
cancellationWidget: Text("Cancel"),
emptyWidget: Text("empty"),
onCancelled: () {
print("Cancelled triggered");
},
mainAxisSpacing: 10,
onItemFound: (Post post, int index) {
return Container(
color: Colors.lightBlue,
child: ListTile(
title: Text(post.title),
isThreeLine: true,
subtitle: Text(post.body),
onTap: () {
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => Detail()));
},
),
);
},
),
),
);
}
}
class Detail extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: SafeArea(
child: Center(child: Text("Detail", style: TextStyle(fontSize: 30),)),
),
);
}
}
Output:
Upvotes: 0
Reputation: 2218
This works
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({
Key key,
}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List fooList = ['one', 'two', 'three', 'four', 'five'];
List filteredList = List();
@override
void initState() {
super.initState();
filteredList = fooList;
}
void filter(String inputString) {
filteredList =
fooList.where((i) => i.toLowerCase().contains(inputString)).toList();
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Column(
children: [
TextField(
decoration: InputDecoration(
hintText: 'Search ',
hintStyle: TextStyle(
fontSize: 14,
),
),
onChanged: (text) {
text = text.toLowerCase();
filter(text);
},
),
Expanded(
child: ListView.builder(
itemCount: filteredList.length,
itemBuilder: (BuildContext context, int index) => ListTile(
title: Text(filteredList[index]),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Display(
text: filteredList[index],
),
),
);
},
),
),
)
],
),
);
}
}
class Display extends StatelessWidget {
final String text;
const Display({Key key, this.text}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(text),
),
);
}
}
Upvotes: 1
Reputation: 195
Usually I use blocs for the filtering part and any implementing stuff to make this page just for design but this is just for explanation.
First you should filter list on change assuming x is the list then it should be like this:
TextField(
onChanged: (value) {
x = x.where((i) => x.contains(value))
},
decoration: InputDecoration(
hintText: "Search",
hintStyle: TextStyle(
color: kPrimaryColor.withOpacity(0.5),
),
enabledBorder: InputBorder.none,
focusedBorder: InputBorder.none,
),
then you should add a container to show filtered list:
Container(
child: ListView.builder(
itemCount: x.lenghth,
itemBuilder: (context, index) {
return Text(x[index]);
}
)
),
Upvotes: 0