将需要一些建议。 我试图在 app巴尔实施陈列,并在其中设有一个带有搜索力的SearchDelegate()级。 我的法典没有错误,但当我操作时,在搜寻纽扣时,它显示:
══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════ The following assertion was thrown while handling a gesture: Navigator operation requested with a context that does not include a Navigator. The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.
我的守则主要在这里。 d)
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("Raincheck"),
actions: [
IconButton(
icon: const Icon(Icons.search),
onPressed: () {
showSearch(
context: context,
delegate: LocationSearchDelegate(),
);
},
),
// IconButton(
// onPressed: () {},
// icon: const Icon(Icons.settings),
// )
],
),
body: const Center(
child: HomePage(),
),
),
);
}
}
这里是我的所在地点。
class LocationSearchDelegate extends SearchDelegate {
List<String> suggestions = [
"USA",
"China",
"Australia",
];
@override
List<Widget>? buildActions(BuildContext context) => [
IconButton(
icon: const Icon(Icons.clear),
onPressed: () {
query = "";
})
];
@override
Widget? buildLeading(BuildContext context) => IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () => close(context, null));
@override
Widget buildResults(BuildContext context) {
List<String> matchQuery = [];
for (var suggestion in suggestions) {
if (suggestion.toLowerCase().contains(query.toLowerCase())) {
matchQuery.add(suggestion);
}
}
return ListView.builder(
itemCount: matchQuery.length,
itemBuilder: (context, index) {
var result = matchQuery[index];
return ListTile(
title: Text(result),
);
},
);
}
@override
Widget buildSuggestions(BuildContext context) {
return ListView.builder(
itemCount: suggestions.length,
itemBuilder: (context, index) {
final suggestion = suggestions[index];
return ListTile(
title: Text(suggestion),
onTap: () {
query = suggestion;
},
);
},
);
}
}