问题描述
我无法访问 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,);
这就是我的意思;
我做了什么;
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,);