微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Flutter 中访问 AppBarTextField 的搜索按钮?

如何解决如何在 Flutter 中访问 AppBarTextField 的搜索按钮?

我无法访问 AppBarTextField 小部件的搜索按钮;我在下面给出了小部件;

AppBarTextField(
          leading: IconButton(
              onpressed: () {
                Get.back();
              },icon: Icon(Icons.arrow_back_ios)),iconTheme: IconThemeData(color: Colors.black),searchButtonIcon: Icon(Icons.search),centerTitle: true,defaultHintText: "ara",searchContainerColor: Colors.white,style: GoogleFonts.montserrat(color: Colors.black),backgroundColor: Colors.white,autofocus: false,controller: searchController,title: Text(
            "Müşteri Listesi",),elevation: 0,onBackpressed: _onRestoreAllData,onChanged: _onSearchChanged,);

这就是我的意思;

enter image description here

我做了什么;

searchButtonIcon: IconButton(
          onpressed: () {
            print("my value");
          },icon: Icon(Icons.search)),

我确实喜欢上面,它有效。当我按下搜索按钮时,它会打印我的值,但它不会执行其主要功能,例如它不会打开文本字段进行输入。

解决方法

您需要为您的搜索创建自定义搜索委托。下面的例子显示帮助你

class CustomSearchDelegate extends SearchDelegate {
  @override
  List<Widget> buildActions(BuildContext context) {
    return [
      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) {
    if (query.length < 3) {
      return Column(
        mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
          Center(
            child: Text(
              "Search term must be longer than two letters.",)
        ],);
    }
    
return ListView(
children:[
// SEARCH RESULTS HERE
]);
    
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    // This method is called everytime the search term changes. 
    // If you want to add search suggestions as the user enters their search term,this is the place to do that.
    return Column();
  }
}

然后在你的 IconButton 中调用这个 customSearchDelegate

IconButton(
  icon: Icon(Icons.search),onPressed: () {
    showSearch(
      context: context,delegate: CustomSearchDelegate(),);
  },
,

这是我如何做我想做的;

AppBar(
      leading: isTyping
          ? IconButton(
              onPressed: () {
                isTyping = false;
                setState(() {});
              },icon: Icon(Icons.arrow_back))
          : IconButton(
              onPressed: () {
                Get.back();
              },icon: Icon(Icons.arrow_back_ios)),iconTheme: IconThemeData(color: Colors.black),centerTitle: true,actions: [
        isTyping
            ? IconButton(
                onPressed: () {
                  /* isTyping = true; */
                  _onRestoreAllData();
                  setState(() {});
                },icon: Icon(Icons.close))
            : IconButton(
                onPressed: () {
                  isTyping = true;
                  focusNode.requestFocus();
                  setState(() {});
                },icon: Icon(Icons.search)),],backgroundColor: Colors.white,title: isTyping
          ? TextField(
              onChanged: _onSearchChanged,controller: searchController,focusNode: focusNode,decoration: InputDecoration(
                  hintText: "Ara",enabledBorder: UnderlineInputBorder(
                    borderSide: BorderSide(color: Colors.white),focusedBorder: UnderlineInputBorder(
                    borderSide: BorderSide(color: Colors.white),)))
          : Text(
              "Müşteri Listesi",style: GoogleFonts.montserrat(color: Colors.black),elevation: 0,);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。