English 中文(简体)
挥发性显示器
原标题:Flutter showSearch gesture exception
  • 时间:2023-06-14 02:35:20
  •  标签:
  • flutter

将需要一些建议。 我试图在 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;
          },
        );
      },
    );
  }
}
问题回答

单独 你的法典就是这样,那么你的问题就会固定下来。 我假定这种情况正在发生,因为你正在使用材料,而不是家庭背景。

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: Home());
  }
}

class Home extends StatelessWidget {
  const Home({super.key});

  @override
  Widget build(BuildContext context) {
    return 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(),
      ),
    );
  }
}

我不知道你们的主页上的内容,如果说其布局不使用一只耳光,就意味着会激起这种情绪,把所有植被带回家。





相关问题
Flutter App cannot be installed on Android TV

I m building a Flutter app that should support Android TV and Mobile devices. Despite Google Play shows that it is supported, I cannot install app on my MiBox device. While trying to install it, both ...

Moving the icon button to the right causes rendering issues

I am trying to align the icon button to the right of the Text field, I tried the row which made the icon button not centered vertically and now I am trying out the Stack widget. Here is my code - ...

热门标签