![](/img/trans.png)
[英]How do I access an ImageButton in a different class in Android Studio?
[英]Flutter: How do I access the variable of a different class?
我正在嘗試在我的應用程序中實現一個顯示歌曲的搜索欄,然后用戶可以通過搜索欄找到特定的歌曲。 我將歌曲列表傳遞到我的CustomSearchDelegate class 中,但我無法從buildResults和buildSuggestions 的 for 循環中訪問變量 songTitle 。 相關代碼如下:
import 'package:flutter/material.dart';
class Recordings extends StatelessWidget {
Recordings({Key? key, required this.title}) : super(key: key);
final String title;
//This will be replaced by data from the database
final List<Widget> songs = [
const Song(songTitle: "September"),
const Song(songTitle: "Don't Stop Me Now"),
const Song(songTitle: "Let It Go"),
const Song(songTitle: "Smoke on the Water"),
const Song(songTitle: "Don't Kill My Vibe"),
const Song(songTitle: "Mamma Mia"),
const Song(songTitle: "4'33"),
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
actions: [
IconButton(
onPressed: () {
showSearch(context: context, delegate: CustomSearchDelegate(searchTerms: songs));
},
icon: const Icon(Icons.search)),
],
),
body: Container(
color: Colors.grey[600],
child: Center(
child: ListView(
children: songs,
),
),
),
);
}
}
class Song extends StatelessWidget {
const Song({Key? key, required this.songTitle}) : super(key: key);
final String songTitle;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(10),
child: Container(
alignment: Alignment.center,
color: Colors.lightBlue[300],
child: Text(
songTitle,
style: const TextStyle(color: Colors.black),
),
),
);
}
}
class CustomSearchDelegate extends SearchDelegate {
CustomSearchDelegate({required this.searchTerms});
final List<Widget> searchTerms;
@override
List<Widget> buildActions(BuildContext context) {
return [
IconButton(
onPressed: () {
query = "";
},
icon: const Icon(Icons.clear),
),
];
}
@override
Widget buildLeading(BuildContext context) {
return IconButton(
onPressed: () {
close(context, null);
},
icon: const Icon(Icons.arrow_back),
);
}
@override
Widget buildResults(BuildContext context) {
List<Widget> matchQuery = [];
//looping through each item in searchTerms
for (var song in searchTerms) {
if (song.title.toLowerCase().contains(query.toLowerCase())) { //ERROR OCCURS HERE
matchQuery.add(song);
}
}
return ListView.builder(
itemCount: matchQuery.length,
itemBuilder: ((context, index) {
var result = matchQuery[index].title;
return ListTile(
title: Text(result),
);
}),
);
}
@override
Widget buildSuggestions(BuildContext context) {
List<Widget> matchQuery = [];
//looping through each item in searchTerms
for (var song in searchTerms) {
if (song.title.toLowerCase().contains(query.toLowerCase())) { //ERROR OCCURS HERE
matchQuery.add(song);
}
}
return ListView.builder(
itemCount: matchQuery.length,
itemBuilder: ((context, index) {
var result = matchQuery[index].title;
return ListTile(
title: Text(result),
);
}),
);
}
}
想出了如何解決這個問題。 我在需要訪問songTitle的地方將List<Widget>
更改為List<Song>
。 我還在返回songTitle的Song class 中添加了getSongTitle 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.