I want to filter my list by the name of the title since when I look for the title of the item in my list it has to be with the exact name. This is my ListView, when looking for the title of an item it does not look for it as I want. If there is any way to filter by approximation of the title it would be of great help.
import 'package:flutter/material.dart';
import 'package:flutter_application_1/detailsCG.dart';
import 'package:flutter_application_1/model/cantoscglist.dart';
class ListScreenCG extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title:
Text('Cantos Corario Grande', style: TextStyle(fontSize: 20.0)),
centerTitle: true,
actions: <Widget>[
IconButton(
onPressed: () {
showSearch(context: context, delegate: LetraCGSearch());
},
icon: Icon(Icons.search),
)
],
),
body: ListView.builder(
itemCount: cantosListCG.length,
itemBuilder: (context, index) {
LetraCG letraCG = cantosListCG[index];
return Card(
child: ListTile(
title: Text(letraCG.titleCG),
//subtitle: Text(letra.numero.toString()),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailsCG(letraCG)));
},
),
);
}));
}
}
class LetraCGSearch extends SearchDelegate<LetraCG> {
@override
List<Widget> buildActions(BuildContext context) {
return [
IconButton(
icon: Icon(Icons.clear),
onPressed: () {
query = "";
},
)
];
}
@override
Widget buildLeading(BuildContext context) {
return IconButton(
icon: Icon(Icons.navigate_before),
onPressed: () {
Navigator.pop(context);
},
/*onPressed: () {
//close(context, null);
},
icon: Icon(Icons.arrow_back),*/
);
}
@override
Widget buildResults(BuildContext context) {
return Text(query);
}
@override
Widget buildSuggestions(BuildContext context) {
final mylist = query.isEmpty
? cantosListCG
: cantosListCG
.where((element) =>
element.titleCG.toLowerCase().startsWith(query.toLowerCase()))
.toList();
;
return mylist.isEmpty
? Center(
child: Text(
'Not Results Found...',
style: TextStyle(fontSize: 20),
))
: ListView.builder(
itemCount: mylist.length,
itemBuilder: (context, index) {
final LetraCG letracg = mylist[index];
return ListTile(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailsCG(letracg)));
},
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(letracg.titleCG),
//Text(listitem.letraCG),
Divider()
],
),
);
});
}
}
This is where I have my list with the items and information I want to filter. I would like to know if there is any way to be able to filter it not by the exact name of the title.
import 'dart:ffi';
import 'package:flutter/material.dart';
class LetraCG {
int numeroCG;
String titleCG;
String letraCG;
LetraCG({
required this.numeroCG,
required this.titleCG,
required this.letraCG,
});
}
List<String> cantoslistOnSearch = [];
List<LetraCG> cantosListCG = [
LetraCG(numeroCG: 1, titleCG: 'No nos rendiremos', letraCG: '''Letra'''),
LetraCG(numeroCG: 2, titleCG: '2.- Escondido', letraCG: '''Letra'''),
LetraCG(numeroCG: 4, titleCG: '4.- Humilde', letraCG: '''Letra'''),
LetraCG(numeroCG: 5, titleCG: 'Tacos', letraCG: ''' Letra '''),
];
Thank you very much for your attention
Instead of
startsWith
you could usecontains
:https://api.flutter.dev/flutter/dart-core/String/contains.html