我使用搜索委托创建了一个搜索选项。搜索委托一切都很好。但是,当我按下键盘上的搜索按钮时,它显示了黑屏。为什么会这样反应?而我该如何解决呢?
这是我的homepage.dart搜索图标代码-
FutureBuilder(
future: fetchBooks(),
builder: (context, snapshot) {
return IconButton(
icon: Icon(
Icons.search,
color: _whiteCream,
),
onPressed: () async {
var booksSearchData = snapshot.data
.where((b) =>
b.category == 1 ||
b.category == 3 ||
b.category == 8 ||
b.category == 9 ||
b.category == 10 ||
b.category == 11 ||
b.category == 12)
.toList();
final Book result = await showSearch(
context: context,
delegate: BooksSearch(booksSearchData));
Scaffold.of(context)
.showSnackBar(SnackBar(content: Text(result.name)));
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => result.category == 1
? BookDescription(storyBooksValue: result)
: PdfScreen(singleBookData: result)),
);
},
);
}),
这是我的搜索代表代码-
import 'package:flutter/material.dart';
import 'package:boimarket/model/model.dart';
class BooksSearch extends SearchDelegate<Book> {
BooksSearch(this.allBooksData);
final List<Book> allBooksData;
@override
List<Widget> buildActions(BuildContext context) {
return [
SizedBox(
width: 5.0,
),
IconButton(
icon: Icon(Icons.clear),
onPressed: () {
query = '';
},
)
];
}
@override
Widget buildLeading(BuildContext context) {
return IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
close(context, null);
},
);
}
@override
Widget buildResults(BuildContext context) {
return Container(
child: null,
color: Colors.black,
);
}
@override
Widget buildSuggestions(BuildContext context) {
return FutureBuilder(
future: fetchBooks(),
builder: (context, AsyncSnapshot<List<Book>> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return Text('Check Your Internet connection');
case ConnectionState.waiting:
return Center(
child: Text('Please Wait'),
);
case ConnectionState.active:
return Text('');
case ConnectionState.done:
if (!snapshot.hasData) {
return Center(
child: Text('No Data'),
);
} else if (snapshot.hasError) {
print(snapshot.error);
return Text('${snapshot.error}');
} else {
print("query $query");
final List<Book> result = allBooksData
.where((a) =>
a.name.toLowerCase().contains(query.toLowerCase()) ||
a.author.toLowerCase().contains(query.toLowerCase()) ||
a.genreClass
.toLowerCase()
.contains(query.toLowerCase()))
.toList();
result.sort((a, b) => a.name.compareTo(b.name));
return ListView(
children: result
.map<ListTile>((a) => ListTile(
leading: FadeInImage.assetNetwork(
fadeOutCurve: Curves.easeInCubic,
placeholder: 'assets/images/bookshelf.jpg',
image: a.imgUrl == null
? 'assets/images/bookshelf.jpg'
: a.imgUrl,
fit: BoxFit.cover,
),
title: Text(
a.name,
overflow: TextOverflow.fade,
),
subtitle: Text(
a.author,
overflow: TextOverflow.visible,
),
trailing: Text(
a.genreClass,
overflow: TextOverflow.clip,
),
onTap: () {
close(context, a);
},
))
.toList());
}
}
});
}
}