Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - how to change textcolor in search delegate class?

I managed to change the hintStyle-color

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),
    ),
  );
}

But if i type something into the appbar searchfield, the color is still black...

enter image description here

How can I properly change the textcolor in the SearchDelegate class?

like image 959
Matthias Avatar asked Jun 11 '20 10:06

Matthias


People also ask

How do you customize search delegate in flutter?

I found one way to customize flutter search delegate the way you want. you just have to copy flutter's search delegates code and then customize the code you want.

How do you change the search bar color on flutter?

There is a option known as backgroundColor in Appbar to change the color of your app bar. return new AppBar( backgroundColor: Colors.

How do I change the default blue color flutter?

How to Change the Default Primary Color of Flutter App. You need to pass a ThemeData to the theme parameter of MaterialApp in your Flutter App. You have to pass your own color of choice. You can also set the custom color as the default primary color of your App.


Video Answer


3 Answers

Using SearchDelegate you can customize both, Search's text hint value and color as well as query's color and size. To achieve this:

Search's text hint value --> you can override searchFieldLabel which is a String.

@override
String get searchFieldLabel => 'Your Custom Hint Text...';

Search's color --> you can override with hintColor property of the Theme class:

@override
  ThemeData appBarTheme(BuildContext context) {
    return Theme.of(context).copyWith(
      hintColor: Colors.white,
    );
  }

Query's text color and size --> you need to override delegate's appBarTheme method and change what you need. To change query's text color, set the textTheme of headline6:

@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;
}
like image 173
Carlos Daniel Avatar answered Oct 18 '22 07:10

Carlos Daniel


This is how I am theming search delegate:

  @override
  ThemeData appBarTheme(BuildContext context) {
    return Theme.of(context).copyWith(
      inputDecorationTheme: searchFieldDecorationTheme,
      textTheme: Theme.of(context).textTheme.copyWith(
        headline6: TextStyle(color: Colors.white),
      ),
    );
  }

I copy global textTheme styles, and override only specific headline. For more search styling (like hint color, search input size, input underline etc.), inputDecorationTheme is used.

like image 21
Andris Avatar answered Oct 18 '22 05:10

Andris


Change the headline6 text style in your app ThemeData :

MaterialApp(
      theme: ThemeData(
      textTheme: TextTheme(
          headline6: TextStyle(color: 'Your Prefered Color'))
      ),
      home: Home()
    );
like image 39
Kalsang Dorjee Avatar answered Oct 18 '22 06:10

Kalsang Dorjee