首页 > 解决方案 > Flutter SerachDelegate 修改 Hint Text Color 和 TextField Cursor Color

问题描述

我正在我的颤振应用程序中使用 SearchDelegate 实现搜索栏。

我已经覆盖了 ThemeData appBarTheme(BuildContext context) 函数以返回我的主 App ThemeData。

但是,这只会更改搜索视图,仅更改应用栏颜色。它不使用主题中定义的光标或提示颜色。

感谢任何建议。

标签: searchfluttercursorhint

解决方案


您是否在 Apple 模拟器上运行您的应用程序?因为 cursorColor 似乎是平台相关的。TextField 类的文档指出 cursorColor 字段

默认为 [ThemeData.cursorColor] 或 [CupertinoTheme.primaryColor],具体取决于 [ThemeData.platform]。

我必须创建一个 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,
  );
}

推荐阅读