首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在颤振中自动扩展和关注应用程序中的搜索视图

如何在颤振中自动扩展和关注应用程序中的搜索视图
EN

Stack Overflow用户
提问于 2022-05-12 07:19:30
回答 1查看 889关注 0票数 0

现在我必须单击appBar中的搜索栏,然后打开searchView,

我想直接打开searchView,

怎么做?由于showSearch不返回小部件,所以我不能直接将它挂载在脚手架的主体中,

我们将非常感谢您的帮助!

我现在的代码是:

代码语言:javascript
运行
复制
 @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
      title: Text("Search Product"),
      actions: <Widget>[
        IconButton(
          onPressed: () {
            showSearch(context: context, delegate: DataSearch());
          },
          icon: Icon(Icons.search),
        )
      ],
    ));
  }
}

class DataSearch extends SearchDelegate<String> {
  @override
  List<Widget>? buildActions(BuildContext context) {
    return [
      IconButton(
          onPressed: () {
            query = "";
          },
          icon: Icon(Icons.clear))
    ];
  }

  @override
  Widget? buildLeading(BuildContext context) {
    return IconButton(
        onPressed: () {
          close(context, "");
        },
        icon: Icon(Icons.arrow_back));
  }

  @override
  Widget buildResults(BuildContext context) {
    throw UnimplementedError();
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    final suggestionList =
        productsController.products.where((p) => p.name!.toLowerCase().startsWith(query.toLowerCase())).toList();

    return ListView.builder(
      itemCount: query == "" ? 5 : suggestionList.length,
      itemBuilder: (BuildContext context, int index) {
        return ListTile(
            onTap: () {
              Navigator.pushNamed(context, ProductDetails.routeName,
                  arguments: ProductsDetailsArguments(product: suggestionList[index]));
            },
            leading: Icon(Icons.search),
            title: RichText(
                text: TextSpan(
                    text: suggestionList[index].name!.substring(0, query.length),
                    style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
                    children: [
                  TextSpan(
                      text: suggestionList[index].name!.substring(query.length),
                      style: query == "" ? TextStyle(color: Colors.grey) : TextStyle(color: Colors.grey.shade600))
                ])));
      },
    );
  }
}

现在,这些是屏幕:

  1. 2

我想直接转到第二个屏幕

EN

回答 1

Stack Overflow用户

发布于 2022-05-12 09:26:53

当您导航到相应的页面时,尝试使用FocusNode并请求焦点。

颤振TextField FocusNode

有关注释中的引用,请参考此代码作为示例,

代码语言:javascript
运行
复制
TextButton(
      onPressed: () {
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => const Screen2(),
          ),
        );
        showSearch(context: context, delegate: DataSearch());
      },
      child: Text("Navigate to Screen 2"),
    ),
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72211514

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档