Flutter - 如何更改搜索委托 class 中的文本颜色?
Flutter - how to change textcolor in search delegate class?
我设法更改了 hintStyle
-颜色
@override
ThemeData appBarTheme(BuildContext context) {
return ThemeData(
primaryColor: kPrimaryColor,
primaryIconTheme: IconThemeData(
color: Colors.white,
),
inputDecorationTheme: InputDecorationTheme(
hintStyle:
Theme.of(context).textTheme.title.copyWith(color: Colors.white),
),
);
}
但是如果我在应用栏搜索字段中输入内容,颜色仍然是黑色...
如何正确更改 SearchDelegate
class 中的 textcolor
?
参考问题评论中的讨论,appBarTheme
的textTheme
属性允许更改颜色。
示例代码来源 @Matthias
代码:
textTheme: TextTheme(title: TextStyle( color: Colors.white, fontSize: 18,),),
更改应用中的 headline6
文本样式 ThemeData
:
MaterialApp(
theme: ThemeData(
textTheme: TextTheme(
headline6: TextStyle(color: 'Your Prefered Color'))
),
home: Home()
);
使用 SearchDelegate,您可以自定义搜索的文本提示值和颜色以及查询的颜色和大小。为此:
搜索的文本提示值 --> 您可以覆盖 searchFieldLabel,它是一个字符串。
@override
String get searchFieldLabel => 'Your Custom Hint Text...';
搜索的颜色 --> 您可以使用主题的 hintColor 属性 覆盖 class:
@override
ThemeData appBarTheme(BuildContext context) {
return Theme.of(context).copyWith(
hintColor: Colors.white,
);
}
查询的文本颜色和大小 --> 您需要重写委托的 appBarTheme 方法并更改您需要的内容。要更改查询的文本颜色,请设置 headline6 的 textTheme:
@override
ThemeData appBarTheme(BuildContext context) {
assert(context != null);
final ThemeData theme = Theme.of(context).copyWith(
textTheme: TextTheme(
headline6: TextStyle(
color: Colors.white,
fontSize: 18.0,
),
),
);
assert(theme != null);
return theme;
}
所有这些关于 textTheme 的回答 属性 都会影响搜索页面其他部分的文本小部件。所以你最终会在浅色主题中看到有点透明的文本,因为文本颜色已经与背景混合了。
所以这是一个不完整的解决方案
这就是我为搜索委托设置主题的方式:
@override
ThemeData appBarTheme(BuildContext context) {
return Theme.of(context).copyWith(
inputDecorationTheme: searchFieldDecorationTheme,
textTheme: Theme.of(context).textTheme.copyWith(
headline6: TextStyle(color: Colors.white),
),
);
}
我复制全局 textTheme 样式,仅覆盖特定标题。
对于更多搜索样式(如提示颜色、搜索输入大小、输入下划线等),使用 inputDecorationTheme。
我设法更改了 hintStyle
-颜色
@override
ThemeData appBarTheme(BuildContext context) {
return ThemeData(
primaryColor: kPrimaryColor,
primaryIconTheme: IconThemeData(
color: Colors.white,
),
inputDecorationTheme: InputDecorationTheme(
hintStyle:
Theme.of(context).textTheme.title.copyWith(color: Colors.white),
),
);
}
但是如果我在应用栏搜索字段中输入内容,颜色仍然是黑色...
如何正确更改 SearchDelegate
class 中的 textcolor
?
参考问题评论中的讨论,appBarTheme
的textTheme
属性允许更改颜色。
示例代码来源 @Matthias
代码:
textTheme: TextTheme(title: TextStyle( color: Colors.white, fontSize: 18,),),
更改应用中的 headline6
文本样式 ThemeData
:
MaterialApp(
theme: ThemeData(
textTheme: TextTheme(
headline6: TextStyle(color: 'Your Prefered Color'))
),
home: Home()
);
使用 SearchDelegate,您可以自定义搜索的文本提示值和颜色以及查询的颜色和大小。为此:
搜索的文本提示值 --> 您可以覆盖 searchFieldLabel,它是一个字符串。
@override
String get searchFieldLabel => 'Your Custom Hint Text...';
搜索的颜色 --> 您可以使用主题的 hintColor 属性 覆盖 class:
@override
ThemeData appBarTheme(BuildContext context) {
return Theme.of(context).copyWith(
hintColor: Colors.white,
);
}
查询的文本颜色和大小 --> 您需要重写委托的 appBarTheme 方法并更改您需要的内容。要更改查询的文本颜色,请设置 headline6 的 textTheme:
@override
ThemeData appBarTheme(BuildContext context) {
assert(context != null);
final ThemeData theme = Theme.of(context).copyWith(
textTheme: TextTheme(
headline6: TextStyle(
color: Colors.white,
fontSize: 18.0,
),
),
);
assert(theme != null);
return theme;
}
所有这些关于 textTheme 的回答 属性 都会影响搜索页面其他部分的文本小部件。所以你最终会在浅色主题中看到有点透明的文本,因为文本颜色已经与背景混合了。
所以这是一个不完整的解决方案
这就是我为搜索委托设置主题的方式:
@override
ThemeData appBarTheme(BuildContext context) {
return Theme.of(context).copyWith(
inputDecorationTheme: searchFieldDecorationTheme,
textTheme: Theme.of(context).textTheme.copyWith(
headline6: TextStyle(color: Colors.white),
),
);
}
我复制全局 textTheme 样式,仅覆盖特定标题。 对于更多搜索样式(如提示颜色、搜索输入大小、输入下划线等),使用 inputDecorationTheme。