简体   繁体   English

Flutter - 如何更改搜索委托 class 中的文本颜色?

[英]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...但是,如果我在 appbar 搜索字段中输入一些内容,颜色仍然是黑色...

在此处输入图像描述

How can I properly change the textcolor in the SearchDelegate class?如何正确更改SearchDelegate textcolor中的文本颜色?

Using SearchDelegate you can customize both, Search's text hint value and color as well as query's color and size.使用 SearchDelegate 您可以自定义搜索的文本提示值和颜色以及查询的颜色和大小。 To achieve this:为达到这个:

Search's text hint value and color --> you can override searchFieldLabel and searchFieldStyle , the first is a String, the latter a TextStyle:搜索的文本提示值和颜色 --> 可以覆盖searchFieldLabelsearchFieldStyle ,第一个是 String,后者是 TextStyle:

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

@override
TextStyle get searchFieldStyle => TextStyle(
    color: Colors.white,
    fontSize: 18.0,
  );

Query's text color and size --> you need to override delegate's appBarTheme method and change what you need.查询的文本颜色和大小 --> 您需要覆盖委托的appBarTheme方法并更改您需要的内容。 To change query's text color, set the textTheme of headline6 :要更改查询的文本颜色,请设置header6textTheme

@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;
}

Change the headline6 text style in your app ThemeData :在您的应用ThemeData中更改headline6文本样式:

MaterialApp(
      theme: ThemeData(
      textTheme: TextTheme(
          headline6: TextStyle(color: 'Your Prefered Color'))
      ),
      home: Home()
    );

With reference to the discussion in the comments of the question, appBarTheme 's textTheme property allows changing the color.参考问题评论中的讨论, appBarThemetextTheme属性允许更改颜色。 example code credit @Matthias示例代码信用@Matthias

Code:代码:

textTheme: TextTheme(title: TextStyle( color: Colors.white, fontSize: 18,),),

All these answers regarding textTheme property affect the Text widget in other parts of the search page.所有这些关于 textTheme 属性的答案都会影响搜索页面其他部分的 Text 小部件。 So you would end up with somewhat transparent text in light theme because the text color has blended with the background.所以你最终会在浅色主题中得到一些透明的文本,因为文本颜色已经与背景混合。

So it is an incomplete solution所以这是一个不完整的解决方案

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.我复制全局 textTheme styles,并仅覆盖特定标题。 For more search styling (like hint color, search input size, input underline etc.), inputDecorationTheme is used.对于更多搜索样式(如提示颜色、搜索输入大小、输入下划线等),使用inputDecorationTheme

I added the hintColor:Colors.white line and it worked for me.我添加了hintColor:Colors.white线,它对我有用。

@override
ThemeData appBarTheme(BuildContext context) {
final ThemeData theme = Theme.of(context);
return theme.copyWith(
  primaryColor: Colors.blue,
  primaryIconTheme: theme.primaryIconTheme.copyWith(color: Colors.white),
  hintColor: Colors.white,
  
  textTheme: TextTheme(
    headline6: TextStyle(color: Colors.white,
    fontSize: 18)
    
  )
);

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

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