简体   繁体   English

Flutter SerachDelegate 修改 Hint Text Color 和 TextField Cursor Color

[英]Flutter SerachDelegate modify Hint Text Color and TextField Cursor Color

I'm implementing the search bar using SearchDelegate in my flutter app.我正在我的颤振应用程序中使用 SearchDelegate 实现搜索栏。

I've overridden the ThemeData appBarTheme(BuildContext context) function to return my main App ThemeData.我已经覆盖了 ThemeData appBarTheme(BuildContext context) 函数来返回我的主 App ThemeData。

However, this only changes the Search views, App Bar Color only.但是,这只会更改搜索视图,仅更改应用栏颜色。 It does not use the Cursor or Hint color defined in the theme.它不使用主题中定义的光标或提示颜色。

Appreciate any suggestions.感谢任何建议。

Are you running your app on the Apple Simulator?您是否在 Apple Simulator 上运行您的应用程序? Because cursorColor seems to be platform dependent.因为 cursorColor 似乎是平台相关的。 The documentation for the TextField class states that the cursorColor field TextField 类的文档指出 cursorColor 字段

Defaults to [ThemeData.cursorColor] or [CupertinoTheme.primaryColor] depending on [ThemeData.platform].默认为 [ThemeData.cursorColor] 或 [CupertinoTheme.primaryColor],具体取决于 [ThemeData.platform]。

I had to create a CupertinoThemeData and pass it to the ThemeData of my app in the main.dart file, like this:我必须创建一个 CupertinoThemeData 并将其传递给 main.dart 文件中我的应用程序的 ThemeData,如下所示:

@override
Widget build(BuildContext context) {
  return MaterialApp(
    theme: appBarCursorColorTheme(context),
    home: MyHomePage(); // MySearchDelegate contained inside MyHomePage()
  );
}

ThemeData appBarCursorColorTheme(BuildContext context) {
  final ThemeData theme = Theme.of(context);
  CupertinoThemeData ctd =
    CupertinoThemeData.raw(null, Colors.white, null, null, null, null);
  return theme.copyWith(
    cupertinoOverrideTheme: ctd,
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM