如何找到正确的索引? [英] How can I go to the correct index?
本文介绍了如何找到正确的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我搜索时,它会根据查询列表的索引进行搜索.像这里.因此,它转到错误的页面.我希望该列表根据其自身的索引进行查找.如何获得正确的索引并将其应用于此处:
When I search, it goes according to the index of the query list. Like here. So it goes to the wrong page. I want the list to go according to its own index. How can I get the right index and apply it here:
Navigator.push(context,
MaterialPageRoute(builder: (context) => DetailPage(index)));
SearchDelegate 下的
buildSuggestions:
buildSuggestions under SearchDelegate
:
@override
Widget buildSuggestions(BuildContext context) {
final suggestionList = query.isEmpty
? [" "]
: MyData.TEAM_NAME
.where((p) => p.toLowerCase().contains(query.toLowerCase()))
.toList();
return ListView.builder(
itemBuilder: (context, index) => ListTile(
onTap: () {
close(context, suggestionList[index]);
Navigator.push(context,
MaterialPageRoute(builder: (context) => DetailPage(index)));
},
title: Text(suggestionList[index]),
),
itemCount: suggestionList.length,
);
}
我的数据列表:
class MyData {
static const List<String> TEAM_NAME = [
"team1 name",
"team2 name",
"abc Team",
"xyz"
];
static const List<String> INFO1 = [
"team 1 first info",
"team2 info1",
"abc's info",
"xyz's info1"
];
static const List<String> INFO2 = [
"team 1 second info",
"team2 info2",
"abc's info 2",
"xyz's info2"
];
}
DetailPage
DetailPage
class DetailPage extends StatelessWidget {
int incomingIndex;
TeamModel chosenTeam;
DetailPage(this.incomingIndex);
@override
Widget build(BuildContext context) {
chosenTeam = TeamList.allTeams[incomingIndex];
return DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
title: Text(chosenTeam.teamName),
bottom: TabBar(tabs: [
Tab(
text: "tab1",
),
Tab(
text: "tab2",
),
]),
),
body: TabBarView(
children: [
SingleChildScrollView(
child: Center(
child: Text(
chosenTeam.info,
style: TextStyle(fontSize: 33),
))),
SingleChildScrollView(
child: Center(
child: Text(
chosenTeam.info2,
style: TextStyle(fontSize: 33),
))),
],
)),
);
}
}
推荐答案
MyData.TEAM_NAME.where((p)=> p.toLowerCase().contains(query.toLowerCase())).toList();
创建新的列出
,其中包含找到的元素,这就是为什么索引不对应的原因.
MyData.TEAM_NAME.where((p) => p.toLowerCase().contains(query.toLowerCase())).toList();
creates a new List
with found elements in it, that's why the indicies are not corresponding.
您可以这样更改代码:
@override
Widget buildSuggestions(BuildContext context) {
final suggestionList = query.isEmpty
? [" "]
: MyData.TEAM_NAME
.where((p) => p.toLowerCase().contains(query.toLowerCase()))
.toList();
return ListView.builder(
itemBuilder: (context, index) => ListTile(
onTap: () {
close(context, suggestionList[index]);
Navigator.push(context,
MaterialPageRoute(builder: (context) => DetailPage( MyData.TEAM_NAME.indexWhere((item)=> item == suggestionList[index]) )));
},
title: Text(suggestionList[index]),
),
itemCount: suggestionList.length,
);
}
这篇关于如何找到正确的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文