Android 如何自定义SearchDelegate(创建自定义搜索字段)
我一直在尝试将flatterAndroid 如何自定义SearchDelegate(创建自定义搜索字段),android,flutter,dart,Android,Flutter,Dart,我一直在尝试将flatterSearchDelgate自定义为我想要的搜索字段类型。它得到了一个名为appBarTheme的方法,返回类型为ThemeData。通常使用ThemeData可以更改appbar主题,但在我的情况下它不会做任何更改。我可以自定义提示文本样式searchFieldStyle方法,但仅此而已 以下是代码: class CustomSearchDelegate extends SearchDelegate<Country> { @override The
SearchDelgate
自定义为我想要的搜索字段类型。它得到了一个名为appBarTheme
的方法,返回类型为ThemeData
。通常使用ThemeData
可以更改appbar主题,但在我的情况下它不会做任何更改。我可以自定义提示文本样式searchFieldStyle
方法,但仅此而已
以下是代码:
class CustomSearchDelegate extends SearchDelegate<Country> {
@override
ThemeData appBarTheme(BuildContext context) {
return ThemeData(
appBarTheme: AppBarTheme(
elevation: 0,
color: themeColor,
//app bar color I wanted
),
);
}
@override
TextStyle get searchFieldStyle => TextStyle(
color: whiteTextColor,
fontWeight: FontWeight.w600,
fontFamily: GoogleFonts.poppins().fontFamily,
);
@override
List<Widget> buildActions(BuildContext context) {
return [
IconButton(
icon: Icon(
Icons.close_rounded,
color: Colors.white,
),
onPressed: () => query = '',
),
];
}
@override
Widget buildLeading(BuildContext context) {
return IconButton(
icon: Icon(
Icons.arrow_back_ios,
color: Colors.white,
),
onPressed: () {
close(context, null);
},
);
}
@override
Widget buildResults(BuildContext context) {
return Column(
children: [],
);
}
@override
Widget buildSuggestions(BuildContext context) {
return Column(
children: [],
);
}
}
class CustomSearchDelegate扩展了SearchDelegate{
@凌驾
主题数据应用程序(构建上下文){
返回主题数据(
阿帕巴瑟姆:阿帕巴瑟姆(
海拔:0,
颜色:themeColor,
//我想要的应用程序栏颜色
),
);
}
@凌驾
TextStyle获取搜索字段样式=>TextStyle(
颜色:whiteTextColor,
fontWeight:fontWeight.w600,
fontFamily:GoogleFonts.poppins().fontFamily,
);
@凌驾
列出buildActions(BuildContext上下文){
返回[
图标按钮(
图标:图标(
图标。关闭/四舍五入,
颜色:颜色,白色,
),
onPressed:()=>query='',
),
];
}
@凌驾
小部件buildLeading(BuildContext上下文){
返回图标按钮(
图标:图标(
Icons.arrow\u back\u ios,
颜色:颜色,白色,
),
已按下:(){
关闭(上下文,空);
},
);
}
@凌驾
小部件构建结果(构建上下文){
返回列(
儿童:[],
);
}
@凌驾
小部件构建建议(构建上下文){
返回列(
儿童:[],
);
}
}
如果有人能帮我解决这个问题,那就太有帮助了
此外,以前也曾提出过类似的问题,但从未得到回答
不幸的是,您无法完全控制
SearchDelegate
中的AppBar
主题,因为您在appBarTheme
中指定的一些主题属性值没有分配给SearchDelegate
中使用的AppBar小部件。你可以看看源代码。它只接受MaterialApp
theme属性中指定的主题数据中指定的值。在我的例子中,我需要更改光标颜色,但是更改MaterialApp
中的颜色也会修改其他地方使用的文本字段中的颜色
一种解决方案是,您甚至可以在打开
SearchDelegate
之前更改颜色,即在showSearch
之前更改颜色,并在从showSearch返回后将其重新更改为原始颜色。是的,我注意到它采用了MaterialApp中指定的主题数据属性,但AppBar颜色设置为即使在我改变了我的应用程序的原色之后,仍然是颤振应用程序的常用原色…谢谢你的回答。是的,你是对的。抱歉,我没有注意到您想要更改appBar的颜色。源代码中未正确分配属性。主题数据的primaryColor
是SearchDelegate