在SearchDelegate Flutter中更改searchFieldLabel的字体样式

时间:2020-02-01 01:53:45

标签: flutter dart material-design

我使用SearchDelegate的Material来实现SearchAppBar,并使用this question的响应来更改搜索委托的提示文本默认值。

但是,我遇到的问题是无法在搜索字段中更改提示文本的字体样式,该提示样式如下图所示:

Search Delegate hint

我已经有了要实现的样式,并且正在我的应用程序的另一种字体中使用此样式,但是我也想将此样式应用于此提示文本,以免与应用程序的样式指南冲突。

我用于更改搜索代表默认消息的代码:

class SearchRoomAppBar extends SearchDelegate {

    SearchRoomAppBar() : super(
        searchFieldLabel: "Search user",
        keyboardType: TextInputType.text,
        textInputAction: TextInputAction.search,
    );   
}

2 个答案:

答案 0 :(得分:0)

您可以覆盖SearchDelegate appBarTheme 方法:

  @override
  ThemeData appBarTheme(BuildContext context) {
      assert(context != null);
      final ThemeData theme = Theme.of(context);
      assert(theme != null);
      return theme;
  }

https://dartpad.dartlang.org/69724799fd6653ea4cf650a5a758c3d1

enter image description here

答案 1 :(得分:0)

SearchDelegate应用栏文本的样式设置为ThemeData.textTheme.headline6

默认情况下,SearchDelegate使用应用程序的ThemeData,但是您可以覆盖ThemeData appBarTheme(BuildContext context)方法以返回自定义版本。

  @override
  ThemeData appBarTheme(BuildContext context) {
    // You can use Theme.of(context) directly too
    var superThemeData = super.appBarTheme(context);

    return superThemeData.copyWith(
      textTheme: superThemeData.textTheme.copyWith(
        headline6: /* Whatever style you want to apply to your text */,
      ),
    );
  }