Flutter SerachDelegate modify Hint Text Color and TextField Cursor Color
Asked Answered
S

1

2

I'm implementing the search bar using SearchDelegate in my flutter app.

I've overridden the ThemeData appBarTheme(BuildContext context) function to return my main 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.

Selenite answered 23/5, 2019 at 14:43 Comment(0)
D
0

Are you running your app on the Apple Simulator? Because cursorColor seems to be platform dependent. The documentation for the TextField class states that the cursorColor field

Defaults to [ThemeData.cursorColor] or [CupertinoTheme.primaryColor] depending on [ThemeData.platform].

I had to create a CupertinoThemeData and pass it to the ThemeData of my app in the main.dart file, like this:

@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,
  );
}
Delacroix answered 29/10, 2019 at 0:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.