Here is the code that I used
import 'package:flutter/material.dart';
import 'package:filter_list/filter_list.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp();
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Filter List',
theme: ThemeData(
primarySwatch: Colors.blueGrey,
),
home: FilterPage(),
);
}
}
class User {
final String? name;
final String? avatar;
User({this.name, this.avatar});
}
class FilterPage extends StatelessWidget {
FilterPage({Key? key}) : super(key: key);
final List<User> userList = [
User(name: "Jon", avatar: ""),
User(name: "Ethel ", avatar: ""),
User(name: "Elyse ", avatar: ""),
User(name: "Nail ", avatar: ""),
User(name: "Valarie ", avatar: ""),
User(name: "Lindsey ", avatar: ""),
User(name: "Emelyan ", avatar: ""),
User(name: "Carolina ", avatar: ""),
User(name: "Catherine ", avatar: ""),
User(name: "Stepanida ", avatar: ""),
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Filter List Widget Example "),
),
body: SafeArea(
child: FilterListWidget<User>(
listData: userList,
hideHeaderText: true,
onApplyButtonClick: (list) {
if (list != null) {
print("Selected items count: ${list.length}");
}
},
choiceChipLabel: (item) {
/// Used to print text on chip
return item!.name;
},
validateSelectedItem: (list, val) {
/// identify if item is selected or not
return list!.contains(val);
},
onItemSearch: (list, text) {
/// When text change in search text field then return list containing that text value
///
///Check if list has value which matchs to text
if (list!.any((element) =>
element.name!.toLowerCase().contains(text.toLowerCase()))) {
/// return list which contains matches
return list
.where((element) =>
element.name!.toLowerCase().contains(text.toLowerCase()))
.toList();
}
else{
return [];
}
},
),
),
);
}
}
So is there any way to change instead printing the lengths, but I want to print the value in the selected list for example if I select "Ethel" and "Jon", I need it to print exactly the names. Is there any way to solve it?..............................................................................................................................................................................................
CodePudding user response:
I think this is the supposed answer. You can try this:
if (list != null) {
list.map((value) {
print("name: ${value.name}");
}
} else {
print("List is null");
}
CodePudding user response:
First, why do not use print(list)
? In this case do not forget to override the toString()
method.
String toString() => 'User (name: $name)';
Second, you may create a mapped list of user names and print it.
final selectedItems = list.map((user) => user.name).toList();
print(selectedItems);