searchable_listview 1.0.0 searchable_listview: ^1.0.0 copied to clipboard
A new easy way to filter listview with simple implementation with possibilty to customize search field and empty widget
import 'package:flutter/material.dart';
import 'package:searchable_listview/searchable_listview.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: SafeArea(
child: ExampleApp(),
),
),
);
}
}
class ExampleApp extends StatelessWidget {
final List<Actor> actors = [
Actor(
age: 47,
name: 'Leonardo',
lastName: 'DiCaprio',
),
Actor(
age: 58,
name: 'Johnny',
lastName: 'Depp',
),
Actor(
age: 78,
name: 'Robert',
lastName: 'De Niro'
),
Actor(
age: 44,
name: 'Tom',
lastName: 'Hardy'
),
Actor(
age: 66,
name: 'Denzel',
lastName: 'Washington'
),
Actor(
age: 49,
name: 'Ben',
lastName: 'Affleck'
),
];
ExampleApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return SizedBox(
width: double.infinity,
child: Column(
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.all(15),
child: SearchableList<Actor>(
initialList: actors,
builder: (dynamic user) => UserItem(user: user),
filter: _filterUserList,
emptyWidget: const EmptyView(),
inputDecoration: InputDecoration(
labelText: "Search Actor",
fillColor: Colors.white,
focusedBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: Colors.blue,
width: 1.0,
),
borderRadius: BorderRadius.circular(10.0),
),
),
),
),
)
],
),
);
}
List<Actor> _filterUserList(search) {
return actors
.where(
(element) => element.name.toLowerCase().contains(search),
)
.toList();
}
}
class UserItem extends StatelessWidget {
final Actor user;
const UserItem({
Key? key,
required this.user,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 60,
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(10),
),
child: Row(
children: [
const SizedBox(
width: 10,
),
Icon(Icons.star, color: Colors.yellow[700],),
const SizedBox(
width: 10,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Firstname: ${user.name}',
style: const TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
),
),
Text(
'Lastname: ${user.lastName}',
style: const TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
),
),
Text(
'Age: ${user.age}',
style: const TextStyle(
color: Colors.black,
),
),
],
),
],
),
),
);
}
}
class EmptyView extends StatelessWidget {
const EmptyView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Icon(
Icons.error,
color: Colors.red,
),
Text('no actor is found with this name'),
],
);
}
}
class Actor {
int age;
String name;
String lastName;
Actor({
required this.age,
required this.name,
required this.lastName,
});
}