簡體   English   中英

Flutter:搜索代理黑屏

[英]Flutter: Search Delegate black screen

我使用搜索委托創建了一個搜索選項。 搜索委托中的一切都很好。 但是當我按下鍵盤上的搜索按鈕時,它顯示了一個黑屏。 為什么會有這樣的反應? 我該如何解決?

搜索代表演示

這是我的主頁。搜索圖標的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());
              }
          }
        });
  }
}

這是因為這段代碼。

  @override
  Widget buildResults(BuildContext context) {
    return Container(
      child: null,
      color: Colors.black,
    );
  }

從文檔

buildResults(BuildContext context) → Widget 用戶從搜索頁面提交搜索后顯示的結果。

參考

要解決此問題,請確保返回結果而不是黑色容器。 您可能想要添加一個ListView並用數據填充它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM